GDBでプロセスID表示とdefine値表示

プロセスID表示

runした後、

(gdb) info proc id

を使う。

define値表示

  • gオプションでコンパイルすると定義内容が表示できないので、
(gdb) p FOOBAR
No symbol "FOOBAR" in current context.
(gdb) shell grep FOOBAR /usr/include/bar.h

としていたが、-g3オプションだとprintで表示可能でしかもinfo macroでどこに定義されているのかがわかる。

2つの例

$ cat test.h
/**
 * test.h
 * $Id$
 */
#ifndef _TEST_H
#define _TEST_H 1

#define TEST_STR "This is test"

#endif /* _TEST_H */
$ cat test.c
/**
 * test.c
 * $Id$
 */
#include <stdio.h>

#include "test.h"

int main(int argc, char **argv) {
	fprintf(stdout, "Hello World![%s]\n", TEST_STR);

	return 0;
}
$ cc -g3 -o test test.c
$ gdb ./test
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/kohchi/C/test...done.
(gdb) b main
Breakpoint 1 at 0x400513: file test.c, line 10.
(gdb) run
Starting program: /home/kohchi/C/test 

Breakpoint 1, main (argc=1, argv=0x7fffffffe218) at test.c:10
10		fprintf(stdout, "Hello World![%s]\n", TEST_STR);
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64
(gdb) info proc id
process 3516
cmdline = '/home/kohchi/C/test'
cwd = '/home/kohchi/C'
exe = '/home/kohchi/C/test'
(gdb) shell ps -fC test
UID        PID  PPID  C STIME TTY          TIME CMD
kohchi    3516  3511  0 14:17 pts/0    00:00:00 /home/kohchi/C/test
(gdb) info macro TEST_STR
Defined at /home/kohchi/C/test.h:8
  included at /home/kohchi/C/test.c:7
#define TEST_STR "This is test"
(gdb) p TEST_STR
$1 = "This is test"
(gdb) n
Hello World![This is test]
12		return 0;
(gdb) 
13	}
(gdb) 
0x000000303241ed1d in __libc_start_main () from /lib64/libc.so.6
(gdb) 
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
(gdb) quit
$