首页 > 代码库 > gdb 调试openmp多线程程序 示例介绍
gdb 调试openmp多线程程序 示例介绍
首先一个简单程序源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <omp.h> 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了。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。