How to save settings in gdb? - c++

Does anyone know how to save gdb settings (like "set print pretty on" or "set print elements 0", both from here)? I don't want to set my configuration every time that I will use gdb :/
I searched in google and SO, but I found nothing.

Add any commands you want to auto run in the .gdbinit file in your home directory.

The existing answer works for commands that can run before the binary is loaded, but for example if you want to add catch throw you cannot do it in .gdbinit since that command needs to run after the binary is loaded.
But gdb can take a file with commands to run after binary loading using:
-x file
Execute GDB commands from file file.
I automated that by creating an alias:
alias gdb='gdb -x ~/.gdbinit_x'
and added my after binary load commands in that file.

Related

GDB exec Error: No such file or directory

I have an executable, which I can read symbols from (so it seems.) My problem is this: when it comes time to run, I get the following error:
(gdb) run
Starting program: /home/usr/src/etcetera/etcetera/bin/theExecutable.exe
Cannot exec -c exec /home/usr/src/etcetera/etcetera/bin/theExecutable.exe.
Error: No such file or directory
During startup program exited with code 127
obviously, I have edited the directories here. I searched how to fix this on SO, and tried some of the following solutions:
GDB cannot see source file
GDB can't find source file
GDB won't load source file
got onto this link:
https://sourceware.org/gdb/download/onlinedocs/gdb/Source-Path.html#Source-Path
and am trying to change the source file directory. (The source files are not in the same location as the executable, but instead are spread over a range of different places.) Unless I am mistaken, the way of doing this is to go:
(gdb) directory /home/usr/src/etcetera/etcetera/rootDirectoryForSourcefiles
and have the GDB search this directory. I have even tried changing directory into the source directory, and then running but still, it wants to try where the executable lives.
Am I completely missing the mark here in an obvious way, or is this likely to be quite obscure?
You are barking up the wrong tree. You problem has ~nothing to do with source files, and everything to do with your executable file.
It may be related to something in your ~/.gdbinit, or your ~/.bashrc, or the way you invoked GDB.
You should start by doing a basic sanity check:
env SHELL=/bin/sh gdb -nx /bin/date
(gdb) run
If that doesn't work, your GDB installation is screwed up.
If that does work, one of the three things I mentioned above is very likely the cause of your troubles.
I had this problem and it turned out that the shell wasn't set correctly in the /etc/passwd file.
To solve it, I opened the file with
sudo vipw
and added /bin/bash to the my account's data there.
Try to:
export SHELL=/bin/sh
before running gdb
I had met same problem. When my
SHELL=/usr/local/bin/tcsh
but I have only the file .cshrc, gdb reports the same error.
When I change SHELL:
setenv SHELL /bin/csh
Then everything goes fine.

Default settings for objdump?

So I'm still new to disassembling and I've got a couple of problems that would help being taken care of.
I'm following along the book "Art of Exploitation". The author prefers using Intel syntax in 32bit mode when he objdumps and since I want to follow along I want to configure my syntax the same. Since my Ubuntu uses AT&T 64bit mode by default, I have to input optional arguments each time I objdump like:
$ objdump -M i386,intel -D a.out
Is there a way to change the default settings for objdump?
I also want to change the gdb syntax to intel so I looked for .gdbinit but couldn't find it. Instead I made a new one in the home directory by doing this:
$ echo "set disassembly-flavor intel" > ~/.gdbinit
but didn't seem to work.
Can anyone tell me where the environment variables (or environment variable config files) for objdump and gdbinit are?
Here are my solutions to both of these questions:
To set the "-M intel" option every time for objdump, I created an alias. To create an alias, find your .bashrc file and edit it. The file should be located in your home directory, if you haven't moved it. The line that I chose to add is included below.
alias odi='objdump -M intel -d'
This alias also always sets the disassemble option and shortens the command. I didn't want to replace the regular objdump command, but 90% of the time when using it, I'm just using it for quick disassembly. Make sure you restart bash before testing any new aliases.
As for gdb, what you attempted should have worked. All that you need to do is make a file, named .gdbinit, in your home directory and include the following line in it:
set disassembly-flavor intel
You must have made a mistake somewhere else.

(VS2010 C++) Execute a command every time the program is run?

The IDE I'm using is VS2010 for writing C++
I want to execute the command cmd C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt every time the program I'm coding is run from within the IDE. (This command should open a console to track changes made to the file Log.txt)
There are ways to make a command run every time the program is built, but I can't find a way to make a command run whenever the program itself is run, even if it's already built. Where or how might I be able to set that kind of thing up?
I've tried putting $(TargetPath) & C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt into the project's Properties->Debugging->Command (TargetPath is the full name of the debug executable) but VS reads the entire thing as a filename and gets confused.
You can create a file run.cmd for example next to the vcxproj file, which would contain:
%1
C:\utilities\unix\tail.exe -f -n15 %2Log.txt
And then in Properties->Debugging->Command you write:
$(ProjectDir)\run.cmd
and in Command Arguments you write:
"$(TargetPath)" "$(ProjectDir)"
I may have misspelled the macros, but you get the idea: it executes first your program and then whatever you want.
Edit: Unfortunately it works only if you start without debugging (Ctrl+F5), because otherwise the debugger tries to attach to run.cmd and complains that the format is unsupported.

How GDB startup files work?

How do I save my current GDB session? and How do I load it again on GDB startup later. There is a brief discussion on .gdbinit in Art of debugging , Chapter 1. But I really don't get the saving part. Is it an autosave?
The .gdbinit file saves some configurations and user-defined commands. When gdb starts, it will try to search .gdbinit file according to some sequences (For the sequences, you can refer https://sourceware.org/gdb/current/onlinedocs/gdb/Mode-Options.html#Mode-Options). For .gdbinit file, you can refer this as an example:https://github.com/gdbinit/Gdbinit/blob/master/gdbinit.
I think you want to store the whole memory dump into the file, then restart gdb, it will reload it. I have search the invoking gdb manual(https://sourceware.org/gdb/current/onlinedocs/gdb/Invoking-GDB.html#Invoking-GDB), but can't find gdb can do it.
Personally, I think this file you request is very similar to core dump file.

Change NetBeans Debugger Command

I have a .gdbinit file to set the solib-absolute-prefix and solib-search-path when attaching a debugger from NetBeans. I've turned on the debugger console (by adding -J-Dgdb.console.window=true to netbeans_default_options in /usr/share/netbeans/6.9/etc/netbeans.conf) and it tells me:
Debugger Command: [/path/to/gdb, -nx, --nw, --silent, --interpreter=mi]
The -nx is preventing it from loading my .gdbinit file.
Is there any way in NetBeans to either (1) remove -nx from that command, (2) specify a .gdbinit file to load with gdb, or (3) specify library search paths explicitly?
Never mind, you can add -J-Dgdb.init.enable=trueto netbeans_default_options in /usr/share/netbeans/6.9/etc/netbeans.conf and it will load the .gdbinit file.