EzDevInfo.com

gdb interview questions

Top gdb frequently asked interview questions

How do I print the elements of a C++ vector in GDB?

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity.


Source: (StackOverflow)

Can I set a breakpoint on 'memory access' in GDB?

I am running an application through gdb and I want to set a breakpoint for any time a specific variable is accessed / changed. Is there a good method for doing this? I would also be interested in other ways to monitor a variable in C/C++ to see if/when it changes.


Source: (StackOverflow)

Advertisements

Show current assembly instruction in gdb

I'm doing some assembly-level debugging in gdb. Is there a way to get gdb to show me the current assembly instruction in the same way that it shows the current source line? The default output after every command looks like this:

0x0001433f      990         Foo::bar(p);

This gives me the address of the current instruction, but I have to keep referring back to the output of disassemble in order to see which instruction I'm currently executing.


Source: (StackOverflow)

How to highlight and color gdb output during interactive debugging?

Please don't reply I should use ddd, nemiver, emacs, vim, or any other front-end, I just prefer gdb as it is, but would like to see its output with some terminal colors.


Source: (StackOverflow)

How to print register values in gdb?

I want to print the value of %eax and %ebp:

(gdb) p $eax
$1 = void

Source: (StackOverflow)

Set breakpoint in C or C++ code programmatically for gdb on Linux

How can I set a breakpoint in C or C++ code programatically that will work for gdb on Linux?

I.e.:

int main(int argc, char** argv)
{
    /* set breakpoint here! */
    int a = 3;
    a++;  /*  In gdb> print a;  expect result to be 3 */
    return 0;
}

Source: (StackOverflow)

Inspect the return value of a function in gdb

Is it possible to inspect the return value of a function in gdb assuming the return value is not assigned to a variable?


Source: (StackOverflow)

How to remove a single breakpoint with gdb?

I can add a break point in gdb with:

b <filename>:<line no>

How can I remove an existing breakpoint at a particular location?


Source: (StackOverflow)

Is there a way to change another process's environment variables?

On Unix, is there any way that one process can change another's environment variables (assuming they're all being run by the same user)? A general solution would be best, but if not, what about the specific case where one is a child of the other?

Edit: How about via gdb?


Source: (StackOverflow)

Do you debug C++ code in VIM? How?

The question is to all you people, who use VIM to develop C++ apps.

There was a period in my life, which can be described as 'I hate VIM!!!'..'VIM is nice!' However, having grown up mostly on MS Dev IDE, I've got used to those F5-F11 shortcuts when debugging code, watch window, call stack and the main code - all visible vithout need to type any gdb commands.

So, here is the question: Do you use VIM as well for debugging ? Or you switch to some IDE for this purpose ? Which one ? For those who use VIM to debug code: are there plugins to set breakpoints in editor, highlight the line we're currently debugging, auto-navigation during step, step into, step out ?

Please, don't tell me you use gdb as command line, see only one line which is debugged etc.

And thanks for all your answers. Andrey


Source: (StackOverflow)

Is there a C++ gdb GUI for Linux?

Briefly: Does anyone know of a GUI for gdb that brings it on par or close to the feature set you get in the more recent version of Visual C++?

In detail: As someone who has spent a lot of time programming in Windows, one of the larger stumbling blocks I've found whenever I have to code C++ in Linux is that debugging anything using commandline gdb takes me several times longer than it does in Visual Studio, and it does not seem to be getting better with practice. Some things are just easier or faster to express graphically.

Specifically, I'm looking for a GUI that:

  • Handles all the basics like stepping over & into code, watch variables and breakpoints
  • Understands and can display the contents of complex & nested C++ data types
  • Doesn't get confused by and preferably can intelligently step through templated code and data structures while displaying relevant information such as the parameter types
  • Can handle threaded applications and switch between different threads to step through or view the state of
  • Can handle attaching to an already-started process or reading a core dump, in addition to starting the program up in gdb

If such a program does not exist, then I'd like to hear about experiences people have had with programs that meet at least some of the bullet points. Does anyone have any recommendations?

Edit:
Listing out the possibilities is great, and I'll take what I can get, but it would be even more helpful if you could include in your responses:
(a) Whether or not you've actually used this GUI and if so, what positive/negative feedback you have about it.
(b) If you know, which of the above-mentioned features are/aren't supported

Lists are easy to come by, sites like this are great because you can get an idea of people's personal experiences with applications.


Source: (StackOverflow)

How to pass arguments and redirect stdin from a file to program run in gdb?

I usually run a program as :

./a.out arg1 arg2 <file

I would like to debug it using gdb.

I am aware of the set args functionality, but that only works from the gdb prompt.


Source: (StackOverflow)

Printing all global variables/local variables?

How can I print all global variables/local variables? Is that possible in gdb?


Source: (StackOverflow)

How can I make gdb save the command history?

How can I set up gdb so that it saves the command history? When starting a new gdb session I'd like to use the arrow up keys to access the commands of the previous sessions.


Source: (StackOverflow)

Core dump file analysis [duplicate]

This question already has an answer here:

What are all the things I will need to check while analyzing core dump file? Please tell me from scratch.


Source: (StackOverflow)