2. 设置断点调试,在实际开发中有什么作用?
16 //================用户传入方法======================
17 void active_func()
19 printf("%s\n", __func__);
20 printf("go to home\n");
(gdb)
21 sleep(100);
24 void pthread_handler(void *value)
26 char *name = (char *)value;
28 if (0 == strcmp("cat", name))
29 animal_active_handle(name, active_func);
(gdb) b 21
Breakpoint 1 at 0x12aa: file test.c, line 21.
(gdb) run
Starting program: /home/cjj/gdb-test/test_out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7d96700 (LWP 40777)]
#########name=cat##########
active_func
go to home
[New Thread 0x7ffff7595700 (LWP 40778)]
[140737343215360]dog
[New Thread 0x7ffff6d94700 (LWP 40779)]
[Switching to Thread 0x7ffff7d96700 (LWP 40777)]
Thread 2 "test_out" hit Breakpoint 1, active_func () at test.c:21
21 sleep(100);
(gdb) bt
#0 active_func () at test.c:21
#1 0x000055555555527b in animal_active_handle (name=0x555555556041 "cat",
func=0x55555555528a <active_func>) at test.c:12
#2 0x00005555555552f9 in pthread_handler (value=0x555555556041) at test.c:29
#3 0x00007ffff7f94609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#4 0x00007ffff7eb9163 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00005555555552aa in active_func at test.c:21
breakpoint already hit 1 time
(gdb) info frame
Stack level 0, frame at 0x7ffff7d95eb0:
rip = 0x5555555552aa in active_func (test.c:21); saved rip = 0x55555555527b
called by frame at 0x7ffff7d95ed0
source language c.
Arglist at 0x7ffff7d95e98, args:
Locals at 0x7ffff7d95e98, Previous frame's sp is 0x7ffff7d95eb0
Saved registers:
rbp at 0x7ffff7d95ea0, rip at 0x7ffff7d95ea8
(gdb) info threads
Id Target Id Frame
1 Thread 0x7ffff7d97740 (LWP 40773) "test_out" clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:78
* 2 Thread 0x7ffff7d96700 (LWP 40777) "test_out" active_func () at test.c:21
3 Thread 0x7ffff7595700 (LWP 40778) "test_out" 0x00007ffff7e7726f in __GI___clock_nanosleep (
clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x7ffff7594e90,
rem=rem@entry=0x7ffff7594e90) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:78
4 Thread 0x7ffff6d94700 (LWP 40779) "test_out" clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:78
(gdb)
注:当代码量比较大,并且代码的调用比较复杂时候,可以在某个函数内添加断点,然后根据bt指令打印出函数调用的堆栈信息,可以清晰的知道这个函数是被谁调用的,方便分析函数的调用关系