Gdb Command Abbreviation command Description
gdb ./binary_name   start gdb 
run command_line r command_line Start the program being debugged, possibly with command line arguments args. [run]
break function b function Set a breakpoint at the beginning of function [break]
break filename:linenumber
b filename:linenumber Set a breakpoint at line number of the current file. [break]
delete n   Delete breakpoint number n [delete]
info break info b List all breakpoints [info]
list [optional_line]  l [optional_line] List next listsize lines. If optional_line is given, list the lines centered around optional_line. [list]
backtrace bt print callstack
frame number f number jump to caller functions
print var_name[expression] p var_name[expression] print variable, expression(any kind of combinations of variables)
continue c run until next breakpoint
next n Step over step by step
step s Step into functions. [step]
finish   Step out of the current function. Execute the rest of the current function. [finish]
watch var_name[*(int*)add]   Set a watchpoint for an expression. gdb will break when the expression expr is written into by the program and its value changes.
watch -l var_name   Set a watchpoint for an expression. gdb will break when the expression expr is written into by the program and its value changes.
rwatch  [-l] var_name   Set a watchpoint that will break when the value of expr is read by the program.
awatch  [-l] var_name   Set a watchpoint that will break when expr is either read from or written into by the program.
q   quit gdb [quit]

An example

Here is an example code. This example code store two strings together. The memory layout is: [str1_len, str1, str2_len, str2]. str2_len locates in the middle of the memory. It's possible that str1 is longer than the max string size( g_str_max_size), then str2 will overrite parts of str1.

 1 #include "stdio.h"
 2 #include "stdlib.h"
 3 #include "string.h"
 4 #include "assert.h"
 5 const int g_str_max_size = 10;
 6 void store_string(void *mem, char *str)
 7 {
 8     int len = strlen(str);
 9     *(int*)mem = len;
10     strcpy(mem + sizeof(int), str);
11 }
12 void print_strings(void *mem)
13 {
14     printf("Length of string a:  %d\n", *(int*) mem);
15     printf("string s: %s\n\n", (char*)(mem + sizeof(int)));
16     
17     printf("Length of string b:  %d\n", *(int*) (mem + (g_str_max_size * sizeof(char) + sizeof(int))));
18     printf("string s: %s\n", (char*)(mem + (g_str_max_size * sizeof(char) + sizeof(int) * 2)));
19 }
20 int main(int argc, char *argv[])
21 {
22     if(argc != 3)
23     {
24         printf("usage: ./two_strings string1 string2\n");
25         return 0;
26     }
27     
28     void *mem = malloc(2 * (g_str_max_size * sizeof(char) + sizeof(int)));
29     memset(mem, 0, 2 * (g_str_max_size * sizeof(char) + sizeof(int)));
30     
31     // char *str1 = "string b.";
32     // char *str2 = "This is string a.";
33     // assert(strlen(str1) < g_str_max_size);
34     // assert(strlen(str2) < g_str_max_size);
35     
36     // strlen1 --> *(int*) mem + sizeof(int)
37     // strlen2 --> *(int*)(mem + (g_str_max_size * sizeof(char) + 2 * sizeof(int)))
38     store_string(mem                                                , argv[1]);
39     store_string(mem + (g_str_max_size * sizeof(char) + sizeof(int)), argv[2]);
40     
41     print_strings(mem);
42     
43     return 0;
44 }
View Code

相关文章: