gdb: how to disable a specific pretty-printer - gdb

How to disable a specific pretty-printer in gdb?
For example, disable C++11 std::unique_ptr printer.
gdb help or documentation doesn't give a real-world or working example.

This worked for me:
(gdb) info pretty-printer
global pretty-printers:
libstdc++-v6
...
std::unique_ptr
...
(gdb) disable pretty-printer libstdc++-v6 std::unique_ptr
Source: https://www-zeuthen.desy.de/unix/unixguide/infohtml/gdb/Pretty_002dPrinter-Commands.html

Related

Is there a way to run functions (like std::string.size()) when examining core dumps with gdb?

Consider a c++ program that produces some segmentation fault and aborted.
In regular debugging using gdb, I can do the following and see results
(gdb) r
(gdb) p str_var.size()
where str_var is defined as std::string in the file.
However, I encountered some problem when debugging with a core dump. After I load the core dump in gdb by
gdb EXECUTABLE core.pid
and run the following command in gdb terminal
(gdb) p str_var.size()
gdb says "You can't do that without a process to debug."
I am only able to do things like bt (view stack trace) or directly print std::string variable, but just couldn't find an easy way to check some information like printing the size of a std::string. Is it always the case that debugging capabilities are limited when debugging a core dump? Particularly for the problem here, is there a way to know the size of a std::string in core dump debugging?
No, to be able to call a function, you need a context that is not present when reading core files. The only way to get you info is to explore the class(es) in order to find where it is stored, but can be painful as the stl can have some quite hard to follow implementations.
Moreover, depending on you program and compilation options, some values may be inlined (not stored anyway) or stored in registers, which can make the task quite painful.
Nevertheless, as said n.m, there are some pretty printers that ease the task if you use some standard stl (For example do not work on stlport AFAIK)
If you compile with gcc, std::string struct:
You should read _Rep before _M_dataplus._M_p.
p ((std::string::_Rep*)image_data)[-1]
And then print this.
{<std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep_base> = {_M_length = 30752, _M_capacity = 32711, _M_refcount = -1}, static _S_max_size = 4611686018427387897, static _S_terminal = 0 '\000', static _S_empty_rep_storage = <optimized out>}

How to debug C++11 code with unique_ptr in DDD (or gdb)?

std::unique_ptr are nice, but I find them less comfortable when debugging in DDD or gdb.
I'm using the gdb pretty printers that are part of gcc (e.g., /usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py). That is a big win for readability, for example:
$ print pTest
std::unique_ptr<MyType> containing 0x2cef0a0
However, dereferencing the pointer does not work:
$ print *pTest
Could not find operator*.
When I need to access the value, I have to manually copy the pointer and cast it to the correct type, for example:
print *((MyType*) 0x2cef0a0)
If the process is still running, this version works (still ugly but better):
print *pTest.get() // will not work if analyzing a core dump
The straightforward approach to Display *pTest in DDD does not work either. It only results in the following error:
<error: Could not find operator*.>
Is there a way to debug C++11 code with unique_ptr in DDD (without breaking the workflow like I do with my cumbersome workarounds)?
I'm not afraid to use gdb commands, but DDD integration would be a plus. For example, following pointers in data structures by just double-clicking on them is often faster than typing.
I already tried to drop the pretty printer, but it is also not optimal. The best that I could come up with is the following:
print pTest._M_t->_M_head_impl
This problem is actually not related to C++11, unique_ptr or pretty printing. The problem is that gcc does not emit code for std::unique_ptr::operator* that could be called by gdb to dereference the unique_ptr. If you for instance add *pTest; to your code then gdb does perform the dereferencing.
A similar problem is described in the SO post How to `print`/evaluate c++ template functions in gdb. Almost the same problem is described for an auto_ptr at https://sourceware.org/ml/archer/2012-q1/msg00003.html. If I understand the thread correctly one workaround would be to patch the pretty printer and also print out the dereferenced pointer when printing the unique_ptr. A gdb bug report can be found at http://sourceware.org/bugzilla/show_bug.cgi?id=12937.
The gdb wiki at https://sourceware.org/gdb/wiki/STLSupport describes more pretty printing solutions, which could have other workarounds.
Edit: A more elegant solution forcing the compiler to emit code for all member templates including operator* is to explicitly instantiate the class:
template class std::unique_ptr<MyType>;
GDB has a feature called xmethods which allows you to reimplement C++ methods in Python. This makes get() and the operator* available in GDB even if the compiler did not explicitly emit code for them.
Make sure that you are loading not only the pretty printers but also the xmethods in your .gdbinit:
python
import sys
sys.path.insert(0, '/usr/share/gcc-8.2.1/python/')
# This would only enable the printers but not the xmethods:
# from libstdcxx.v6.printers import register_libstdcxx_printers
from libstdcxx.v6 import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

How to define alias for gdb function

GDB supports function by command define. I want to write a helper script for GDB, and I hope each function has a meaningful name and an alias, just like bt and backtrace.
Does GDB support this feature?
An example to complete matt's answer:
alias ir = info registers
ir
as documented at: https://sourceware.org/gdb/onlinedocs/gdb/Aliases.html
Unlike Bash aliases, you cannot pass arguments to the definition of those aliases, e.g.:
alias ir = info registers eax
The registers part is only accepted because it is not an argument, but a subcommand.
But you can pass arguments when using the alias:
ir eax
You can then list all currently defined aliases with:
help aliases
(gdb) apropos alias
alias -- Alias one command to another
aliases -- Aliases of other commands

Tracing function calls in C++ using gdb

I have a system developed in C++ on Linux platform. I am doing some debugging of this system. I want to look for the complete sequence of function calls to a function. Lets assume the functions are called in the following sequence
function_1 -> function_2 -> function_3 -> function_4
If I put a break point at function_4, the execution will be holded at that point. I want to see that functions_1, function_2 and function_3 are called before function_4. If there any gdb command to trace these function calls?
Thanks,
Ankur
You want a backtrace. The gdb command bt will show exactly what you are interested in.
bt: backtrace
http://sourceware.org/gdb/onlinedocs/gdb/Backtrace.html
If function_1() calls function_2() which calls function_3() etc
You can set your breakpoint in function_4() and you use the command
where
To print a backtrace of the stack
Another tool that may be useful is valgrind with the callgrind tool

how to print std::map value in gdb

I have a std::map< std::string, std::string> cont;
I want to see cont[ "some_key" ] in gdb. When I'm trying
p cont[ "some_ket" ]
I'm getting this message: One of the arguments you tried to pass to operator[] could not be converted to what the function wants.
I'm using GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh). Thanks
The latest gdb has python support baked in so one could easily write a function to print out the contents of any stl structure. However you'd have to learn the API and write the script. Luckily gcc 4.5 will ship with the needed python scripts to get gdb to intelligently handle stl data structures.
EDIT: you don't have to wait for GCC 4.5 (which by the way has already been released), you can just grab the code from SVN.
You can write your own dump functions and call them:
(gdb) call dump(m)
see this thread:
http://www.mail-archive.com/help-gplusplus#gnu.org/msg02109.html
I'm curious about the GDB helper macros.
Gdb doesn't understand C++ operator overloading.