[置顶] gdb调试

1. 使main程序含参数

>> b main

>> r "argv[1]" "argv[2]"


2.调试子进程

>> set follow-fork-mode child


3. 帮助命令

>> help

List of classes of commands:


aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands


Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

然后可以从中选出subclass,

>>help running

......

>>help thread

>>thread


========================================================================================================================

GDB打印void*类型变量

由于path.etls的的类型是void*类型,所以不能直接打印出来,那么则强制转换一下,赋值给另一个变量,再把另一个变量值打印出来。

(gdb) set $path=((ngx_path_t     **)ngx_cycle->paths.elts)[2]
(gdb) print *$path
$16 = {
  name = {
    len = 29,
    data = 0x80ed15c "/usr/local/nginx/fastcgi_temp"
  },
  len = 5,
  level = {1, 2, 0},
  manager = 0,
  loader = 0,
  data = 0x0,
  conf_file = 0x0,
  line = 0
}
(gdb)

=============================================================================================================================

使用gdb加载glibc代码方法directory

详细介绍:https://sourceware.org/gdb/onlinedocs/gdb/Source-Path.html

root@ubuntu:/tmp# gdb ./a.out

(gdb) directory /tmp/eglibc-2.15/stdio-common

(gdb) l
25
26      /* Write formatted output to stdout from the format string FORMAT.  */
27      /* VARARGS1 */
28      int
29      __printf (const char *format, ...)

30      {
31        va_list arg;
32        int done;
33
34        va_start (arg, format);

=============================================================================================================================

如果想调shared lib,则断点应该在session开始之前下,如果在程序运行中下断点,则不好用。

It's quite common to have a breakpoint inside a shared library. Shared libraries can be loaded and unloaded explicitly, and possibly repeatedly, as the program is executed. To support this use case, gdb updates breakpoint locations whenever any shared library is loaded or unloaded. Typically, you would set a breakpoint in a shared library at the beginning of your debugging session, when the library is not loaded, and when the symbols from the library are not available. When you try to set breakpoint, gdb will ask you if you want to set a so called pending breakpoint—breakpoint whose address is not yet resolved.

set breakpoint pending auto
This is the default behavior. When  gdb cannot find the breakpoint location, it queries you whether a pending breakpoint should be created. 
set breakpoint pending on
This indicates that an unrecognized breakpoint location should automatically result in a pending breakpoint being created. 
set breakpoint pending off
This indicates that pending breakpoints are not to be created. Any unrecognized breakpoint location results in an error. This setting does not affect any pending breakpoints previously created. 
show breakpoint pending
Show the current behavior setting for creating pending breakpoints.

=============================================================================================================================

you can set gdb to use Intel-style disassembly by issuing the command:

set disassembly-flavor intel
ref:  http://en.wikibooks.org/wiki/X86_Assembly/NASM_Syntax

=============================================================================================================================

>>b *main

可以用于在函数入口处下断点,与 b main是有区别的。 b main是在prolog之后停,而b *main在prolog之前停

=============================================================================================================================

To find out what does it looks like in assembly we compile it, and start
up gdb.  Remember to use the -static flag. Otherwise the actual code the
for the execve system call will not be included.  Instead there will be a
reference to dynamic C library that would normally would be linked in at
load time

=============================================================================================================================

gdb在指定内存出下断点有三种方式:

watch:Set a watchpoint for an expression. gdb will break when the expression expr is written into by the program and its value changes,写中断。

rwatch:rwatch let you break on read,读中断。

awatch:awatch let you break on read/write,访问中断。

如果要断电指定内存,则:

watch *0x8049630


Currently, the awatch and rwatch commands can only set hardware watchpoints, because accesses to data that don't change the value of the watched expression cannot be detected without examining every instruction as it is being executed, and gdb does not do that currently. If gdb finds that it is unable to set a hardware breakpoint with the awatch or rwatch command, it will print a message like this:

=============================================================================================================================

gdb获取数据类型:pd为变量名

(gdb) whatis pd
type = struct pthread *


gdb获取类型定义:pd为变量名

(gdb) ptype pd
type = struct pthread {
    union {
        tcbhead_t header;
        void *__padding[24];
    };
    list_t list;
    pid_t tid;
    pid_t pid;
    union {
        __pthread_slist_t robust_list;
        struct robust_list_head robust_head;
    };
    struct _pthread_cleanup_buffer *cleanup;
    struct pthread_unwind_buf *cleanup_jmp_buf;
    int cancelhandling;
    int flags;
    struct pthread_key_data specific_1stblock[32];
    struct pthread_key_data *specific[32];
    _Bool specific_used;
    _Bool report_events;
    _Bool user_stack;
    _Bool stopped_start;
    int parent_cancelhandling;
    int lock;
    int setxid_futex;
    hp_timing_t cpuclock_offset;
    struct pthread 

........


=============================================================================================================================

用于将程序运行到指定的frame

frame n
f n
Select frame number  n. Recall that frame zero is the innermost (currently executing) frame, frame one is the frame that called the innermost one, and so on. The highest-numbered frame is the one for  main.

up n
Move  n frames up the stack. For positive numbers  n, this advances toward the outermost frame, to higher frame numbers, to frames that have existed longer.  n defaults to one.
down n
Move  n frames down the stack. For positive numbers  n, this advances toward the innermost frame, to lower frame numbers, to frames that were created more recently.  n defaults to one. You may abbreviate  down as

=============================================================================================================================

info source
Show the name of the current source file--that is, the source file for the function containing the current point of execution--and the language it was written in.
info sources

Print the names of all source files in your program for which there is debugging information, organized into two lists: files whose symbols have already been read, and files whose symbols will be read when needed.



你可能感兴趣的:([置顶] gdb调试)