Commit b4608c0455a410994b31888b458a552d640b9765

Authored by bellard
1 parent d5a0b50c

added gdb support to vl


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@288 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 3 changed files with 497 additions and 65 deletions
Makefile
... ... @@ -98,7 +98,7 @@ SRCS:= $(OBJS:.o=.c)
98 98 OBJS+= libqemu.a
99 99  
100 100 # cpu emulator library
101   -LIBOBJS=thunk.o exec.o translate.o cpu-exec.o
  101 +LIBOBJS=thunk.o exec.o translate.o cpu-exec.o gdbstub.o
102 102  
103 103 ifeq ($(TARGET_ARCH), i386)
104 104 LIBOBJS+=translate-i386.o op-i386.o helper-i386.o
... ...
gdbstub.c 0 → 100644
  1 +/*
  2 + * gdb server stub
  3 + *
  4 + * Copyright (c) 2003 Fabrice Bellard
  5 + *
  6 + * This library is free software; you can redistribute it and/or
  7 + * modify it under the terms of the GNU Lesser General Public
  8 + * License as published by the Free Software Foundation; either
  9 + * version 2 of the License, or (at your option) any later version.
  10 + *
  11 + * This library is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 + * Lesser General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public
  17 + * License along with this library; if not, write to the Free Software
  18 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 + */
  20 +#include <stdlib.h>
  21 +#include <stdio.h>
  22 +#include <string.h>
  23 +#include <unistd.h>
  24 +#include <errno.h>
  25 +#include <sys/socket.h>
  26 +#include <netinet/in.h>
  27 +#include <netinet/tcp.h>
  28 +#include <signal.h>
  29 +
  30 +#include "config.h"
  31 +#ifdef TARGET_I386
  32 +#include "cpu-i386.h"
  33 +#endif
  34 +#ifdef TARGET_ARM
  35 +#include "cpu-arm.h"
  36 +#endif
  37 +#include "thunk.h"
  38 +#include "exec.h"
  39 +
  40 +//#define DEBUG_GDB
  41 +
  42 +int gdbstub_fd = -1;
  43 +
  44 +/* return 0 if OK */
  45 +static int gdbstub_open(int port)
  46 +{
  47 + struct sockaddr_in sockaddr;
  48 + socklen_t len;
  49 + int fd, val, ret;
  50 +
  51 + fd = socket(PF_INET, SOCK_STREAM, 0);
  52 + if (fd < 0) {
  53 + perror("socket");
  54 + return -1;
  55 + }
  56 +
  57 + /* allow fast reuse */
  58 + val = 1;
  59 + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  60 +
  61 + sockaddr.sin_family = AF_INET;
  62 + sockaddr.sin_port = htons(port);
  63 + sockaddr.sin_addr.s_addr = 0;
  64 + ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  65 + if (ret < 0) {
  66 + perror("bind");
  67 + return -1;
  68 + }
  69 + ret = listen(fd, 0);
  70 + if (ret < 0) {
  71 + perror("listen");
  72 + return -1;
  73 + }
  74 +
  75 + /* now wait for one connection */
  76 + for(;;) {
  77 + len = sizeof(sockaddr);
  78 + gdbstub_fd = accept(fd, (struct sockaddr *)&sockaddr, &len);
  79 + if (gdbstub_fd < 0 && errno != EINTR) {
  80 + perror("accept");
  81 + return -1;
  82 + } else if (gdbstub_fd >= 0) {
  83 + break;
  84 + }
  85 + }
  86 +
  87 + /* set short latency */
  88 + val = 1;
  89 + setsockopt(gdbstub_fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
  90 + return 0;
  91 +}
  92 +
  93 +static int get_char(void)
  94 +{
  95 + uint8_t ch;
  96 + int ret;
  97 +
  98 + for(;;) {
  99 + ret = read(gdbstub_fd, &ch, 1);
  100 + if (ret < 0) {
  101 + if (errno != EINTR && errno != EAGAIN)
  102 + return -1;
  103 + } else if (ret == 0) {
  104 + return -1;
  105 + } else {
  106 + break;
  107 + }
  108 + }
  109 + return ch;
  110 +}
  111 +
  112 +static void put_buffer(const uint8_t *buf, int len)
  113 +{
  114 + int ret;
  115 +
  116 + while (len > 0) {
  117 + ret = write(gdbstub_fd, buf, len);
  118 + if (ret < 0) {
  119 + if (errno != EINTR && errno != EAGAIN)
  120 + return;
  121 + } else {
  122 + buf += ret;
  123 + len -= ret;
  124 + }
  125 + }
  126 +}
  127 +
  128 +static inline int fromhex(int v)
  129 +{
  130 + if (v >= '0' && v <= '9')
  131 + return v - '0';
  132 + else if (v >= 'A' && v <= 'F')
  133 + return v - 'A' + 10;
  134 + else if (v >= 'a' && v <= 'f')
  135 + return v - 'a' + 10;
  136 + else
  137 + return 0;
  138 +}
  139 +
  140 +static inline int tohex(int v)
  141 +{
  142 + if (v < 10)
  143 + return v + '0';
  144 + else
  145 + return v - 10 + 'a';
  146 +}
  147 +
  148 +static void memtohex(char *buf, const uint8_t *mem, int len)
  149 +{
  150 + int i, c;
  151 + char *q;
  152 + q = buf;
  153 + for(i = 0; i < len; i++) {
  154 + c = mem[i];
  155 + *q++ = tohex(c >> 4);
  156 + *q++ = tohex(c & 0xf);
  157 + }
  158 + *q = '\0';
  159 +}
  160 +
  161 +static void hextomem(uint8_t *mem, const char *buf, int len)
  162 +{
  163 + int i;
  164 +
  165 + for(i = 0; i < len; i++) {
  166 + mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
  167 + buf += 2;
  168 + }
  169 +}
  170 +
  171 +/* return -1 if error or EOF */
  172 +static int get_packet(char *buf, int buf_size)
  173 +{
  174 + int ch, len, csum, csum1;
  175 + char reply[1];
  176 +
  177 + for(;;) {
  178 + for(;;) {
  179 + ch = get_char();
  180 + if (ch < 0)
  181 + return -1;
  182 + if (ch == '$')
  183 + break;
  184 + }
  185 + len = 0;
  186 + csum = 0;
  187 + for(;;) {
  188 + ch = get_char();
  189 + if (ch < 0)
  190 + return -1;
  191 + if (ch == '#')
  192 + break;
  193 + if (len > buf_size - 1)
  194 + return -1;
  195 + buf[len++] = ch;
  196 + csum += ch;
  197 + }
  198 + buf[len] = '\0';
  199 + ch = get_char();
  200 + if (ch < 0)
  201 + return -1;
  202 + csum1 = fromhex(ch) << 4;
  203 + ch = get_char();
  204 + if (ch < 0)
  205 + return -1;
  206 + csum1 |= fromhex(ch);
  207 + if ((csum & 0xff) != csum1) {
  208 + reply[0] = '-';
  209 + put_buffer(reply, 1);
  210 + } else {
  211 + reply[0] = '+';
  212 + put_buffer(reply, 1);
  213 + break;
  214 + }
  215 + }
  216 +#ifdef DEBUG_GDB
  217 + printf("command='%s'\n", buf);
  218 +#endif
  219 + return len;
  220 +}
  221 +
  222 +/* return -1 if error, 0 if OK */
  223 +static int put_packet(char *buf)
  224 +{
  225 + char buf1[3];
  226 + int len, csum, ch, i;
  227 +
  228 +#ifdef DEBUG_GDB
  229 + printf("reply='%s'\n", buf);
  230 +#endif
  231 +
  232 + for(;;) {
  233 + buf1[0] = '$';
  234 + put_buffer(buf1, 1);
  235 + len = strlen(buf);
  236 + put_buffer(buf, len);
  237 + csum = 0;
  238 + for(i = 0; i < len; i++) {
  239 + csum += buf[i];
  240 + }
  241 + buf1[0] = '#';
  242 + buf1[1] = tohex((csum >> 4) & 0xf);
  243 + buf1[2] = tohex((csum) & 0xf);
  244 +
  245 + put_buffer(buf1, 3);
  246 +
  247 + ch = get_char();
  248 + if (ch < 0)
  249 + return -1;
  250 + if (ch == '+')
  251 + break;
  252 + }
  253 + return 0;
  254 +}
  255 +
  256 +static int memory_rw(uint8_t *buf, uint32_t addr, int len, int is_write)
  257 +{
  258 + int l, flags;
  259 + uint32_t page;
  260 +
  261 + while (len > 0) {
  262 + page = addr & TARGET_PAGE_MASK;
  263 + l = (page + TARGET_PAGE_SIZE) - addr;
  264 + if (l > len)
  265 + l = len;
  266 + flags = page_get_flags(page);
  267 + if (!(flags & PAGE_VALID))
  268 + return -1;
  269 + if (is_write) {
  270 + if (!(flags & PAGE_WRITE))
  271 + return -1;
  272 + memcpy((uint8_t *)addr, buf, l);
  273 + } else {
  274 + if (!(flags & PAGE_READ))
  275 + return -1;
  276 + memcpy(buf, (uint8_t *)addr, l);
  277 + }
  278 + len -= l;
  279 + buf += l;
  280 + addr += l;
  281 + }
  282 + return 0;
  283 +}
  284 +
  285 +/* port = 0 means default port */
  286 +int cpu_gdbstub(void *opaque, void (*main_loop)(void *opaque), int port)
  287 +{
  288 + CPUState *env;
  289 + const char *p;
  290 + int ret, ch, nb_regs, i;
  291 + char buf[4096];
  292 + uint8_t mem_buf[2000];
  293 + uint32_t *registers;
  294 + uint32_t addr, len;
  295 +
  296 + printf("Waiting gdb connection on port %d\n", port);
  297 + if (gdbstub_open(port) < 0)
  298 + return -1;
  299 + printf("Connected\n");
  300 + for(;;) {
  301 + ret = get_packet(buf, sizeof(buf));
  302 + if (ret < 0)
  303 + break;
  304 + p = buf;
  305 + ch = *p++;
  306 + switch(ch) {
  307 + case '?':
  308 + snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
  309 + put_packet(buf);
  310 + break;
  311 + case 'c':
  312 + main_loop(opaque);
  313 + snprintf(buf, sizeof(buf), "S%02x", 0);
  314 + put_packet(buf);
  315 + break;
  316 + case 'g':
  317 + env = cpu_gdbstub_get_env(opaque);
  318 + registers = (void *)mem_buf;
  319 +#if defined(TARGET_I386)
  320 + for(i = 0; i < 8; i++) {
  321 + registers[i] = tswapl(env->regs[i]);
  322 + }
  323 + registers[8] = env->eip;
  324 + registers[9] = env->eflags;
  325 + registers[10] = env->segs[R_CS].selector;
  326 + registers[11] = env->segs[R_SS].selector;
  327 + registers[12] = env->segs[R_DS].selector;
  328 + registers[13] = env->segs[R_ES].selector;
  329 + registers[14] = env->segs[R_FS].selector;
  330 + registers[15] = env->segs[R_GS].selector;
  331 + nb_regs = 16;
  332 +#endif
  333 + memtohex(buf, (const uint8_t *)registers,
  334 + sizeof(registers[0]) * nb_regs);
  335 + put_packet(buf);
  336 + break;
  337 + case 'G':
  338 + env = cpu_gdbstub_get_env(opaque);
  339 + registers = (void *)mem_buf;
  340 +#if defined(TARGET_I386)
  341 + hextomem((uint8_t *)registers, p, 16 * 4);
  342 + for(i = 0; i < 8; i++) {
  343 + env->regs[i] = tswapl(registers[i]);
  344 + }
  345 + env->eip = registers[8];
  346 + env->eflags = registers[9];
  347 +#define LOAD_SEG(index, sreg)\
  348 + if (tswapl(registers[index]) != env->segs[sreg].selector)\
  349 + cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
  350 + LOAD_SEG(10, R_CS);
  351 + LOAD_SEG(11, R_SS);
  352 + LOAD_SEG(12, R_DS);
  353 + LOAD_SEG(13, R_ES);
  354 + LOAD_SEG(14, R_FS);
  355 + LOAD_SEG(15, R_GS);
  356 +#endif
  357 + put_packet("OK");
  358 + break;
  359 + case 'm':
  360 + addr = strtoul(p, (char **)&p, 16);
  361 + if (*p == ',')
  362 + p++;
  363 + len = strtoul(p, NULL, 16);
  364 + if (memory_rw(mem_buf, addr, len, 0) != 0)
  365 + memset(mem_buf, 0, len);
  366 + memtohex(buf, mem_buf, len);
  367 + put_packet(buf);
  368 + break;
  369 + case 'M':
  370 + addr = strtoul(p, (char **)&p, 16);
  371 + if (*p == ',')
  372 + p++;
  373 + len = strtoul(p, (char **)&p, 16);
  374 + if (*p == ',')
  375 + p++;
  376 + hextomem(mem_buf, p, len);
  377 + if (memory_rw(mem_buf, addr, len, 1) != 0)
  378 + put_packet("ENN");
  379 + else
  380 + put_packet("OK");
  381 + break;
  382 + default:
  383 + /* put empty packet */
  384 + buf[0] = '\0';
  385 + put_packet(buf);
  386 + break;
  387 + }
  388 + }
  389 + return 0;
  390 +}
... ...
... ... @@ -1783,27 +1783,116 @@ static void host_alarm_handler(int host_signum, siginfo_t *info,
1783 1783 }
1784 1784 }
1785 1785  
  1786 +/* main execution loop */
  1787 +
  1788 +CPUState *cpu_gdbstub_get_env(void *opaque)
  1789 +{
  1790 + return global_env;
  1791 +}
  1792 +
  1793 +void main_loop(void *opaque)
  1794 +{
  1795 + struct pollfd ufds[2], *pf, *serial_ufd, *net_ufd, *gdb_ufd;
  1796 + int ret, n, timeout;
  1797 + uint8_t ch;
  1798 + CPUState *env = global_env;
  1799 +
  1800 + for(;;) {
  1801 +
  1802 + ret = cpu_x86_exec(env);
  1803 +
  1804 + /* if hlt instruction, we wait until the next IRQ */
  1805 + if (ret == EXCP_HLT)
  1806 + timeout = 10;
  1807 + else
  1808 + timeout = 0;
  1809 + /* poll any events */
  1810 + serial_ufd = NULL;
  1811 + pf = ufds;
  1812 + if (!(serial_ports[0].lsr & UART_LSR_DR)) {
  1813 + serial_ufd = pf;
  1814 + pf->fd = 0;
  1815 + pf->events = POLLIN;
  1816 + pf++;
  1817 + }
  1818 + net_ufd = NULL;
  1819 + if (net_fd > 0 && ne2000_can_receive(&ne2000_state)) {
  1820 + net_ufd = pf;
  1821 + pf->fd = net_fd;
  1822 + pf->events = POLLIN;
  1823 + pf++;
  1824 + }
  1825 + gdb_ufd = NULL;
  1826 + if (gdbstub_fd > 0) {
  1827 + gdb_ufd = pf;
  1828 + pf->fd = gdbstub_fd;
  1829 + pf->events = POLLIN;
  1830 + pf++;
  1831 + }
  1832 +
  1833 + ret = poll(ufds, pf - ufds, timeout);
  1834 + if (ret > 0) {
  1835 + if (serial_ufd && (serial_ufd->revents & POLLIN)) {
  1836 + n = read(0, &ch, 1);
  1837 + if (n == 1) {
  1838 + serial_received_byte(&serial_ports[0], ch);
  1839 + }
  1840 + }
  1841 + if (net_ufd && (net_ufd->revents & POLLIN)) {
  1842 + uint8_t buf[MAX_ETH_FRAME_SIZE];
  1843 +
  1844 + n = read(net_fd, buf, MAX_ETH_FRAME_SIZE);
  1845 + if (n > 0) {
  1846 + if (n < 60) {
  1847 + memset(buf + n, 0, 60 - n);
  1848 + n = 60;
  1849 + }
  1850 + ne2000_receive(&ne2000_state, buf, n);
  1851 + }
  1852 + }
  1853 + if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
  1854 + uint8_t buf[1];
  1855 + /* stop emulation if requested by gdb */
  1856 + n = read(gdbstub_fd, buf, 1);
  1857 + if (n == 1)
  1858 + break;
  1859 + }
  1860 + }
  1861 +
  1862 + /* timer IRQ */
  1863 + if (timer_irq_pending) {
  1864 + pic_set_irq(0, 1);
  1865 + pic_set_irq(0, 0);
  1866 + timer_irq_pending = 0;
  1867 + }
  1868 +
  1869 + pic_handle_irq();
  1870 + }
  1871 +}
  1872 +
1786 1873 void help(void)
1787 1874 {
1788 1875 printf("Virtual Linux version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
1789   - "usage: vl [-h] bzImage initrd [kernel parameters...]\n"
  1876 + "usage: vl [options] bzImage initrd [kernel parameters...]\n"
1790 1877 "\n"
1791 1878 "'bzImage' is a Linux kernel image (PAGE_OFFSET must be defined\n"
1792 1879 "to 0x90000000 in asm/page.h and arch/i386/vmlinux.lds)\n"
1793 1880 "'initrd' is an initrd image\n"
1794 1881 "-m megs set virtual RAM size to megs MB\n"
1795 1882 "-n script set network init script [default=%s]\n"
  1883 + "-s wait gdb connection to port %d\n"
  1884 + "-p port change gdb connection port\n"
1796 1885 "-d output log in /tmp/vl.log\n"
1797 1886 "\n"
1798 1887 "During emulation, use C-a h to get terminal commands:\n",
1799   - DEFAULT_NETWORK_SCRIPT);
  1888 + DEFAULT_NETWORK_SCRIPT, DEFAULT_GDBSTUB_PORT);
1800 1889 term_print_help();
1801 1890 exit(1);
1802 1891 }
1803 1892  
1804 1893 int main(int argc, char **argv)
1805 1894 {
1806   - int c, ret, initrd_size, i;
  1895 + int c, ret, initrd_size, i, use_gdbstub, gdbstub_port;
1807 1896 struct linux_params *params;
1808 1897 struct sigaction act;
1809 1898 struct itimerval itv;
... ... @@ -1815,8 +1904,10 @@ int main(int argc, char **argv)
1815 1904  
1816 1905 phys_ram_size = 32 * 1024 * 1024;
1817 1906 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
  1907 + use_gdbstub = 0;
  1908 + gdbstub_port = DEFAULT_GDBSTUB_PORT;
1818 1909 for(;;) {
1819   - c = getopt(argc, argv, "hm:dn:");
  1910 + c = getopt(argc, argv, "hm:dn:sp:");
1820 1911 if (c == -1)
1821 1912 break;
1822 1913 switch(c) {
... ... @@ -1834,6 +1925,12 @@ int main(int argc, char **argv)
1834 1925 case 'n':
1835 1926 pstrcpy(network_script, sizeof(network_script), optarg);
1836 1927 break;
  1928 + case 's':
  1929 + use_gdbstub = 1;
  1930 + break;
  1931 + case 'p':
  1932 + gdbstub_port = atoi(optarg);
  1933 + break;
1837 1934 }
1838 1935 }
1839 1936 if (optind + 1 >= argc)
... ... @@ -1974,66 +2071,11 @@ int main(int argc, char **argv)
1974 2071 getitimer(ITIMER_REAL, &itv);
1975 2072 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1976 2073 1000000;
1977   -
1978   - for(;;) {
1979   - struct pollfd ufds[2], *pf, *serial_ufd, *net_ufd;
1980   - int ret, n, timeout;
1981   - uint8_t ch;
1982   -
1983   - ret = cpu_x86_exec(env);
1984   -
1985   - /* if hlt instruction, we wait until the next IRQ */
1986   - if (ret == EXCP_HLT)
1987   - timeout = 10;
1988   - else
1989   - timeout = 0;
1990   - /* poll any events */
1991   - serial_ufd = NULL;
1992   - net_ufd = NULL;
1993   - pf = ufds;
1994   - if (!(serial_ports[0].lsr & UART_LSR_DR)) {
1995   - serial_ufd = pf;
1996   - pf->fd = 0;
1997   - pf->events = POLLIN;
1998   - pf++;
1999   - }
2000   - if (net_fd > 0 && ne2000_can_receive(&ne2000_state)) {
2001   - net_ufd = pf;
2002   - pf->fd = net_fd;
2003   - pf->events = POLLIN;
2004   - pf++;
2005   - }
2006   - ret = poll(ufds, pf - ufds, timeout);
2007   - if (ret > 0) {
2008   - if (serial_ufd && (serial_ufd->revents & POLLIN)) {
2009   - n = read(0, &ch, 1);
2010   - if (n == 1) {
2011   - serial_received_byte(&serial_ports[0], ch);
2012   - }
2013   - }
2014   - if (net_ufd && (net_ufd->revents & POLLIN)) {
2015   - uint8_t buf[MAX_ETH_FRAME_SIZE];
2016   -
2017   - n = read(net_fd, buf, MAX_ETH_FRAME_SIZE);
2018   - if (n > 0) {
2019   - if (n < 60) {
2020   - memset(buf + n, 0, 60 - n);
2021   - n = 60;
2022   - }
2023   - ne2000_receive(&ne2000_state, buf, n);
2024   - }
2025   - }
2026   - }
2027   -
2028   - /* timer IRQ */
2029   - if (timer_irq_pending) {
2030   - pic_set_irq(0, 1);
2031   - pic_set_irq(0, 0);
2032   - timer_irq_pending = 0;
2033   - }
2034   -
2035   - pic_handle_irq();
  2074 +
  2075 + if (use_gdbstub) {
  2076 + cpu_gdbstub(NULL, main_loop, gdbstub_port);
  2077 + } else {
  2078 + main_loop(NULL);
2036 2079 }
2037   -
2038 2080 return 0;
2039 2081 }
... ...