LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2007, 07:57 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-08-2007, 08:29 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.

Last edited by jtshaw; 08-08-2007 at 08:30 PM.
 
Old 08-09-2007, 04:16 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Some doc's about this:
http://www.unixguide.net/unix/programming/1.7.shtml
http://sourceware.org/gdb/current/on...b_5.html#SEC28
http://sourceware.org/gdb/current/on...b_5.html#SEC25
(the last one may help you try jtshaw's idea)
 
Old 08-09-2007, 06:22 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Hko
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DISCUSSION: Debugging with GDB (Part 1) Matir LQ Articles Discussion 4 11-09-2016 10:33 PM
using GDB for remote kernel debugging vibhooti Linux - Software 0 04-23-2007 01:17 AM
SSH forked process shozmo Linux - Newbie 5 06-23-2006 02:49 AM
Help debugging with gdb and core files moon.es Programming 1 05-28-2005 04:58 AM
how to connect forked process to a specific terminal pcweirdo Linux - Software 2 02-22-2005 03:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration