setsid() without forking OR debugging forked process with gdb
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
setsid() without forking OR debugging forked process with gdb
I am trying to debug a server which must setsid() in order to run. This is necessary because it forks an arbitrary number of processes which raise(SIGSTOP) so that the server can set up shared memory, pipes, and make all forks before continuing. The server then calls kill(0, SIGCONT) to restart all of the processes. Normally I use a daemon call, but gdb loses track of the real process when it forks. Calling setsid without daemonizing doesn't work when running from a terminal, and hence from gdb. Is there a way around this? Thanks.
ta0kira
What I would do.... and this is after two seconds of thought so there may be a better way...
Put in an infinite loop in your program after the fork. Run it. Attach to it with the debugger (after it forks of course). Break the loop and debug away.
To deamonize properly, a program forks() twice. After each fork(), the parent exits. By default, gdb followes the parent process when forking. I think this is why you lose control over you parent in gdb. You can tell gdb to follow child after a fork with the gdb-command "set follow-fork-mode child".
So I think it might help to set a breakpoint after the daemon() call, tell gdb to follow the child and run you program.
So I think it might help to set a breakpoint after the daemon() call, tell gdb to follow the child and run you program.
Thanks. I think the breakpoint will have to be right before the daemon call, though, since it won't work after. Then I'll need one after so it doesn't follow the additional forks later on. I'll look into that.
In the meantime, I've solved all of my problems the way I have been for years: with printf/std::cout. That never seems to fail me. Plus, the server has quite a few logging points with extensive output.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.