c - 如何在 gdb 中打印 OpenMP 并行区域的局部变量?

标签 c debugging gdb openmp

我一直在调试 OpenMP 并行区域内的局部变量。

考虑到这段代码:

#include<omp.h>
#include<stdio.h>

int main(){
    int a = 0;
    #pragma omp parallel shared(a)
    {
    int m_a = a + omp_get_thread_num();
    printf("%d\n",m_a); //breakpoint set here
    }
}

代码运行良好,但如果我在带有注释的行设置断点并在 gdb 上键入 info locals,它只会显示 a 变量。键入 print m_a 会导致 No symbol "m_a"in current context。

最佳答案

这什么也没回答,只是报告我不能重复它,代码是用

编译的
gcc x.c -W -Wall -fopenmp -ggdb

和gdb session如下,一切正常,我猜可能是编译标志的问题,尝试在-g或-ggdb之外加上-O0

Reading symbols from ./a.out...done.
(gdb) break 9
Breakpoint 1 at 0x40064d: file x.c, line 9.
(gdb) run
Starting program: /some/path/to/a.out 
[Thread debugging using libthread_db enabled]
Thread 3 "a.out" hit Breakpoint 1, main._omp_fn.0 () at x.c:9
9           printf("%d\n",m_a); //breakpoint set here
(gdb) info locals
m_a = 2
a = 0
(gdb) info threads
  Id   Target Id         Frame 
  1    Thread 0x2aaaaaaebb40 (LWP 31287) "a.out" main._omp_fn.0 () at x.c:9
  2    Thread 0x2aaaabab0700 (LWP 31516) "a.out" main._omp_fn.0 () at x.c:9
* 3    Thread 0x2aaaabcb1700 (LWP 31517) "a.out" main._omp_fn.0 () at x.c:9
  4    Thread 0x2aaaabeb2700 (LWP 31518) "a.out" main._omp_fn.0 () at x.c:9
(gdb) thread 1
[Switching to thread 1 (Thread 0x2aaaaaaebb40 (LWP 31287))]
#0  main._omp_fn.0 () at x.c:9
9           printf("%d\n",m_a); //breakpoint set here
(gdb) info locals
m_a = 0
a = 0
(gdb) thread 2
[Switching to thread 2 (Thread 0x2aaaabab0700 (LWP 31516))]
#0  main._omp_fn.0 () at x.c:9
9           printf("%d\n",m_a); //breakpoint set here
(gdb) info locals
m_a = 1
a = 0
(gdb)

关于c - 如何在 gdb 中打印 OpenMP 并行区域的局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55128846/

相关文章:

java - Maven和SpringBoot调试-Drun.jvmArguments=-Xdebug到底是什么意思

c - 在 C 中打印所有全局变量和静态变量的值?

c - 使用 GDB 查找断言失败(Discoverying STM32 Microcontroller 一书)

c - 用于查找整数数组中最小值的程序(C 语言)无法编译

c - 程序正在代码块上运行,但不在黑客级别

c - sigchld 的语法及其声明

debugging - Fortran 变量具有不同的值

c - 如何从 C 命令行接收 arg

java - 远程调试 Java 应用程序

c++ - 如何使用 c++/python 程序在终端中运行程序,然后在该程序中执行语句