Debugging clang with gdb - gdb

When I build clang I add the flag -DCMAKE_BUILD_TYPE=Debug.
Now I want to debug my clang.
I do gdb build/bin/clang. But when I want to add a breakpoint break llvm/lib/Transforms/Instrumentation/AddressSanitizer:instrumentStoreInstruction and gdb throw an error
Function "instrumentStoreInstruction" not defined in "/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp".
But prototype and definition of that function are in that file.

Related

ICC compile options for evaluating macros in GDB while debugging

I would like to evaluate and print the macro while debugging using GDB. While the GDB documentation has steps to do that by compiling using -g3 flag in gcc compiler, I am using Intel Icc compiler. Their debugging compilation options seem to have no information about macros. Is it possible to do that using icc? If yes what are the compilation options.
icc --help prints almost 2000 lines of output, among which there's
-debug [keyword]
Control the emission of debug information.
Valid [keyword] values:
[snip]
[no]macros
Controls output of debug information for preprocessor macros.
but passing -debug macros results in an error:
icc: command line error: Unrecognized keyword 'macros' for option '-debug'
It's unclear what happened here, perhaps the option was available in the past, and removed since then. You can report this to Intel.
The new LLVM-based Intel compiler, ICX, can emit macro definitions to debug info under the same option as Clang, -fdebug-macro.

C++ Suspected stack overflow changing function parameters

I am working on implementing a user level thread library in C++ using setcontext(), makecontext(), getcontext(), and swapcontext() on a Linux system.
I am using a wrapper function to wrap the function the user wants to run as a thread. For example, the user calls newthread(funcPtr), and within the thread library funcPtr is passed to a wrapper function that runs it.
The error occurs differently depending on whether or not I initiate an unused string within the function. If I include the line string s = "a"; the program will run to completion, but gdb reveals that context is switching to somewhere within the string library. Without this line, the program segfaults after leaving the function wrapper.
The gdb output shows the corruption of the parameters to function().
I ran valgrind but did not see anything particularly out of the ordinary in the output, just many "Invalid read of size 4" and "Invalid write of size 4" warnings, usually within the C++ standard map.
You could try also AddressSanitizer for debugging. It can detect stack buffer overflows. Here's how to use it on Linux:
At least gcc 4.8 is needed for AddressSanitizer and libasan must be installed (e.g. on Fedora yum install libasan as root). Compile and link with -g -fsanitize=address and run the generated executable. AddressSanitizer stops and emits information if it detects the first error, no long log files have to be analyzed. Solve the reported problem, compile and run again until AddressSanitizer doesn't stop the program anymore. Unfortunately there might be false positives because you use swapcontext in your program, but it's worth a try. Instrumentation can be turned off for a specific function by adding the attribute no_sanitize_address: extern int func(void) __attribute__((no_sanitize_address));

Compilation GDB give error

Related to problem 16611678 I need a new version of gdb in my centos 6.5 64.
I try to compile GDB (7.7, 7.6.2 and 7.5), without success, the error is related to get_tty_state() in ser-unix.c:
ser-unix.c:118:1: error: conflicting types for ‘get_tty_state’
My gcc version is 4.8.2
Before the function declaration there is preprocessor directives for defining hardwire_ttystate if HAVE_TERMIOS is defined and I think the problem come from here (if needed I can post the piece of code), as HAVE_TERMIOS is undefined.
Any help would be appreciated!
Nathanaël

GDB backtrace tells me symbol name but not source file

I have a problem where I can't seem to step into some functions using GDB.
I'm using the "PImpl idiom", where I have an inline class in my .cpp file, containing functions that get called from the publicly visible class, like so:
// Foo.cpp
class FooImpl
{
public:
void open()
{
// ...
}
};
Foo::open()
{
// Impl is a FooImpl*
impl->open();
}
Using the debugger, I can't seem to step into FooImpl::open().
I know for sure that the call is not inlined (I'm using -fno-inline and I can see the call instruction in the assembly);
I can set a breakpoint inside the function and GDB can hit that breakpoint and tell me its name and what function I'm in.
However, it will not tell me the source file (even though it's the same file as Foo::open())
I can't step INSIDE the function; when I execute step, it simply steps over the call.
This is what my stacktrace looks like when I'm on a breakpoint inside the FooImpl::open() call:
#0 0x080eee52 in macawi::PowerMateInputImpl::open(std::string) ()
#1 0x080ee766 in macawi::PowerMateInput::open (this=0x83cf204)
at ../../app/hal/interfaces/powermateinput_linux.cpp:126
#2 0x08137455 in macawi::ActorInput::backgroundLoop (this=0x83cf204)
at ../../app/common/actors/actorinput.cpp:51
Can anyone tell me why GDB can't determine the source location of the top stack frame, even though it's in the same file as stack frame #1?
(For the record, I'm using a graphical debugger that uses GDB in the background (Qt Creator), but the same things hold when I execute GDB directly).
EDIT: The compilation command line looks like this:
g++ -c -pipe -g -O0 -fno-inline -ggdb -fPIC -Wall -W ...(defines, include dirs, object file, source file)
Can anyone tell me why GDB can't determine the source location of the top stack frame, even though it's in the same file as stack frame #1
This is a bug, either in GDB, or in GCC.
Unfortunately, you haven't told us which versions of GCC and GDB you are using, so we can't even begin to guess which versions you may need to update to.
Try building current GDB and GCC. If they still fail, write a small reproducer and file a bug with GDB (if it turns out to be a GCC bug, GDB developers will tell you).

gdb unable to set breakpoint

I am using g++ 4.1.2 and gdb 7.2
I am debugging code that uses Xerces, which I built using the same tools, though presumably without debugging.
GDB steps through my code just fine, but of course does NOT step through Xerces because it probably doesn't have debugging information, and definitely does not know where the source directory is. But all I want is to set a breakpoint when Xerces (a callback parser) calls a callback object.
Their base class is DefaultHandler
I have a class ContentHandlerBase : public DefaultHandler
Then leaf classes inherit from ContentHandlerBase. These leaf classes are inside namespace A, for example
in gdb I try to set a breakpoint.
b A::LeafContentHandler::LeafContentHandler
b A::LeafContentHandler::endElement
The first breakpoint works because the code is inline (defined in the header).
The second breakpoint does not work, meaning gdb claims that no such symbol exists, even though it obviously does. It is a virtual function defined in the Xerces library, if that makes a difference. Before I recompiled Xerces, it was built with g++3.4.6 and I could not find the symbol in gdb. Now, gdb finds the symbol (I can hit tab) but then it says it doesn't exist, should I wait for a library to load.
Can anyone tell me what I have to do to make it work? I'd prefer not to build all of xerces with debugging.
Note that in some cases, with the constructor in the .cpp file, it also worked for some reason, and then, because it was in the same file, I could set a subsequent breakpoint at linenumber, and that worked.
Try gdb 7.1 - it seems there are some problems in setting breakpoint by function name in gdb 7.2