GDB:如何在调试期间调用具有修改参数的函数

时间:2017-09-29 07:35:04

标签: debugging segmentation-fault fortran gdb gfortran

考虑以下简单的Fortran程序,它通过子程序添加两个整数并打印结果:

      PROGRAM MAIN

      INTEGER I, J, SUM

      I = 1
      J = 1

      CALL ADD(I, J, SUM)

      WRITE(*,*) SUM

      END

      SUBROUTINE ADD(I, J, SUM)

      INTEGER I, J, SUM

      SUM = I + J

      END

通过gfortran -g -O0 gdb-mwe.f -o gdb-mwe进行编译并在GNU调试器中运行,我希望在写入输出之前使用修改后的输入参数从调试器调用ADD。这是发生的事情:

Reading symbols from gdb-mwe...done.
(gdb) break 10
Breakpoint 1 at 0x4007dd: file gdb-mwe.f, line 10.
(gdb) r
Starting program: /home/username/Documents/Fortran/gdb-mwe 

Breakpoint 1, MAIN__ () at gdb-mwe.f:10
10        WRITE(*,*) SUM
(gdb) p j = j+1
$2 = 2
(gdb) call add(i,j,sum)

Program received signal SIGSEGV, Segmentation fault.
0x000000000040079a in add (
i=<error reading variable: Cannot access memory at address 0x1>, 
j=<error reading variable: Cannot access memory at address 0x2>, 
sum=<error reading variable: Cannot access memory at address 0x2>)
at gdb-mwe.f:18
18        SUM = I + J
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(add) will be abandoned.
When the function is done executing, GDB will silently stop.

我如何做到这一点?

1 个答案:

答案 0 :(得分:0)

正如评论中指出的那样,gdb中的open bugs目前阻止这样做。

一个可能的解决方法是调试32位版本的代码。这会导致一些差异,但是对于简单的调试任务来说可能就足够了。

对于intel fortran编译器,这仅需要添加-m32标志(已安装了提供的32位库)。

对于gfortran,似乎必须先安装multilib软件包,如this questions所示。