如何调试“程序接收信号SIGSEGV:分段错误”错误

All*_*ang 3 linux fortran gfortran netcdf

我正在运行 Fortran exe,但收到错误:

 set_nml_output Echo NML values to log file only
 Trying to open namelist log dart_log.nml
 PE 0: initialize_mpi_utilities:  Running with            8  MPI processes.

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用 gdb 来查找一些内容,它报告

[New LWP 9883]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Failed to read a valid object file image from memory.
Core was generated by `./filter'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00002af8e021390c in netcdf::nf90_open (
    path=<error reading variable: value requires 57959040 bytes, which is more than max-value-size>, mode=0, 
    ncid=<error reading variable: Cannot access memory at address 0x7ffe439346b0>, 
    chunksize=<error reading variable: Cannot access memory at address 0x0>, 
    cache_size=<error reading variable: Cannot access memory at address 0x7ffe43934530>, 
    cache_nelems=<error reading variable: Cannot access memory at address 0x7ffe43934528>, 
    cache_preemption=<error reading variable: Cannot access memory at address 0x7ffe439345a0>, 
---Type <return> to continue, or q <return> to quit---
    comm=<error reading variable: Cannot access memory at address 0x7ffe439345a8>, 
    info=<error reading variable: Cannot access memory at address 0x7ffe439345b0>, 
    _path=<error reading variable: Cannot access memory at address 0x7ffe439345b8>) at netcdf4_file.f90:39
39  netcdf4_file.f90: No such file or directory.
(gdb) bt
#0  0x00002af8e021390c in netcdf::nf90_open (
    path=<error reading variable: value requires 57959040 bytes, which is more than max-value-size>, mode=0, 
    ncid=<error reading variable: Cannot access memory at address 0x7ffe439346b0>, 
    chunksize=<error reading variable: Cannot access memory at address 0x0>, 
    cache_size=<error reading variable: Cannot access memory at address 0x7ffe43934530>, 
    cache_nelems=<error reading variable: Cannot access memory at address 0x7ffe43934528>, 
    cache_preemption=<error reading variable: Cannot access memory at address 0x7ffe439345a0>, 
    comm=<error reading variable: Cannot access memory at address 0x7ffe439345a8>, 
    info=<error reading variable: Cannot access memory at address 0x7ffe439345b0>, 
    _path=<error reading variable: Cannot access memory at address 0x7ffe439345b8>) at netcdf4_file.f90:39
Backtrace stopped: Cannot access memory at address 0x7ffe43934598
Run Code Online (Sandbox Code Playgroud)

netcdf4_file.f90:39 如下:

if (present(cache_size) .or. present(cache_nelems) .or. &
       present(cache_preemption)) then
     ret = nf_get_chunk_cache(size_in, nelems_in, preemption_in)
     if (ret .ne. nf90_noerr) then
        nf90_open = ret
        return
     end if
     if (present(cache_size)) then
        size_out = cache_size     #### line 39
     else
        size_out = size_in
     end if
     if (present(cache_nelems)) then
        nelems_out = cache_nelems
     else
        nelems_out = nelems_in
     end if
Run Code Online (Sandbox Code Playgroud)

问题是否与netcdf的版本有关,或者需要修改某些设置?

任何人都可以给我一些如何解决这个问题的建议,因为我对这些并不熟悉。提前致谢。

chw*_*w21 5

分段错误确实很难调试,但我做了以下几件事:

使用调试符号进行编译并进行运行时检查。这些标志取决于编译器,但以下是 gfortran 和 Intel Fortran 的标志:

gfortran     ifort         effect
------------------------------------------------------
-g           -g            Stores the code inside the binary
-O0          -O0           Disables optimisation
-fbacktrace  -traceback    More informative stack trace
-Wall        -warn all     Enable all compile time warnings
-fcheck=all  -check all    Enable run time checks
Run Code Online (Sandbox Code Playgroud)

如果运气好的话,当您的程序在以这种方式编译后崩溃时,将更容易推断出问题所在。