`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 ", str); 17 strcpy(str,"abc"); 18 printf("%s ", str); 19 strcpy(str,"abd"); 20 printf("%s ", 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 '