(gdb) break 24 Breakpoint 1 at 0x11ec: file tst.c, line 24. (gdb) r Starting program: /home/ubuntu/learn_gdb/tst
Breakpoint 1, main (argc=1, argv=0x7fffffffe148) at tst.c:24 24 long result = 0; (gdb) display i 1: i = 21845 (gdb) n 25 for (i = 1; i <= 100; i++) 1: i = 21845 (gdb) n 27 result += i; 1: i = 1 (gdb) n 25 for (i = 1; i <= 100; i++) 1: i = 1 (gdb) n 27 result += i; 1: i = 2 (gdb) n 25 for (i = 1; i <= 100; i++) 1: i = 2 (gdb) n 27 result += i; 1: i = 3 (gdb) n 25 for (i = 1; i <= 100; i++) 1: i = 3 (gdb) n 27 result += i; 1: i = 4 (gdb) n 25 for (i = 1; i <= 100; i++) 1: i = 4
(gdb) break 26 Breakpoint 1 at 0x1269: file tst.c, line 26. (gdb) r Starting program: /home/ubuntu/learn_gdb/tst
Breakpoint 1, main (argc=1, argv=0x7fffffffe148) at tst.c:26 26 for(int k=0;k<10;k++) (gdb) p *arr $1 = 0 (gdb) p arr $2 = {0, 11, 22, 33, 44, 55, 66, 77, 88, 99} (gdb) p j $3 = 3 (gdb) p i $4 = -8137 (gdb) p result $5 = 0 (gdb) p argc $6 = 1 (gdb) show convenience $bpnum = 1 $_gdb_minor = 2 $_gdb_major = 9 $_as_string = <internal function _as_string> $_regex = <internal function _regex> $_streq = <internal function _streq> $_strlen = <internal function _strlen> $_memeq = <internal function _memeq> $_any_caller_matches = <internal function _any_caller_matches> $_any_caller_is = <internal function _any_caller_is> $_caller_matches = <internal function _caller_matches> $_caller_is = <internal function _caller_is> --Type <RET> for more, q to quit, c to continue without paging--q Quit (gdb) p $1 $7 = 0 (gdb) set $1 = 11 Left operand of assignment is not a modifiable lvalue. (gdb) p $1 $8 = 0 (gdb) c Continuing. result[1-100] = 5050 result[1-250] = 31125 [Inferior 1 (process 3039503) exited normally] (gdb) p i No symbol "i" in current context. (gdb) p $4 $9 = -8137
上面的实验说明,单纯的就是历史记录,而且该历史记录无法被修改(上面尝试赋值,但是提示Left operand of assignment is not a modifiable lvalue.),就算当初print的变量已经被销毁了,这条历史记录还是存在的,并且可以被打印出来。
九、GDB环境变量
你可以在GDB的调试环境中定义自己的变量,用来保存一些调试程序中的运行数据。要定义一个GDB的变量很简单只需。使用GDB的set命令。GDB的环境变量和UNIX一样,也是以$起头。如:
set $foo = *object_ptr
使用环境变量时,GDB会在你第一次使用时创建这个变量,而在以后的使用中,则直接对其賦值。环境变量没有类型,你可以给环境变量定义任一的类型。包括结构体和数组。
(gdb) set $k = 0 (gdb) break 21 Breakpoint 1 at 0x124c: file tst.c, line 21. (gdb) r Starting program: /home/ubuntu/learn_gdb/tst
Breakpoint 1, main (argc=1, argv=0x7fffffffe148) at tst.c:21 21 j = 1; (gdb) print arr[$k++] $1 = 0 (gdb) $2 = 11 (gdb) $3 = 22 (gdb) $4 = 33 (gdb) $5 = 44 (gdb) $6 = 55 (gdb) $7 = 66 (gdb) $8 = 77 (gdb) $9 = 88 (gdb) $10 = 99 (gdb) show convenience $bpnum = 1 $k = 10 $_gdb_minor = 2 $_gdb_major = 9 $_as_string = <internal function _as_string> $_regex = <internal function _regex> $_streq = <internal function _streq> $_strlen = <internal function _strlen> $_memeq = <internal function _memeq> $_any_caller_matches = <internal function _any_caller_matches> $_any_caller_is = <internal function _any_caller_is> $_caller_matches = <internal function _caller_matches> --Type <RET> for more, q to quit, c to continue without paging--
Lab2
之前是对环境变量传了一个值,那么如果对环境变量传了一个指针会怎么样?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(gdb) break 20 Breakpoint 1 at 0x1245: file tst.c, line 20. (gdb) r Starting program: /home/ubuntu/learn_gdb/tst
Breakpoint 1, main (argc=1, argv=0x7fffffffe148) at tst.c:20 20 int j=0; (gdb) set $ptr = arr (gdb) show convenience $ptr = (int *) 0x7fffffffe020 $bpnum = 1 $_gdb_minor = 2 $_gdb_major = 9 ... ... (gdb) print *($num) Attempt to take contents of a non-pointer value.