How to configure GDB in Eclipse such that all prcoesses keep on running including the process being debugged? - c

I am new in C programming and I have been trying hard to customize an opensource tool written in C according to my organizational needs.
IDE: Eclipse,
Debugger: GDB,
OS: RHEL
The tool is multi-process in nature (main process executes first time and spawns several child processes using fork() ) and they share values in run time.
While debugging in Eclipse (using GDB), I find that the process being debugged is only running while other processes are in suspended mode. Thus, the only running process is not able to do its intended job because the other processes are suspended.
I saw somewhere that using MI command in GDB as "set non-stop on" could make other processes running. I used the same command in the gdbinit file shown below:
Note: I have overridden above .gdbinit file with an another gdbinit because the .gdbinit is not letting me to debug child processes as debugger terminates after the execution of main process.
But unfortunately debugger stops responding after using this command.
Please see below commands I am using in the gdbinit file:
Commenting non-stop enables Eclipse to continue usual debugging of the current process.
Adding: You can see in below image that only one process is running while others are suspended.
Can anyone please help me to configure GDB according to my requirement?
Thanks in advance.

OK #n.m.: Actually, You were right. I should have given more time to understand the flow of the code.
The tool creates 3 processes first and then the third process creates 5 threads and keeps on wait() for any child thread to terminate.
Top 5 threads (highlighted in blue) shown in the below image are threads and they are children of Process ID: 17991
The first two processes are intended to initiate basic functionality of the tool and hence they just wait to get exit(0). You can see below.
if (0 != (pid = zbx_fork()))
exit(0);
setsid();
signal(SIGHUP, SIG_IGN);
if (0 != (pid = zbx_fork()))
exit(0);
That was the reason I was not actually able to step in these 3 processes. Whenever, I tried to do so, the whole main process terminated immediately and consequently leaded to terminate all other processes.
So, I learned that I was supposed to "step-into" threads only. And yes, actually I can now debug :)
And this could be achieved because I had to remove the MI command "set follow-fork-mode child". So, I just used the default " .gdbinit" file with enabled "Automatically debug forked process".
Thanks everyone for your input. Stackoverflow is an awesome place to learn and share. :)

Related

Forked child keeps being terminated with status 0x008B

I'm on a VirtualBox with Ubuntu 18.10 installed on, and I'm new using it. My code creates 100 forked child that works on a shared memory. SOMETIME I get this message
Sender(Pid = (childPID)) terminated with status 0x008B.
Searching in the web I found that could be a SIGSEGV error. Is it true?
Finally, is there any way to find WHERE the code fails in over 1000 lines? I tryed using this Guide: http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html to find the error with gdb but my terminal says me that I have "No Stack". I'm totally new with this kind of problems, any hint will be appreciated.
Sender(Pid = (childPID)) terminated with status 0x008B.
Searching in the web I found that could be a SIGSEGV error. Is it true?
Yes, that indicates termination by signal 11 (0xB).
Finally, is there any way to find WHERE the code fails in over 1000 lines?
I'd run the program with valgrind.

How to make c programe as daemon in ubuntu?

Hi I am new to the linux environment. I am trying to create daemon process.
#include<stdio.h>
int main()
{
int a=10,b=10,c;
c=sum(a,b);
printf("%d",c);
return (0);
}
int sum(int a,int b)
{
return a+b;
}
I want to create daemon process of it. May i know how can do this? Any help would be appreciated. Thank you.
A daemon generally doesn't use its standard input and output streams, so it is unclear how your program could be run as a daemon. And a daemon program usually don't have any terminal, so it cannot use clrscr. Read also the tty demystified page, and also daemon(7).
I recommend reading some good introduction to Linux programming, like the old freely downloadable ALP (or something newer). We can't explain all of it here, and you need to read an entire book. See also intro(2) and syscalls(2).
I also recommend reading more about OSes, e.g. the freely available Operating Systems: Three Easy Pieces textbook.
You could use the daemon(3) function in your C program to run it as a daemon (but then, you are likely to not have any input and output). You may want to log messages using syslog(3).
You might consider job control facilities of your shell. You could run your program in the background (e.g. type myprog myarg & in your interactive shell). You could use the batch command. However neither background processes nor batch jobs are technically daemons.
Perhaps you want to code some ONC-RPC or JSONRPC or Web API server and client. You'll find libraries for that. See also pipe(7), socket(7)
(take several days or several weeks to read much more)
First find what are the properties of daemon process, as of my knowledge a daemon process have these properties:
Should not have any parent (it itself should be parent)
Process itself is a session leader.
Environment change to root.
File mode creating mask should be zero.
No controlling terminal.
All terminal should be removed
Should not be un-mounted .
Implement the code by considering above properties which is
int i=0;
int main()
{
int pid;
pid=fork();
if(pid!=0) {
/** you can add your task here , whatever you want to run in background **/
exit(0);
}
else
{
setsid();//setting sessions
chdir("/");//root.. should'nt beunmounted
umask(0);
close(0);//all terminal are removed
close(1);
close(2);
while(1)
{
printf("i = %d \n",i);
i++;
}
}
return 0;
}
or you can go through man page of daemon()
int daemon(int nochdir, int noclose);
I hope it helps.
Instead of writing the code to make the C program a daemon I would go with an already mature tool like supervisor:
http://supervisord.org/
I think this below will work
screen cmd arg1 arg2
You can also try
nohup cmd arg1

I want a function in C on linux to collect core dump without terminating the process

abort() do collect the core dump, but I don't want the process to terminate. dump_core() collects the core dump, but in kernel space. Is there any function equivalent to dump_core() in user space?
A simple way to do it yourself is to fork the process (which creates a complete copy of the parent process) and call abort from the child process.
The child process will be aborted with a core-dump, while the parent process continues as if nothing happened.
Use gcore.
.
.
.
char command[ 1024 ];
sprintf( command, "gcore -o /core/file/name %d", getpid() );
system( command );
.
.
.
Error and bounds checking are omitted.
There is no such Linux C command. However, you may find some third party tools that can do this for you. For example, Google coredumper, which is also supposed to be able to capture all the threads. Another way would be to attach gdb to your running process, and issue the gcore command. This is essentially what the gcore command line utility does.
Kernel generates SIGSEGV signal to the process whenever coredumps, I think you should attach a handler to the SIGSEGV signal(Link) and call fork from that handler function.

SIGHUP signal handler reset to default when using system() on embedded linux with busybox

I am working on an embedded linux with busybox. As part of my application I have a rc.init script E80-startmyprog. That script is calling my program prog.
trap "" HUP
startmyprog >${LOGFILE} 2>&1 </dev/null &
startmyprog() {
prog
}
In my program I can see that the signal handlers are set to ignore SIGHUP. I am checking that with
struct sigaction act;
sigaction(1, NULL, &act);
printf("action %p\n", act.sa_sigaction); // prints out 1 -> SIG_IGN
Now at some time in my program I need to start another process, give it some input and check if it printed "yes" on stdout. I am using system to do that.
const int ret = system("[ `echo input | second_process` == yes ]");
Normal behavior is that WIFEXITED(ret) is true and that WEXITSTATUS(ret) is 0 or 1.
But in some cases with unfortunate timing WIFSIGNALED(ret) is true and WTERMSIG(ret) is 1 (SIGHUP).
Debugging shows that if I execl("second_process", "second_process", (char*)NULL) the signal handler state in second_process is set correctly to SIGHUP=ignore. But if I use system, then second_process has SIGHUP=default.
My questions are:
What is going on there? Who is resetting the SIGHUP signal handler at startup? Is it the shell? Is there a way to prevent that? (In the sh man page I have not seen a command line option that looks so.)
I know I could do a workaround and setup a pipe, fork, exec second_process, write input to pipe, read output from pipe and parse output, but that is a lot of stuff compared to a system one liner and there is good chance that I will miss something and get it wrong.
I don't think that's exactly what's happening. When you type 'trap' into the shell, you're asking it list the actions it has established for that signal. When you invoke a subshell, you haven't given it any actions, so it won't show any (it doesn't know about what you gave to its parent) but that doesn't mean it has reset the SIGHUP state.
Try this small script that proves SIGHUP is still ignored even though "trap" shows nothing:
trap "" HUP
echo "TRAPs in parent"
trap
sh -c 'echo "TRAPs in child"; trap; sleep 5; echo "Still here. Traps: "; trap' &
child=$!
sleep 1
kill -HUP $child
echo 'Killed child'
Digging deeper into the sources of the busybox that is running on the Linux I found out that the shell in busybox is calling signal(SIGHUP, SIG_DFL); during init. This resets the sighup handler to default. So the shell itself and the program it runs (provided with -c ...) are running with default SIGHUP handler.
I see no code path that bypasses resetting the SIGHUP signal handler, so it seems I cannot achieve what I want with system().

Stopping own process

The concerned code is very huge and hence i am sorry i cannot post it here. The issue is:- I wrote a small program as follows:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
int main()
{
printf("\n Process id",getpid());
fflush(stdout);
if(kill(getpid(),SIGSTOP)!=0)
printf("\nError");
}
Upon running i get the following o/p:
Process id 2664
[1]+ stopped ./test_SIGSTOP
[Directory Path]$
Which is exactly what is expected. But in my actual program which i said is very huge...control comes to just above the kill call(I know it as I have print statements and fflushed them) and hangs without automatically stopping the process and appearence of the command prompt.
Would be gratefull for pointers.
Thank
You can attach a debugger to a running program and find out where/why it hangs. Also, the raise() function is more convenient to use. But first, use ps and inspect the process's flags to confirm its status (running / sleeping / stopped).

Resources