gdb 调试openmp多线程程序 示例介绍

 首先一个简单程序源代码:

  1 #include 
  2 #include 
  3 #include 
  4 int main(int argc, char * argv[]){
  5   int i, k,m,n;
  6   printf("i=%d,threads_num=%d\n",i, omp_get_num_threads());
  7   printf("i=%d,thread_id=%d\n",i, omp_get_thread_num());
  8 #pragma omp parallel for
  9 for(i=0;i<4;i++)
 10 {
 11    k=10;
 12    m=10;
 13    n=k+m;
 14    printf("i=%d,n=%d,thread_id=%d\n",i,n, omp_get_thread_num());
 15    if(k>1)
 16    printf("OK\n");
 17 }   
 18    return 1;
 19 }

编译成 test_omp

step 1:gdb test_omp

step 2:break 15

step 3:run

(gdb) r
Starting program: /home/xqc/gmptest/test_omp 
[Thread debugging using libthread_db enabled]
i=0,threads_num=1
i=0,thread_id=0
[New Thread 0x7ffff740f700 (LWP 17108)]
[New Thread 0x7ffff6c0e700 (LWP 17109)]
[New Thread 0x7ffff640d700 (LWP 17110)]

Breakpoint 1, main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15
15         k=10;
(gdb)
(gdb) info threads
  4 Thread 0x7ffff640d700 (LWP 17110)  main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15
  3 Thread 0x7ffff6c0e700 (LWP 17109)  main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15
  2 Thread 0x7ffff740f700 (LWP 17108)  main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15
* 1 Thread 0x7ffff7fdf780 (LWP 17105)  main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0) at test_omp.c:15
(gdb) 

step4: thread 2

(gdb) thread 2
[Switching to thread 2 (Thread 0x7ffff740f700 (LWP 17108))]#0  main.omp_fn.0 (.omp_data_i=0x7fffffffe3b0)
    at test_omp.c:15
15         k=10;
(gdb) 


至此,就可以对thread 2号线程进行debug了。


你可能感兴趣的:(linux)