关于使用gdb的一些建议

来源:互联网 发布:人工智能伏羲觉醒2电影 编辑:程序博客网 时间:2024/06/05 02:57

参考:《Linux® Debugging and Performance Tuning: Tips and Techniques》 chapter 3

1. 测试程序

/home/a/j/nomad2:cat gdb_sample2.c #include <stdio.h>#include <pthread.h>#include <unistd.h>void* Thread_function1(void * arg){        unsigned int i=1;        while(i < 11)        {                printf("Child Thread Iteration %d\n",i);                i++;                if(i%2)                        sleep(2);                else                        sleep(1);        }        return arg;}void* Thread_function2(void * arg){        unsigned int i=1;        while(i < 11)        {                printf("Child Thread 2 Iteration %d\n",i);                i++;                if(i%2)                        sleep(2);                else                        sleep(1);        }        return arg;}int main (int argc, char *argv[]){        pthread_t thread;        pthread_t thread2;        if(pthread_create(&thread,NULL,Thread_function1,NULL))        {                return(1);        }        if(pthread_create(&thread2,NULL,Thread_function2,NULL))        {                return(1);        }        unsigned int i = 1;        while(i < 11)        {                printf("Main Loop Iteration %d\n",i);                i++;                if(i%2)                        sleep(1);                else                        sleep(2);        }        return 0;}

2. build with debug info increase the executable size.

/home/a/j/nomad2:ls -lrt a.out
-rwxr-xr-x 1 nomad2 member 11341 Oct 1 19:34 a.out
/home/a/j/nomad2:strip --strip-debug a.out
/home/a/j/nomad2:ls -lrt a.out
-rwxr-xr-x 1 nomad2 member 7522 Oct 1 19:42 a.out

3.  关于调试:
1). When debugging, it is often a good practice to stop program execution at the bottom of a function so that a print or multiple displays can be done to see the current values stored in the data the function has altered. 
list       <func name>
break  <line of }>

2). When gdb is used, the print command must

be invoked to display the variable's value. The user can see much more information at one time.

原创粉丝点击