以The C Programming Language中1.9节的程序为例,试用了以下命令:
1.backtrace
,简写bt
,查看函数调用的顺序(函数调用栈的信息);
2.frame N
,切换到栈编号为N的上下文中;
3.info frame
,简写info f
,查看当前函数调用的栈帧信息 ;
4.info locals
,查看当前局部变量的值;
5.info args
,查看当前函数参数的值;
6.set pagination off
,关闭分页显示;
7.l 1,50
,显示程序的第1-50行;
注意main()中调用getlines的时候实参是line和MAXLINE,getlines的形参是s和lim,在16行的断点处查看line的地址,为0xbfffe7bc,在第29行的断点处发现s=0xbfffe7bc,两者一致,说明了把数组名作为参数时,传递给函数的是数组起始元素的地址,所以getlines中对s进行修改也就是对main中的line进行修改,而无需特地返回一个修改后的数组。MAXLINE是宏,暂时无法直接查看,似乎在用gcc时要设置其他参数才可。
记录如下:
xxx@xxx-VirtualBox:~/test$ gdb 1-9 -q
Reading symbols from 1-9...done.
(gdb) set pagination off
(gdb) l 1,50
1 #include <stdio.h>
2
3 #define MAXLINE 1000
4
5 int getlines(char line[], int maxline);
6 void copy(char to[], char from[]);
7
8 int main()
9 {
10 int len;
11 int max;
12 char line[MAXLINE];
13 char longest[MAXLINE];
14
15 max = 0;
16 while ((len = getlines(line, MAXLINE)) > 0)
17 if (len > max) {
18 max = len;
19 copy(longest, line);
20 }
21 if (max > 0)
22 printf("%s", longest);
23 return 0;
24 }
25
26 int getlines(char s[], int lim)
27 {
28 int c, i;
29 for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
30 s[i] = c;
31 if (c == '\n') {
32 s[i] = c;
33 ++i;
34 }
35 s[i] = '\0';
36 return i;
37 }
38
39 void copy(char to[], char from[])
40 {
41 int i;
42 i = 0;
43 while ((to[i] = from[i]) != '\0')
44 ++i;
45 }
(gdb) b 16
Breakpoint 1 at 0x80484c4: file 1-9.c, line 16.
(gdb) b 29
Breakpoint 2 at 0x8048563: file 1-9.c, line 29.
(gdb) b 43
Breakpoint 3 at 0x80485d0: file 1-9.c, line 43.
(gdb) r
Starting program: /home/ant/test/1-9
Breakpoint 1, main () at 1-9.c:16
16 while ((len = getlines(line, MAXLINE)) > 0)
(gdb) p &line
$1 = (char (*)[1000]) 0xbfffe7bc
(gdb) info f
Stack level 0, frame at 0xbfffefb0:
eip = 0x80484c4 in main (1-9.c:16); saved eip = 0xb7e1e637
source language c.
Arglist at 0xbfffef98, args:
Locals at 0xbfffef98, Previous frame's sp is 0xbfffefb0
Saved registers:
ebp at 0xbfffef98, eip at 0xbfffefac
(gdb) c
Continuing.
Breakpoint 2, getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
29 for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
(gdb) bt
#0 getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
#1 0x0804850d in main () at 1-9.c:16
(gdb) frame 0
#0 getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
29 for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
(gdb) info locals
c = 0
i = 0
(gdb) frame 1
#1 0x0804850d in main () at 1-9.c:16
16 while ((len = getlines(line, MAXLINE)) > 0)
(gdb) info locals
len = 0
max = 0
line = '\000' <repeats 65 times>, "\360\377\267\000\000\000\000\000\000\000\000\251\071\377\267\000\220\373\267\000\220\373\267\260Q\375\267(\352\377\277\270\022\376\267\000\220\373\267\034*\000\000\003\000\000\000\062\000\000\000\377\377\377\377\000\000\000\000,\001\000\000A\a\376\267\000\000\000\000\000\000\033\000\\\361\032\000\\\361\032\000\000\000\000\000\005\000\000\000\000\000\033\000\000\060\033\000\324.\033\000\034Z\033\000\000\360\032\000\003", '\000' <repeats 31 times>...
longest = "P\345td\214P\026\000\214P\026\000\214P\026\000\234a\000\000\234a\000\000\004\000\000\000\004\000\000\000Q\345td", '\000' <repeats 20 times>, "\006\000\000\000\020\000\000\000R\345tdi2\376\267h\356\377\277\260}\373\267͒ᷘ\356\377\277\222\177\376\267", '\000' <repeats 12 times>, "\003\000\000\000GNU\000\335Q\222\247\000\000\000\000\004\243\376\267h\356\377\277\000\000\000\000u\272\375\267`?\376\267\345\222\341\267\067\266\375\267\000\000\000\000X\370\377\267\000\000\000\000d\356\377\277`\356\377\277\020ii\r\000\002\000\000d\356\377\277\071>\376\267\244\201\340\267\016\002\000\000\260Q\375\267\307\016\340==F\376"...
(gdb) frame 0
#0 getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
29 for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
(gdb) c
Continuing.
ab
Breakpoint 3, copy (to=0xbfffeba4 "P\345td\214P\026", from=0xbfffe7bc "ab\n") at 1-9.c:43
43 while ((to[i] = from[i]) != '\0')
(gdb) bt
#0 copy (to=0xbfffeba4 "P\345td\214P\026", from=0xbfffe7bc "ab\n") at 1-9.c:43
#1 0x080484f6 in main () at 1-9.c:19
(gdb) info locals
i = 0
(gdb) n
44 ++i;
(gdb) n
43 while ((to[i] = from[i]) != '\0')
(gdb) p to
$2 = 0xbfffeba4 "a\345td\214P\026"
(gdb) c
Continuing.
Breakpoint 2, getlines (s=0xbfffe7bc "ab\n", lim=1000) at 1-9.c:29
29 for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
(gdb) Quit