Debug Method
Analyze examples using bug
gdb debugging example
Add gdb debugging tool to Openwrt:
cd ssd2xx-openwrt/18.06
make menuconfig
#---Development
#------<*>gdb
examples code:
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
typedef struct
{
char *buffer;
int len;
}S_USER_DATE;
void core_generate_conf(void)
{
struct rlimit limit;
memset(&limit, 0, sizeof(limit));
limit.rlim_cur = RLIM_INFINITY;
limit.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &limit);
}
void data_printf(S_USER_DATE *pdata)
{
for(int i = 0; i < pdata->len; i++)
{
printf("input data[%d]: %d\n", i, pdata->buffer[i]);
}
}
void err_ctrl(S_USER_DATE *pdata)
{
pdata->buffer = NULL;
pdata->len = 255;
}
int main(int argc, char *argv[])
{
char data[5] = {1, 2, 3, 4, 5};
S_USER_DATE s_cache = {
.buffer = data,
.len = sizeof(data)
};
core_generate_conf();
err_ctrl(&s_cache);
data_printf(&s_cache);
return 0;
}
After running the above code, a debug file containing the core
keyword will be generated in the directory:
$gcc -g demo.c -o demo
$./demo
Segmentation fault (core dumped)
$ls
core-demo-82932-1652433788 demo demo.c
The core file will be debugged by using gdb:
$gdb -c core-demo-82932-1652433788 demo
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from demo...done.
[New LWP 82932]
warning: Unexpected size of section `.reg-xstate/82932' in core file.
Core was generated by `./demo'.
Program terminated with signal SIGSEGV, Segmentation fault.
warning: Unexpected size of section `.reg-xstate/82932' in core file.
#0 0x00000000004006af in data_printf (pdata=0x7ffce1df1370) at demo.c:25
25 printf("input data[%d]: %d\n", i, pdata->buffer[i]);
(gdb)
**Bug Tracking **
(gdb) bt #race function calls when errors occur
#0 0x00000000004006af in data_printf (pdata=0x7ffce1df1370) at demo.c:25
#1 0x000000000040075b in main (argc=1, argv=0x7ffce1df1478) at demo.c:45
(gdb) f 0 #f command (frame) selects stack frame 0 and then looks at local variables
#0 0x00000000004006af in data_printf (pdata=0x7ffce1df1370) at demo.c:25
25 printf("input data[%d]: %d\n", i, pdata->buffer[i]);
(gdb) p 0 #p command (print) prints the variable value
(gdb) p pdata
$1 = (S_USER_DATE *) 0x7ffce1df1370
(gdb) p pdata->buffer #It can be observed that a segmentation fault is caused on null pointer pdata->buffer
$2 = 0x0
**Breakpoint **
(gdb) b 44
Breakpoint 1 at 0x400743: file demo.c, line 44.
(gdb) b 45
Breakpoint 2 at 0x40074f: file demo.c, line 45.
(gdb) i b #Set a breakpoint at the function entry using s_cache
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400743 in main at demo.c:44
2 breakpoint keep y 0x000000000040074f in main at demo.c:45
(gdb) run
Starting program: /home/book/pro/gdb_debug/demo
Breakpoint 1, main (argc=1, argv=0x7fffffffddb8) at demo.c:44
44 err_ctrl(&s_cache);
(gdb) p s_cache
$1 = {buffer = 0x7fffffffdcc0 "\001\002\003\004\005\177", len = 5}
(gdb) n
Breakpoint 2, main (argc=1, argv=0x7fffffffddb8) at demo.c:45
45 data_printf(&s_cache);
(gdb) p s_cache
$2 = {buffer = 0x0, len = 255}
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x00000000004006af in data_printf (pdata=0x7fffffffdcb0) at demo.c:25
25 printf("input data[%d]: %d\n", i, pdata->buffer[i]);
(gdb)
Note: When debugging an application compiled with a Makefile, it should be noted that strip should be turned off during the compilation process. The strip command selectively removes line number information, relocation information, debug sections, typchk sections, comment sections, file headers, and all or part of the symbol table from an XCOFF object file.