Skip to main content

20 posts tagged with "c++"

View All Tags

· One min read
Hreniuc Cristian-Alexandru


My server(started from qtcreator in Debug) was sending data to a client and the client crashed ungracefully(CTRL+C), this resulted in two things:

  • SIGPIPE signal was generated(which was handled in my code, but before getting to my code GDB was handleling it, and crashed my server)
  • The server crashed with SEGFAULT when I was running it in terminal with no GDB attached.

So, to investigate the SEGFAULT easier, inside QtCreator, I had to force GDB to ignore the first signal SIGPIPE, I did this by doing thse two steps:

  • uncheck the box "Show a message box when receiving a signal" in Tools -> Options... -> Debugger -> GDB.
  • If you want gdb not to catch a signal but pass it to your program, you need to give gdb a command like the following:handle SIGPIPE pass nostop noprint(see Window -> Views -> Debugger Log or Tools -> Options... -> Debugger -> GDB -> GDB startup script Debugging Helper Customization or Tools -> Options... -> Debugger -> GDB -> Locals & Expressions (like the image below for the SIGPIPE signal)).

· One min read
Hreniuc Cristian-Alexandru

Common flags for sanitizers, here

  1. Redirect report to specific file:
export TSAN_OPTIONS="log_path=tsan_output.log"

All options that can be set for thread sanitizer can be found here

· 2 min read
Hreniuc Cristian-Alexandru

GDB cheatsheet

Start gdb with coredump:

gdb ./executable coredump

Print all bracktrace:

bt full

Go to a specific frame:

frame 3

Content of an oject of a specific type from a specific address:

print *(class::client::client_data*)0x7f2a10000c50

Content of a variable:

print conn_ptr

Print the content of the object stored in a shared_ptr:

print *shared_ptr_obj._M_ptr

Print the content of std::string:

# 0x7fdaa000f690 is the address of the string.

print *(char**)0x7fdaa000f690

Print a specific element from a vector:

# A shared_ptr to a vector
print *((*vector_shared_ptr._M_ptr)._M_impl._M_start+0)

print *((vector_obj)._M_impl._M_start+0)

Print multiple elements from a vector(ex 9 elements):

# A shared_ptr to a vector
print *((*vector_shared_ptr._M_ptr)._M_impl._M_start)@9

print *((vector_obj)._M_impl._M_start)@9


Type info variables to list "All global and static variable names".

Type info locals to list "Local variables of current stack frame" (names and values), including static variables in that function.

Type info args to list "Arguments of the current stack frame" (names and values).

Print long string(more here):

set print elements 0
print string

Print variable value in a file(more here):

set logging on

print string

set logging off

# The content can be found in gdb.txt
# If you want to change the file run:
set logging file my_god_object.log

· One min read
Hreniuc Cristian-Alexandru

Build project with : -fsanitize=address -fsanitize-recover=address

Run the excutable with this enviroment variable: ASAN_OPTIONS=halt_on_error=0. Source:


ASAN_OPTIONS=halt_on_error=0 ./executable

When the address sanitizer will find a problem, it will not abort the execution of the exe as it ussualy does.

· One min read
Hreniuc Cristian-Alexandru

We encountered a problem when using the debugger from qtcreater with the gcc 9.1.0. The values of the std::string were not printed, they were marked as : <not accessible>. We found the fix on stackoverflow.

The fix is simple, you need to change the file: /usr/share/qtcreator/debugger/ as shown in the diff:

< (size, alloc, refcount) = d.split("ppp", value.address() + d.ptrSize())
> (size, alloc, refcount) = d.split("ppp", data - 3 * d.ptrSize())
< if size > 1000:
< size = 1000
> d.check(0 <= size and size <= alloc and alloc <= 100*1000*1000)

· One min read
Hreniuc Cristian-Alexandru

Create a build folder for each project called project_name, which will contain multiple subfolders for each build_type.

Go to Tools -> Options -> Build & Run -> Default build directory:

/path/to/builds/%{JS: Util.asciify("%{CurrentProject:Name}/build-%{CurrentProject:Name}-%{CurrentKit:FileSystemName}-%{CurrentBuild:Name}")}

This will result in a a tree of folders:

  • builds
    • project_name
      • build-project_name-Kit_name-Build_type
    • project_name2
    • build-project_name2-Kit_name-Build_type

Or we can set it to :

/path/to/builds/%{JS: Util.asciify("%{CurrentProject:Name}/%{CurrentBuild:Name}")}

This will result in a a tree of folders:

  • builds
    • project_name
      • Release
      • Debug
    • project_name2
    • build_type

· One min read
Hreniuc Cristian-Alexandru

How to debug using strace:

strace -s 80 -f -p 24274 2>&1 | stdbuf -i0 -o0 -e0 grep write
  • -s number - Strace usually prints only a few chars for each system call, to make it print more chars use -s number of chars
  • -f - Follow all threads opened by parent pid
  • -p pid - Pid to follow
  • 2>&1 - Reddirect stderr to stdin to be able to grep it
  • stdbuf -i0 -o0 -e0 - Flush each line to grep