|
From: John R.
|
Tom Hughes wrote:
> In message <4341B3F4.9060605@BitWagon.com>
> John Reiser <jreiser@BitWagon.com> wrote:
>
>
>>>2) I'm not sure how you're supposed to continue execution from gdb.
>>
>>"detach" then "quit":
>
>
> A simple quit has always been enough in my experience - that was
> certainly the intention.
>
> Even though I wrote the current debugger attachment code it isn't
> actually something I actually use very often - hardly ever in
> fact - so there might well be a problem.
Looking carefully at the process numbers, the debugger is not dealing
with the original process itself, but with a fork(). [In the output below,
the original process is 9614, the fork is 9615, and the gdb is 9616.]
So the debugger cannot alter the state of the original process, such as
by "set var my_var = 123" Why? Running gdb on yourself works, and
with proper care can even be told to use the user's symbols, and ignore
valgrind's. See the second example below. [It might be necessary to
spoon-feed some 'add-symbol-file' commands ahead of interactive input.]
=====invoking gdb from valgrind(memcheck); note 3 processes: vg, vg.fork(), gdb.
==9614== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- y
starting debugger
==9614== starting debugger with cmd: /usr/bin/gdb -nw /proc/9615/fd/1015 9615
[gdb banner snipped]
Attaching to program: /proc/9615/fd/1015, process 9615
0xb0022d0d in ?? ()
(gdb) shell
[user@host ~]$ ps l
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
0 500 2561 2542 15 0 4512 1500 wait Ss pts/0 0:00 bash
0 500 9614 2561 16 0 1557980 8608 wait S pts/0 0:00 valgrind301
--db-attach=yes ./uninit
0 500 9616 9614 15 0 6972 2784 wait S pts/0 0:00 /usr/bin/gdb -nw
/proc/9615/fd/1015 9615
1 500 9615 9614 17 0 1557980 8612 finish T pts/0 0:00 valgrind301
--db-attach=yes ./uninit
0 500 9619 9616 15 0 4512 1488 wait S pts/0 0:00 bash
0 500 9639 9619 16 0 4440 812 - R+ pts/0 0:00 ps l
$
=====
=====invoking gdb on yourself
$ cat gdbself.c
#include <stdio.h>
#include <unistd.h>
int x;
char cmd[100];
main(int argc, char *argv[])
{
printf("x=%d\n", x);
sprintf(cmd, "gdb %s %d\n", argv[0], getpid());
printf("%s\n", cmd);
system(cmd);
printf("x=%d\n", x);
return 0;
}
$ gcc -g -o gdbself gdbself.c
$ ./gdbself
x=0 ## note original value
gdb ./gdbself 9797
[gdb banner snipped]
Attaching to program: ./gdbself, process 9797
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
0x001c9402 in ?? ()
(gdb) shell
$ ps l
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
0 500 9619 2542 16 0 4516 1488 - Ss+ pts/1 0:00 bash
0 500 9798 9797 15 0 8736 4468 wait S pts/0 0:00 gdb ./gdbself 9797
0 500 9797 9619 16 0 1412 344 ptrace T pts/0 0:00 ./gdbself
0 500 9803 9798 15 0 4516 1492 wait S pts/0 0:00 bash
0 500 9823 9803 16 0 4444 812 - R+ pts/0 0:00 ps l
$ exit
(gdb) set var x = 123 ## change value in debugger
(gdb) q
The program is running. Quit anyway (and detach it)? (y or n) y
Detaching from program: ./gdbself, process 9797
x=123 ## note that the value got changed
$
=====
--
|