How to use GDB Debugger on a macOS? - c

I am currently using a mid 2012 macbook Pro that is running macOS Mojave version 10.14.5. I am trying to use the GDB debugger for debugging C language code. I've already used Homebrew to install gdb on my computer, however it does not seem to work. Here is the output that I get from the terminal in VSCode:
steelwinds-MacBook-Pro:Chapter05 steelwind$ gdb ex2_dbg.out
GNU gdb (GDB) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin18.7.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
--Type <RET> for more, q to quit, c to continue without paging--<RET>
Type "apropos word" to search for commands related to "word"...
Reading symbols from ex2_dbg.out...
Reading symbols from
/Users/steelwind/HardWay/CandC++/CandC++/Chapter05/ex2_dbg.out.dSYM/Contents/Resources/
DWARF/ex2_dbg.out...
(gdb) run
Starting program: /Users/steelwind/HardWay/CandC++/CandC++/Chapter05/ex2_dbg.out
Unable to find Mach task port for process-id 7502: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
(gdb) q
steelwinds-MacBook-Pro:Chapter05 steelwind$
I'm not sure if I missed a step or if there is something that I should do instead of using Homebrew, but any advice is helpful.

The hint is this line: please check gdb is codesigned - see taskgated(8)
Because of one of Apple's security features, you will need to codesign GDB. Here's how to do that: "please check gdb is codesigned - see taskgated(8)" - How to get gdb installed with homebrew code signed?

Related

Visual Studio Code and Cygwin is not debugging. And have a problem with gdb

I just started coding(?), I think... I was setting up Visual Studio Code for C programming and I was using Cygwin.
I'm writing a single Hello World print code. Yes, I can compile it with Ctrl+Shift+B. But when I tried the debugging with F5, it's giving this error down there;
GNU gdb (GDB) (Cygwin 9.2-1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Error creating process /cygdrive/c/Users/EM&ES BİLGİSAYAR/cworkings/fckworld/fckworld.exe, (error 193).
The program 'c:\Users\EM&ES BİLGİSAYAR\cworkings\fckworld\fckworld.exe' has exited with code 42 (0x0000002a).
I was using gdb v10 but after 6 hours of researching on the internet, I downgraded it to V9. Nothing is changed except "error 2" to "error 193".

gdb core dump "is not a core dump: File format not recognized"

I compiled my C file test with the flags -g -Wall -std=gnu99 -pedantic
I ran it and a core file was created
I tried analysing it gdb test core and I get this error message
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
"/[...]/aula02/core" is not a core dump: File format not recognized
I was using a VirtualMachine and executing files that were on a macOS directory, when doing it in the VM Desktop it works fine

Abort trap 6: when running gdb on Mac OS

I have installed gdb on my Mac (with Mac OS 10.9 / Mavericks) via homebrew and codesigned it successfully.
Before starting a debug session with gdb I compiled my C-files like in this example:
gcc -g test.c -o test
Afterwards I called gdb:
Users-MacBook-Pro:Test User$ gdb ./test
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin13.4.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...Reading symbols from /Users/User/Test/test.dSYM/Contents/Resources/DWARF/test...done.
done.
Calling gdb commands like r or sta always results in the following error:
(gdb) r
Abort trap: 6
What is the problem? Any ideas how to fix it?
I'd suggest rebuilding gdb from the scratch. Instead of using some pre-built installations, you can always try to install it by yourself:
mkdir src
cd src
curl "http://ftp.gnu.org/gnu/gdb/gdb-8.0.tar.gz" -o gdb-8.0.tar.gz
tar zxf gdb-8.0.tar.gz
cd gdb-8.0
./configure --prefix=$HOME/opt/usr/local
make
make install
Remember to sign it before you can use it: https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gnat_ugn_unw/Codesigning-the-Debugger.html
This way, you can easily debug your code. For details of installation, take a look here: Running GDB in macOS sierra

default linker script for debugging information

I am trying to use gdb for learning debugging. When I compile the following code using no options, gdb points out that no debugging symbols were found.
int main()
{
return 0;
}
$gcc debug.c
$gdb a.out
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...(no debugging symbols found)...done.
(gdb) quit
But when I give the -g flag with gcc, following output is seen:
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) Quit
(gdb) quit
How do I figure out what is wrong and see if the linker has enabled stripping of the debugging information somewhere?
There's no stripping involved here, since there's no debug inclusion in the first place.
-g option is required to enable debug information.
By default (without any option), debug information is not embedded in the object files
So the correct way is: enable -g to tell the compiler to embed debug symbols, else they're not included and gdb does not find them.
(yes, another way to avoid the debugger to find them is to pass -Wl,--strip-debug to the linker, but that's another story)
But when I give the -g flag with gcc, following output is seen:
Reading symbols from a.out...done.
That's the output you get when gdb reads the debug symbols without errors. It succeeded so at that point the debugger is ready for you to tell it where to put breakpoints and to run the program being debugged.

GDB Cannot Set a Breakpoint on Shared Library

I am trying to debug a program which loads a shared library. I can debug the main program parts perfectly but in the shared library, I run into some problems.
gdb -p 70876
GNU gdb (GDB) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin15.5.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 70876
Reading symbols from /usr/local/pgsql/bin/postgres...done.
0x00007fff9496a2a2 in poll () from /usr/lib/system/libsystem_kernel.dylib
(gdb) b multi_ProcessUtility
Breakpoint 1 at 0xdaa9: multi_ProcessUtility. (2 locations)
(gdb) b multi_utility.c:129
Cannot access memory at address 0xdaa9
(gdb)
So, the problem is that, I can set a breakpoint with the method name itself and the program stops at the specified point. However, I cannot set the breakpoint with the filename and line number. (The specified filename and line number also addresses the same function)
When the program stops in the shared library function, running info source command result with No current source file. message
At the time I have attached to the process, the shared library is loaded and running.
I am on Mac OS X El Capitan, using GNU GCC 6 and GDB 7.11.1 from homebrew. I have compiled both the main program and the shared library with "-Og -ggdb -g3" flags.
I have solved my problem by downgrading to gdb 6.3.50.
Basically, I have installed gdb-apple from macports and it is based on gdb 6.3.50. I don't know whether it is related to gdb version or gdb-apple port has some tweaks that make it work.

Resources