GDB MI mechanism to debug a self executing program - c

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.

Related

Program can't kill() processes when launched from boot script

I'm sure my question has probably been answered previously but I didn't find anything specific to my situation after searching for a while.
Background:
I have written a suite of data acquisition tools in C that run on an embedded system running Debian Wheezy. There is a main module, called Dispatch, whose job is to launch the rest of the modules and pass messages between them. I put a trivial bash script in /etc/init.d that executes Dispatch when the system boots since this system runs unattended. This system runs without any local user interaction so Dispatch should really be written to function as a daemon but it is not. The startup script simply executes /opt/bcdispatch &.
There's a bug in one of the other modules that causes it to crash every few days. I'm trying to hunt down that bug but in the meantime I am trying to write a watchdog program that will detect the crash, kill off all of my processes, then relaunch Dispatch. For reasons I won't go into it is not sufficient to just relaunch the crashed process, the whole suite of tools needs to be restarted.
What I'm trying to do:
I wrote a simple watchdog program that periodically executes popen("ps aux | grep bc") (all of my process names start with "bc" which makes it easy to find them with grep), finds that one of the modules has crashed by looking for anything with a "zombie" status in any of the lines read from popen(), kills all of my processes by calling system("kill <PID>"), then executes the startup script in /etc/init.d and exits. I modified the startup script so that it launches the watchdog after launching Dispatch. The startup script now looks like:
/opt/bcdispatch &
/opt/mywatchdog &
Everything is being run as root. There are no other user accounts on the system.
Problem
The watchdog process works fine if I run it from the command line. It kills off all of the processes it's supposed to, launches the startup script, then exits. However, when the watchdog is launched by the startup script at boot time it doesn't do its thing. It's running, one of the processes it's monitoring has crashed, but it doesn't kill the rest of them off. It just sits there like a giant turd. I can start another instance of it from the command line and that one works just fine.
Question
So my question (finally!) is: why can't my program kill other processes when launched via a startup script? I suspect it has something to do with the fact that the watchdog process no longer has a terminal associated with it? I tried substituting the call to system("kill <PID>") with kill(PID) but that didn't change anything.
EDIT
It just occurred to me that it's not the kill()ing part that doesn't work (well, that might be broken as well), the call to popen("ps aux | grep bc") must not be working since the watchdog should exit after it finds the zombie process but it isn't. Its PID is still the same as it was when the system booted. I guess this means the title of this question isn't very good.
Found the problem. The output of my watchdog's call to popen("ps aux | grep bc") was being truncated to 80 columns, presumably because it was no longer attached to a terminal and that's the default terminal width. That truncation was causing problems for the way the program was parsing the results of the ps command so it never found the crashed process. Changing the command to popen("ps -w aux | grep bc") was all that was needed to fix it.

Qt using gdb to debug an attached process

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

How to attach debugger to a C program when run using Jenkins

I have a batch script that calls a bunch of programs written in C that do a lot of data processing. It works fine when run manually but when Jenkins calls the batch, one of the programs fails with a useless error message.
Before I litter the program with printf()s, I tried to use DebugBreak() to attach the debugger to see what difference Jenkins is making but the program just ends without invoking the debugger.
Is there a way to invoke the debugger from within the failing program that'll work with Jenkins?
EDIT: I tried calling MessageBox() but nothing appears and Jenkins sits there waiting for console output.

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.

Issue with program executed by crontab

I've coded a program in c for an embedded system (Devkit8000, which is a clone of the well known BeagleBoard) running Angstrom Linux.
The program creates a couple of threads, on of them is responsible of taking pictures with a camera connected to the board, and right now the second thread only moves that images to another path. The program should be running during the whole day, and the only way to stop it is sending a signal.
I edited the crontab to launch the program in a specific hour and to send a signal when it has to stop, the issue is that launching the program in this way cause the process to be killed after some time running, but, if i launch the program manually (through the command line), it works perfectly and dont get stopped.
I have no idea about the reason of this different behaviour between crontab and command line. I've checked the system logs but didnt find anything useful. I've also been reading a little and find that the OS can kill a process if it is using so much resources, but doesnt make sense that this happens in only 1 scenario (crontab vs manually)...
Any clue about what is happening?
Thank you in advance!
The main difference is that running a job through cron invokes a non-interactive non-login shell. The effect of that depends on the default shell for your user. For example, if you are using Korn shell or Bash then your .profile will not be executed, as it would on an interactive login shell. Korn shell 88 will execute .kshrc (the $ENV file) but ksh93 will not.
So, a good start might be to call your program from a script, after first "sourcing" your .profile file:
. $HOME/.profile
Failing that... When you say that the process is "killed", do you get such a message? If so, then that sounds like someone sending SIGKILL, i.e. kill -9. If not, then maybe you could run strace or ltrace to find out at what point it dies.

Resources