其他分享
首页 > 其他分享> > 初学GDB(2)

初学GDB(2)

作者:互联网

搬运自我的CSDN https://blog.csdn.net/u013213111/article/details/88096937

以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时要设置其他参数才可。

记录如下:

  1 xxx@xxx-VirtualBox:~/test$ gdb 1-9 -q
  2 Reading symbols from 1-9...done.
  3 (gdb) set pagination off
  4 (gdb) l 1,50
  5 1    #include <stdio.h>
  6 2    
  7 3    #define MAXLINE 1000
  8 4    
  9 5    int getlines(char line[], int maxline);
 10 6    void copy(char to[], char from[]);
 11 7    
 12 8    int main()
 13 9    {
 14 10        int len;
 15 11        int max;
 16 12        char line[MAXLINE];
 17 13        char longest[MAXLINE];
 18 14    
 19 15        max = 0;
 20 16        while ((len = getlines(line, MAXLINE)) > 0)
 21 17            if (len > max) {
 22 18                max = len;
 23 19                copy(longest, line);
 24 20            }
 25 21        if (max > 0)
 26 22            printf("%s", longest);
 27 23        return 0;
 28 24    }
 29 25    
 30 26    int getlines(char s[], int lim)
 31 27    {
 32 28        int c, i;
 33 29        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
 34 30            s[i] = c;
 35 31        if (c == '\n') {
 36 32            s[i] = c;
 37 33            ++i;
 38 34        }
 39 35        s[i] = '\0';
 40 36        return i;
 41 37    }
 42 38    
 43 39    void copy(char to[], char from[])
 44 40    {
 45 41        int i;
 46 42        i = 0;
 47 43        while ((to[i] = from[i]) != '\0')
 48 44            ++i;
 49 45    }
 50 (gdb) b 16
 51 Breakpoint 1 at 0x80484c4: file 1-9.c, line 16.
 52 (gdb) b 29
 53 Breakpoint 2 at 0x8048563: file 1-9.c, line 29.
 54 (gdb) b 43
 55 Breakpoint 3 at 0x80485d0: file 1-9.c, line 43.
 56 (gdb) r
 57 Starting program: /home/ant/test/1-9 
 58 
 59 Breakpoint 1, main () at 1-9.c:16
 60 16        while ((len = getlines(line, MAXLINE)) > 0)
 61 (gdb) p &line
 62 $1 = (char (*)[1000]) 0xbfffe7bc
 63 (gdb) info f
 64 Stack level 0, frame at 0xbfffefb0:
 65  eip = 0x80484c4 in main (1-9.c:16); saved eip = 0xb7e1e637
 66  source language c.
 67  Arglist at 0xbfffef98, args: 
 68  Locals at 0xbfffef98, Previous frame's sp is 0xbfffefb0
 69  Saved registers:
 70   ebp at 0xbfffef98, eip at 0xbfffefac
 71 (gdb) c
 72 Continuing.
 73 
 74 Breakpoint 2, getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
 75 29        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
 76 (gdb) bt
 77 #0  getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
 78 #1  0x0804850d in main () at 1-9.c:16
 79 (gdb) frame 0
 80 #0  getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
 81 29        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
 82 (gdb) info locals
 83 c = 0
 84 i = 0
 85 (gdb) frame 1
 86 #1  0x0804850d in main () at 1-9.c:16
 87 16        while ((len = getlines(line, MAXLINE)) > 0)
 88 (gdb) info locals
 89 len = 0
 90 max = 0
 91 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>...
 92 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"...
 93 (gdb) frame 0
 94 #0  getlines (s=0xbfffe7bc "", lim=1000) at 1-9.c:29
 95 29        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
 96 (gdb) c
 97 Continuing.
 98 ab
 99 
100 Breakpoint 3, copy (to=0xbfffeba4 "P\345td\214P\026", from=0xbfffe7bc "ab\n") at 1-9.c:43
101 43        while ((to[i] = from[i]) != '\0')
102 (gdb) bt
103 #0  copy (to=0xbfffeba4 "P\345td\214P\026", from=0xbfffe7bc "ab\n") at 1-9.c:43
104 #1  0x080484f6 in main () at 1-9.c:19
105 (gdb) info locals
106 i = 0
107 (gdb) n
108 44            ++i;
109 (gdb) n
110 43        while ((to[i] = from[i]) != '\0')
111 (gdb) p to
112 $2 = 0xbfffeba4 "a\345td\214P\026"
113 (gdb) c
114 Continuing.
115 
116 Breakpoint 2, getlines (s=0xbfffe7bc "ab\n", lim=1000) at 1-9.c:29
117 29        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
118 (gdb) Quit

 

标签:gdb,000,29,GDB,初学,267,line,377
来源: https://www.cnblogs.com/lyrich/p/10500960.html