34

I'm getting an an annoying error every time gdb catches an exception. I've run the following example program

#include <stdexcept>

int main() {
  throw std::invalid_argument("");
  return 0;
}

And the result from running gdb is

terminate called after throwing an instance of 'std::invalid_argument'
  what():  

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.

It's not all that bad, as I do get the information I need, it's just bugging me...

Do anyone know how to fix this?

ClausH
  • 341
  • 1
  • 3
  • 4
  • 1
    Use the package manager for your distro and search for that file? – UKMonkey Jan 16 '18 at 10:21
  • I did. It's in the gnulib package, but located in /usr/share/gnulib/lib/raise.c after installation. Gdb isn't finding it. – ClausH Jan 16 '18 at 10:23
  • 2
    There is nothing to fix here. Even if you fetch `raise.c` and will be able to see what is going on on that line it would be just a waste of time because it is not part of your code that actually caused an exception to be thrown. – user7860670 Jan 16 '18 at 10:23
  • 4
    I'll go a different route. You don't care. You don't need the source code of raise. Once raise() is called, you know an assert has failed or an exception was thrown. Print the callstack (`bt`) and move to the appropriate frame (`frame n`) to debug your code. – YSC Jan 16 '18 at 10:24
  • 6
    'it's just bugging me' no no no ... it's de-bugging you ;) – UKMonkey Jan 16 '18 at 10:26
  • What is your OS? – ks1322 Jan 16 '18 at 10:26
  • On Fedora you can install `glibc-debuginfo` package. – ks1322 Jan 16 '18 at 10:28
  • I'm on Ubuntu. I agree that it doesn't cause any actual problems, I'd still like to fix it, if possible. – ClausH Jan 16 '18 at 10:46

1 Answers1

46

To do full source code debugging of the C library on Ubuntu, there are just a few steps to take:

  1. Install the debuginfo version of libc6.

    It's probably already installed - the gdb package on Ubuntu has a dependency on it - but in case it isn't, run sudo apt install libc6-dbg.

  2. Prepare the package system to download and process source code packages, if this hasn't previously been done.

    sudo apt install dpkg-dev
    grep deb-src /etc/apt/sources.list
    

    Grep's output should show (and there may be additional matches that we don't need to worry about):

    deb-src http://archive.ubuntu.com/ubuntu/ bionic main restricted
    deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
    

    If the grep shows that these deb-src lines are commented out with #:

    # deb-src http://archive.ubuntu.com/ubuntu/ bionic main restricted
    # deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
    

    then edit /etc/apt/sources.list to remove the # at the beginning of these lines and then run sudo apt update .

  3. Download the source code corresponding to the installed version of the C library.

    First, create a directory anywhere - I'll use /opt/src here.

    Then do the following:

    cd /opt/src
    apt source libc6
    

    If the apt command gives an error message like

    E: You must put some 'source' URIs in your sources.list

    then my instructions in step 2 may have become outdated; post a comment here.

    When the download is complete, run this:

    find $PWD -maxdepth 1 -type d -name 'glibc*'
    

    Remember this name - it'll be something like /opt/src/glibc-2.23

  4. Determine where gdb expects to find the source code and make appropriate adjustments.

    Run gdb, have it run your program until it stops, and at the gdb prompt do this:

    (gdb) info source
    Current source file is ../sysdeps/unix/sysv/linux/raise.c
    Compilation directory is /build/glibc-KM3i_a/glibc-2.23/signal
    

    So gdb is expecting the source code to be in /build/glibc-KM3i_a/glibc-2.23 . There are two ways to fix this:

    • Move (or use a symlink) so that the source code is (or appears to be) in /build/glibc-KM3i_a/glibc-2.23 .

      or

    • Tell gdb how to substitute the correct source directory pathname:

      (gdb) set substitute-path /build/glibc-KM3i_a/glibc-2.23 /opt/src/glibc-2.23
      

    Now, go back to your frame, and gdb should show the source code line:

    (gdb) frame 1
    #1 0xb7e2fea9 in __GI_raise (sig=6) at ../sysdeps/unix/sysv/linux/raise.c:54
             return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
    
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
  • Thank you very much. I wont be back at the computer with the problem for another two weeks, but I look forward to seeing if this works. :) – ClausH Jan 22 '18 at 14:09
  • I'm getting the error in step 3: E: `You must put some 'deb-src' URIs in your sources.list` – migs647 Oct 13 '21 at 16:43
  • @migs647 I'll look into it. What release of Ubuntu are you using? – Mark Plotnick Oct 13 '21 at 21:24
  • @MarkPlotnick 20.04 :) – migs647 Oct 13 '21 at 23:32
  • @migs647 Step 2 should have worked on 20.04; the lines will have `focal` instead of `bionic`. If you do `grep ^deb-src /etc/apt/sources.list`, is there any output? – Mark Plotnick Oct 15 '21 at 14:33
  • @MarkPlotnick `deb-src http://us.archive.ubuntu.com/ubuntu/ disco main restricted deb-src http://us.archive.ubuntu.com/ubuntu/ disco-updates main restricted` The focal ones are uncommented, but don't show up when I execute the grep line. – migs647 Oct 15 '21 at 15:48
  • @migs647 OK, please check in an editor to see why there aren't any other lines with `deb-src` at the beginning. It looks like disco packages are no longer on that server; any errors when you run `apt update` ? – Mark Plotnick Oct 15 '21 at 16:25
  • @migs647 Actually, since you may have a Frankendistro, please run `apt-cache showpkg libc6 | more` and see which distro your libc came from. That will determine which deb-src lines you need. – Mark Plotnick Oct 18 '21 at 21:43
  • Instead of putting it the source code into `/opt/src/`, the directory `/usr/local/src/` is a viable alternative [conforming to the *FHS*](https://unix.stackexchange.com/a/10816/20230). – Abdull Aug 14 '23 at 08:04
  • [drgn's *Getting Debugging Symbols*](https://drgn.readthedocs.io/en/latest/getting_debugging_symbols.html) and [Michael Stapelberg's *Debugging experience in Debian (2019)*](https://michael.stapelberg.ch/posts/2019-02-15-debian-debugging-devex/) have further valuable tips for identifying the correct source file packages and setting them up for *gdb*. – Abdull Aug 14 '23 at 09:58