Commit 59030a8cd4000fcf92d9b8225c6083f9e6ab86b8

Authored by aliguori
1 parent bc14ca24

gdbstub: Rework configuration via command line and monitor (Jan Kiszka)

Introduce a more canonical gdbstub configuration (system emulation only)
via the new switch '-gdb dev'. Keep '-s' as shorthand for
'-gdb tcp::1234'. Use the same syntax also for the corresponding monitor
command 'gdbserver'. Its default remains to listen on TCP port 1234.

Changes in v4:
 - Rebased over new command line switches meta file

Changes in v3:
 - Fix documentation

Changes in v2:
 - Support for pipe-based like to gdb (target remote | qemu -gdb stdio)
 - Properly update the qemu-doc

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6992 c046a42c-6fe2-441c-8c8c-71466251a162
gdbstub.c
@@ -2337,27 +2337,40 @@ static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len) @@ -2337,27 +2337,40 @@ static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2337 return len; 2337 return len;
2338 } 2338 }
2339 2339
2340 -int gdbserver_start(const char *port) 2340 +#ifndef _WIN32
  2341 +static void gdb_sigterm_handler(int signal)
  2342 +{
  2343 + if (vm_running)
  2344 + vm_stop(EXCP_INTERRUPT);
  2345 +}
  2346 +#endif
  2347 +
  2348 +int gdbserver_start(const char *device)
2341 { 2349 {
2342 GDBState *s; 2350 GDBState *s;
2343 - char gdbstub_port_name[128];  
2344 - int port_num;  
2345 - char *p; 2351 + char gdbstub_device_name[128];
2346 CharDriverState *chr = NULL; 2352 CharDriverState *chr = NULL;
2347 CharDriverState *mon_chr; 2353 CharDriverState *mon_chr;
2348 2354
2349 - if (!port || !*port)  
2350 - return -1;  
2351 - if (strcmp(port, "none") != 0) {  
2352 - port_num = strtol(port, &p, 10);  
2353 - if (*p == 0) {  
2354 - /* A numeric value is interpreted as a port number. */  
2355 - snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),  
2356 - "tcp::%d,nowait,nodelay,server", port_num);  
2357 - port = gdbstub_port_name; 2355 + if (!device)
  2356 + return -1;
  2357 + if (strcmp(device, "none") != 0) {
  2358 + if (strstart(device, "tcp:", NULL)) {
  2359 + /* enforce required TCP attributes */
  2360 + snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
  2361 + "%s,nowait,nodelay,server", device);
  2362 + device = gdbstub_device_name;
2358 } 2363 }
  2364 +#ifndef _WIN32
  2365 + else if (strcmp(device, "stdio") == 0) {
  2366 + struct sigaction act;
2359 2367
2360 - chr = qemu_chr_open("gdb", port, NULL); 2368 + memset(&act, 0, sizeof(act));
  2369 + act.sa_handler = gdb_sigterm_handler;
  2370 + sigaction(SIGINT, &act, NULL);
  2371 + }
  2372 +#endif
  2373 + chr = qemu_chr_open("gdb", device, NULL);
2361 if (!chr) 2374 if (!chr)
2362 return -1; 2375 return -1;
2363 2376
monitor.c
@@ -570,17 +570,18 @@ static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) @@ -570,17 +570,18 @@ static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
570 } 570 }
571 571
572 #ifdef CONFIG_GDBSTUB 572 #ifdef CONFIG_GDBSTUB
573 -static void do_gdbserver(Monitor *mon, const char *port)  
574 -{  
575 - if (!port)  
576 - port = DEFAULT_GDBSTUB_PORT;  
577 - if (gdbserver_start(port) < 0) {  
578 - monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",  
579 - port);  
580 - } else if (strcmp(port, "none") == 0) { 573 +static void do_gdbserver(Monitor *mon, const char *device)
  574 +{
  575 + if (!device)
  576 + device = "tcp::" DEFAULT_GDBSTUB_PORT;
  577 + if (gdbserver_start(device) < 0) {
  578 + monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
  579 + device);
  580 + } else if (strcmp(device, "none") == 0) {
581 monitor_printf(mon, "Disabled gdbserver\n"); 581 monitor_printf(mon, "Disabled gdbserver\n");
582 } else { 582 } else {
583 - monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port); 583 + monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
  584 + device);
584 } 585 }
585 } 586 }
586 #endif 587 #endif
qemu-options.hx
@@ -1216,19 +1216,25 @@ STEXI @@ -1216,19 +1216,25 @@ STEXI
1216 Do not start CPU at startup (you must type 'c' in the monitor). 1216 Do not start CPU at startup (you must type 'c' in the monitor).
1217 ETEXI 1217 ETEXI
1218 1218
1219 -DEF("s", 0, QEMU_OPTION_s, \  
1220 - "-s wait gdb connection to port\n")  
1221 -STEXI  
1222 -@item -s  
1223 -Wait gdb connection to port 1234 (@pxref{gdb_usage}). 1219 +DEF("gdb", HAS_ARG, QEMU_OPTION_gdb, \
  1220 + "-gdb dev wait for gdb connection on 'dev'\n")
  1221 +STEXI
  1222 +@item -gdb @var{dev}
  1223 +Wait for gdb connection on device @var{dev} (@pxref{gdb_usage}). Typical
  1224 +connections will likely be TCP-based, but also UDP, pseudo TTY, or even
  1225 +stdio are reasonable use case. The latter is allowing to start qemu from
  1226 +within gdb and establish the connection via a pipe:
  1227 +@example
  1228 +(gdb) target remote | exec qemu -gdb stdio ...
  1229 +@end example
1224 ETEXI 1230 ETEXI
1225 1231
1226 -DEF("p", HAS_ARG, QEMU_OPTION_p, \  
1227 - "-p port set gdb connection port [default=%s]\n") 1232 +DEF("s", 0, QEMU_OPTION_s, \
  1233 + "-s shorthand for -gdb tcp::%s\n")
1228 STEXI 1234 STEXI
1229 -@item -p @var{port}  
1230 -Change gdb connection port. @var{port} can be either a decimal number  
1231 -to specify a TCP port, or a host device (same devices as the serial port). 1235 +@item -s
  1236 +Shorthand for -gdb tcp::1234, i.e. open a gdbserver on TCP port 1234
  1237 +(@pxref{gdb_usage}).
1232 ETEXI 1238 ETEXI
1233 1239
1234 DEF("d", HAS_ARG, QEMU_OPTION_d, \ 1240 DEF("d", HAS_ARG, QEMU_OPTION_d, \
@@ -4233,8 +4233,7 @@ static void termsig_setup(void) @@ -4233,8 +4233,7 @@ static void termsig_setup(void)
4233 int main(int argc, char **argv, char **envp) 4233 int main(int argc, char **argv, char **envp)
4234 { 4234 {
4235 #ifdef CONFIG_GDBSTUB 4235 #ifdef CONFIG_GDBSTUB
4236 - int use_gdbstub;  
4237 - const char *gdbstub_port; 4236 + const char *gdbstub_dev = NULL;
4238 #endif 4237 #endif
4239 uint32_t boot_devices_bitmap = 0; 4238 uint32_t boot_devices_bitmap = 0;
4240 int i; 4239 int i;
@@ -4317,10 +4316,6 @@ int main(int argc, char **argv, char **envp) @@ -4317,10 +4316,6 @@ int main(int argc, char **argv, char **envp)
4317 initrd_filename = NULL; 4316 initrd_filename = NULL;
4318 ram_size = 0; 4317 ram_size = 0;
4319 vga_ram_size = VGA_RAM_SIZE; 4318 vga_ram_size = VGA_RAM_SIZE;
4320 -#ifdef CONFIG_GDBSTUB  
4321 - use_gdbstub = 0;  
4322 - gdbstub_port = DEFAULT_GDBSTUB_PORT;  
4323 -#endif  
4324 snapshot = 0; 4319 snapshot = 0;
4325 nographic = 0; 4320 nographic = 0;
4326 curses = 0; 4321 curses = 0;
@@ -4653,10 +4648,10 @@ int main(int argc, char **argv, char **envp) @@ -4653,10 +4648,10 @@ int main(int argc, char **argv, char **envp)
4653 break; 4648 break;
4654 #ifdef CONFIG_GDBSTUB 4649 #ifdef CONFIG_GDBSTUB
4655 case QEMU_OPTION_s: 4650 case QEMU_OPTION_s:
4656 - use_gdbstub = 1; 4651 + gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4657 break; 4652 break;
4658 - case QEMU_OPTION_p:  
4659 - gdbstub_port = optarg; 4653 + case QEMU_OPTION_gdb:
  4654 + gdbstub_dev = optarg;
4660 break; 4655 break;
4661 #endif 4656 #endif
4662 case QEMU_OPTION_L: 4657 case QEMU_OPTION_L:
@@ -5370,14 +5365,10 @@ int main(int argc, char **argv, char **envp) @@ -5370,14 +5365,10 @@ int main(int argc, char **argv, char **envp)
5370 } 5365 }
5371 5366
5372 #ifdef CONFIG_GDBSTUB 5367 #ifdef CONFIG_GDBSTUB
5373 - if (use_gdbstub) {  
5374 - /* XXX: use standard host:port notation and modify options  
5375 - accordingly. */  
5376 - if (gdbserver_start(gdbstub_port) < 0) {  
5377 - fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",  
5378 - gdbstub_port);  
5379 - exit(1);  
5380 - } 5368 + if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
  5369 + fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
  5370 + gdbstub_dev);
  5371 + exit(1);
5381 } 5372 }
5382 #endif 5373 #endif
5383 5374