[Solved] GDB: "Unable to open 'malloc.c': File not found."

Hello. I’m trying to switch over to using GDB instead of using print statements everywhere in my code, but I’m running into problems. Most notably, GDB seems to not know what to do whenever an implicit loop is used when generating an array inside a function. Take the code below, for instance:

!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
! Main program:
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
program main
use :: iso_fortran_env, only: dp=>real64
implicit none

!++ Declare variables:
real(dp), dimension(11) :: arr

!++ Execution:
arr =  linspace(0._dp,2._dp,11)
print*, arr

contains
    !**********************************************************************************************************************************!
    function linspace(a,b,N) result(lin_arr)
    !! Equivalent to Numpy's numpy.linspace.
    !! Returns an array whose "N" elements are linearly spaced, from "a" to "b" (inclusive).
    !! The array's indices will be bounded in the region (0,N-1), so that the i-th element is simply
    !! x_i = a + dx*i,      i=0,1,...,N-1.
    !! dx = (b-a)/(N-1)

    !++ Declare variables:
    ! IN
    real(dp), intent(in) :: a,b ! Linspace limits.
    integer, intent(in) :: N ! Total array elements.

    ! INOUT/OUT
    real(dp),dimension(0:N-1) :: lin_arr

    ! Local
    integer :: i ! Loop index
    real(dp) :: dx ! Lattice spacing

    !++ Initialization:
    dx=(b-a)/(N-1)

    !++ Execution:
    ! lin_arr = a + dx*[0,1,2,...,N-1]
    lin_arr = a+dx*[(i, i=0,N-1)]

    end function 
    !**********************************************************************************************************************************!


end program

I’m using VS Code for graphical debugging. This program was compiled with the following flags:

gfortran -O0 -g -Wall -Wextra -pedantic -fcheck=all main.f90 -o main

When debugging, after stepping inside the linspace function, the debugger hangs upon reaching lin_arr = a+dx*[(i, i=0,N-1)], and VS Code accuses the following:

Unable to open 'malloc.c': File not found.

I tried to debug it manually, and had the same problem. In VS Code, this call stack is the one where the debugger hangs:

libc.so.6!__GI___libc_malloc(size_t bytes) (/build/glibc-sMfBJT/glibc-2.31/malloc/malloc.c:3023)
MAIN__::linspace(real(kind=8) (11) __result, real(kind=8) a, real(kind=8) b, integer(kind=4) n) ([insert_path_here]/main.f90:44)
MAIN__() ([insert_path_here]/main.f90:14)

Does anyone know what I’m doing wrong? After some time looking up the internet for answers, I noticed some StackOverflow threads commenting about “bad array allocation”. Is that the case?

All help is appreciated.

I tried to reproduce this using three different installations of gfortran on my Windows laptop (plain, Cygwin and MinGW), but failed, because gdb was unable to run - some message about not being able to read an XML-formatted library list. That, however, has nothing to do with the problem you reported.
When I tried this on Linux with just -g, it worked without any problem. With all the options you gave it was different - cannot resolve DW_OP_push_object_address. NO idea what went wrong.

Nevermind - it had nothing to do with the options. My guess is it is due to the internal function and setting a breakpoint in that. My first try was simply a breakpoint in the main program and step into linspace. So, in short: I cannot reproduce the problem you are experiencing.

Hi @Uendo, I am the dev of the VS Code Modern Fortran extension. I had a look at your code and I was unable to replicate the problem on my Linux machines.

gdb_ani

Could you provide some additional information mainly:

  1. gfortran version
  2. GDB version
  3. OS

I will attach a tasks.json and a launch.json script for you to use so that we are on the same page. Please let me know how you get on either here or in a private message.

tasks.json file
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "compile",
      "type": "shell",
      "command": "gfortran",
      "args": [
        "-Wall",
        "-g",
        "${file}",
        "-o${fileDirname}/${fileBasenameNoExtension}"
      ],
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared"
      },
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": "$gcc"
    }
  ]
}
launch.json
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch current F90",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "cwd": "${fileDirname}",
      "stopAtEntry": false,
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "compile",
      "logging": {
        "engineLogging": false,
        "moduleLoad": true,
        "exceptions": true
      }
    }
  ]
}

NOTE: If you are on Windows you will probably have to adjust the path character from / to \\

NOTE 2: I have removed most compile arguments for the sake of simplicity and debugging the issue

Hi, @Arjen. No problem, thank you for contributing.

Hi, @gnikit. I’m on Linux, I’ve tested your .json files and I still have the same issue. I could try to record it, is there a tool I can use to convert it to a gif, just like what you’ve done?

  1. GNU Fortran (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
  2. GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
  3. System:
    • Kernel: 5.13.0-39-generic x86_64 bits:
    • 64 compiler: N/A;
    • Desktop: Xfce 4.16.0; tk: Gtk 3.24.20; wm: xfwm4; dm: LightDM;
    • Distro: Linux Mint 20.3 Una; base: Ubuntu 20.04 focal

Note: I realized that, by simply stepping over during debugging (F10), I have the exact same outcome as yours. However, it is when I attempt to step into (F11) through the entire program that the call stack hangs at the lin_arr = a+dx*[(i, i=0,N-1)] line. Can you reproduce this?

Thank you for your help.

So to be clear are you attempting to step into the line lin_arr = a+dx*[(i, i=0,N-1)]?

Yes, basically.

I don’t think stepping into vs over the line should yield a different result. The only differences I noticed are the gfortran and GDB versions. Mine are (installed and updated from the ppa):

  • gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  • GNU gdb (GDB) 11.0.50.20201009-git

I would try and use a newer version of GDB and hopefully that fixes it! I vaguely recall having similar issues with GDB crashing a while back which got resolved when I upgraded.

OK, I’ll try to upgrade it.

I’ve downloaded GDB 11.2 from this link, but you said that you have installed it from “the ppa”, but I couldn’t find any ppa packages for GDB. Could you tell me what this ppa is? Do I also need to uninstall my current version of gdb beforehand?

Thanks.

Okay I stand corrected (as I mentioned it has been a while), I manually built and installed GDB because of my issues. The link you posted is correct, I think a ./configure && make -j && sudo make install wil work

1 Like

Thank you very much, now it works perfectly!

There were a few missing dependencies that took me quite a long time to figure out what they were, but I eventually managed to compile GDB 11.2. This video was very useful, and it might help anyone who wanders into this thread.

Could you help me verify just one last thing? When I place a breakpoint on the end program line in the test program I showed before, the button is greyed out when debugging. Is this expected to happen? It also occurred in GDB 9.2.0, so I’m guessing that’s the case.
Screenshot_2022-04-15_16-31-14

I think yes. That means that GDB cannot evaluate a breaking point for that location. Something similar will happen if you compile your binary without debug symbols -g and if you breakpoint a comment (i think).
Normally you would not expect your program to run to the end statement, it will have returned before that.

That’s great, I’ll keep it in mind.

@Uendo BTW I would make sure that you have a ~/.gdbinit file to load all the STL readers for C++ otherwise GDB is a nightmare to use

This is mine

# System-wide GDB initialization file.
python
import sys 
sys.path.insert(0, "/usr/share/gcc/python")
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

You need to look for the folder that ultimately contains printers.py. For me this is /usr/share/gcc/python/libstdcxx/v6/printers.py

Thank you once again, I know next to nothing about C/C++.

After some searches around the web, I came across this page from the GDB wiki, which contains the same info you just mentioned, but it seems to be horribly outdated. Is there anything else you’d recommend about learning GDB or will this wiki suffice?