Qt using gdb to debug an attached process - c

Environment
Qt 5.6.1
Qt Creator 4.0.1
gdb 7.11
Ubuntu 16.04 LTS
Scenario
C: A client application to communicate with M.
M: A manager process to notify L to launch a new process T.
L: A Launcher process to launch new T by forking itself.
T: A new process running in the background.
I am able to run test application and debug the process C in Qt with gdb. But I am not able to debug the T.
Here is the way I tried to debug the T:
Set breakpoints in both C and T;
When the breakpoint is hit. I use Qt menu option "Debug"->"Start Debugging"->"Attach to running application". To try to attach the debugger to the T process.
This is the problem I am having
Instead of hitting the breakpoints that I set in the T. The gdb always hit an invisible breakpoint in function epoll_wait(). After that, if I continue (F5). The application will keep hanging without hitting any further breakpoints in T. Unless I force stop by using the Qt debug option "Stop Debugger". The application is keep waiting. After I stopped the debugger, the C still breaks in the original breakpoint.
The problem with the debugger in Qt
It seems that Qt uses two different debuggers for different processes. I am thinking it might be caused by the C is hanging. So the T process is keep waiting. But I did not set any breakpoints in wrap_epoll_wait() function I am not sure why gdb breaks there. And in the Qt Debugger. I cannot find a way to switch back to C process to let the process to continue to run. (The Qt debugger component "Threads" drop list is disabled by some reason, I can not select a different thread).
The things I tried
Modified the /etc/sysctl.d/10-ptrace.conf set kernel.yama.ptrace_scope value to 0
Turned the debugger option "Tools->Options->Debugger->GDB Extended->Debug all children" on and off in the Qt.
None of above things changed the fact that the debugger is hanging after the debugger breaks in the function wrap_epoll_wait().
My Question
Anyone at good gdb and Qt knowledge could help me? And let me know how the gdb debug multiple processes works in Qt? How to switch the debugger between different processes and why gdb breaks on somewhere I did not set the breakpoint?
Thank you very much,
Rong

Since T created by forking from L. The gdb settings 'set follow-fork-mode' needs to be set to 'child' in Qt creator.
reference:
https://sourceware.org/gdb/onlinedocs/gdb/Forks.html

Related

How to debug child project process in Visual Studio

I have a solution with two console applications. Application B is started by application A. I would like to debug both, but at least application B.
I have tried to configure as debug profile for application B to start executable A instead of itself, and I have set up application B to be the startup project. When I hit Run application A is started as expected, which in turn starts application B, however I still get the warning on my breakpoint in application B that it won't be hit because the source code is different. I have tried to set dependency of A from B, and also of B from A, but neither made any difference.
Update: However, attaching to the already running process B works as expected. This can be a workaround but a quite cumbersome one.
What can be done to get this right?
What can be done to get this right?
Please try to use the Child Process Debugging Power Tool which is also suitable for VS2019.
We can easily debug this child project with this extension by Debug-->Other Debug Targets-->Child Process Debugging Settings.(If you are debugging .NET code, you must choose to enable mixed mode debugging which means that you debug two different types of projects.)
In the pop-up page, check the box to open the startup child process debugging and click Save. After that, default debug will support all child processes.
Note that by default, the same debug engine settings are used as the parent process debug.
More information you could refer to this blog. Hope it could help you.

How to debug C application using eclipse IDE under Linux

As you know,eclipse IDE has a convenient attached debugging facility for C project.You can see it from the GUI and you can use this facility to debug process that are already in running status,like daemon process..
My question is that when a process just started and I want to debug it from the begining of the process(i.e. from the first line of main function),how I can do it using the IDE?
I know under Windows,there is a tool called gflag,using this tool we can do some configuraitons before starting the process,and when the process is launched,the gflag can detect this and let the debugger tool(e.g. virtual studio) attach the process automatically.
Do not tell me that use sleep fuction.
Check CDT reverse debugging. You will need GDB 7.0 or later for this feature.
Refer
How_do_I_do_Reverse_Debugging
Open source code with eclipse and double click the left of line number to add a break point. Then you can create a session to debug your application

Debugging GTK Event callbacks

I'm trying to debug for the first time a gtk code and actually, I want to debug a callback function for key-press-event so I set a breakpoint with gdb and when it hits the breakpoint, the whole desktop evirement is freezing (I'm running under gnome-shell) it seems that the graphic envirement is waiting for the event to finish.
I got some idea that didn't worked :
The first attempts was to assign some gdb commands to the breakpoint :
(gdb) break on_key_press_callback
(gdb) commands
> back trace
> next
> next
> next
> continue
> end
but I don't know why, only the back trace command is executed, and then freeze.
The second attempt was to debug remotely using gdbserver and gdb on tty1 (no graphigs to freeze :) ) I was able to send commands like next and step after the breakpoint but there was nothing to see (can't list code, inspect the stack, ect ...)
So any good tips to be able to debug in such situations ?
Thanks
It's typical to have lockups when debugging an X program running on the same server that you're using to debug. For example, if the inferior (gdb terminology for the program being debugged) does a server grab -- whoops, the gdb GUI (or terminal or emacs etc) is locked out.
Your idea of doing more programmatic debugging is a good one, but it's hard to make it all work nicely. As you found, some gdb commands (basically those related to inferior control) don't work in commands. Also, it just isn't nearly as convenient.
You can go further that direction. For example you could use SystemTap to probe the program instead.
However there are also nicer approaches.
One way is to run the inferior using a virtual X server -- a nested one, or one running in a VM. Then you can escape to the "outer" X server for debugging. This will be much nicer.
Another way is to have two computers, and run the debugger on one and the inferior on the other. In a way this is really the same answer, just using a real machine rather than a virtual one.
The question is old, but for anyone that may be struggling with this issue, there is a way to stop the freezing behaviour from happening by disabling the X server grab.
You have to change the config in your IDE to run the program with the following system property:
-Dsun.awt.disablegrab=true
If you are using javaws (Java Web Start) to run the application use:
-J-Dsun.awt.disablegrab=true
instead.

PostgreSQL step debugging in IDE

I'd like to know if anybody had success with debugging postgres through an IDE (NetBeans or Eclipse) by attaching the debugger to the process and still perform step debugging (stop at breakpoints and watch variables).
For most cases, debugging PostgreSQL isn't significantly different to any other server in this regard.
You determine the process ID of the process you wish to examine, then attach the debugger to it and set breakpoints/watchpoints and continue execution. When you hit the break/watch, examine, step, etc as required.
You can SELECT pg_backend_pid() to get the process ID of the current session.
I usually start psql, SELECT pg_backend_pid(), attach gdb, set my breaks/watches, cont, and in my psql session run whatever I expect to trigger my breakpoint/watchpoint. The same principle applies with IDE debuggers (Visual Studio, Eclipse, NetBeans, whatever).
See: the PostgreSQL wiki for some instructions.

GDB MI mechanism to debug a self executing program

I recently downloaded MI library and executed / read some examples code. In all the examples that I saw the implementation was via a separate program like below:
Main Program
Interact with GDB
exec another program
Gather Debug info of the another program
My question is how can we invoke / implement GDB within current thread/program itself i.e. attach this self-execution process.
how can we invoke / implement GDB MI within current thread/program itself i.e. attach this self-execution/ running process to GDB and then tell it to execute step by step
You can't.
Think about it: the very first thing GDB will do after attaching is stop your program, and wait for instructions on what to do next. At that point your program should send GDB the continue command, except it can't because it has been stopped.
Now, if your program is multi-threaded, and you ask GDB to execute in non-stop mode, and you carefully arrange things such that your thread that performs GDB interaction itself is never stopped, then perhaps this could work. I doubt it will work reliably though.

Resources