gdb使用

`value optimized out`:编译时默认2级优化优化过度, 加上 -g0 选项不优化

在某个文件打断点: b mem_update.c:7881 

导入文件: dir wifi_database.c 

 watch的用法:

先看自己写的一个demon,先来看看源代码:

 1 /*************************************************************************
 2     > File Name: watch_test.c
 3     > Author: star
 4     > Mail: 2530617889@qq.com
 5     > Created Time: Mon 29 Jul 2019 09:00:49 AM CST
 6  ************************************************************************/
 7 
 8 #include <stdio.h>
 9 #include <stdlib.h>
10 #include <string.h>
11 int main(int argc, char const *argv[])
12 {
13     char *str = (char *)malloc(10);
14     memset(str, 0, 10);
15     strcpy(str,"123");
16     printf("%s\n", str);
17     strcpy(str,"abc");
18     printf("%s\n", str);
19     strcpy(str,"abd");
20     printf("%s\n", str);
21     return 0;
22 }

gdb调试:

[zyc@localhost ~]$ gdb ./watch_test
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 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/zyc/watch_test...done.
(gdb) start
Temporary breakpoint 1 at 0x4005cc: file watch_test.c, line 13.
Starting program: /home/zyc/./watch_test

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe588) at watch_test.c:13
13          char *str = (char *)malloc(10);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64
(gdb) p str
$1 = 0x0
(gdb) n
14          memset(str, 0, 10);
(gdb) p str
$2 = 0x602010 ""
(gdb) watch str        # watch指针本身。指针本身没有变(内存地址没变),所以watchpoint 2从来没有命中
Hardware watchpoint 2: str
(gdb) watch *str    # watch指针指向的内容,在后面会看到其实它是监控str[0]的内容
Hardware watchpoint 3: *str
(gdb) set print repeats 0    # 查看一个大的struct等大内存的时候,`print`出完整的内容
(gdb) c
Continuing.
Hardware watchpoint 3: *str

Old value = 0 '\000'
New value = 49 '1'
main (argc=1, argv=0x7fffffffe588) at watch_test.c:16
16          printf("%s\n", str);
(gdb) c
Continuing.
123
Hardware watchpoint 3: *str

Old value = 49 '1'        # 这里验证了watch一个char *,只是监控其第一个字符而已
New value = 97 'a'
main (argc=1, argv=0x7fffffffe588) at watch_test.c:18
18          printf("%s\n", str);
(gdb) c                # 这后面还有 str: "abc" => "abd",但没有命中
Continuing.
abc
abd

# 如果str采用数组的方式:char str[10] = {},会是不一样的情况,这一只简单举一个例子,有兴趣的话自己去验证:
# (gdb) c
# Continuing.
# abc
# Hardware watchpoint 2: str
# 
# Old value = "abc\000\000\000\000\000\000"
# New value = "abd\000\000\000\000\000\000"
# main (argc=1, argv=0x7fffffffe588) at watch_test.c:21
# 21          printf("%s\n", str);
# (gdb)

Watchpoint 2 deleted because the program has left the block in which its expression is valid.

Watchpoint 3 deleted because the program has left the block in which its expression is valid.
0x00007ffff7a39c05 in __libc_start_main () from /lib64/libc.so.6

 

posted on 2018-05-15 14:58  枝桠  阅读(194)  评论(0编辑  收藏  举报

导航