Teen Programmers Unite  
 

 

Return to forum top

unix GNU C debuging

Posted by rendar [send private reply] at December 10, 2002, 03:29:34 PM

Hello!
Is anybody knows where can i find information about debuging C programms.
For example: i have daemon that forks child for incoming tcp request, these cilds "Segmentation fault"'s very rarely,
and there no other posibility to found kludge in code except viewing debuging information. I can't use gdb for debuging it, because there are no core files after thees critical errors.

Posted by unknown_lamer [send private reply] at December 10, 2002, 03:50:41 PM

Run "ulimit -c unlimited" to enable the dumping of core files. Actually, you should do that in your program using ulimit() or setrlimit(). I suppose the best solution is to use a wrapper shell script:

#!/bin/bash

ulimit -c unlimited
your_program


Just wrap your program in a script like that named "debug_yourprog" or something. Then only it will leave coredumps and you can open then with gdb (or a nice frontend like DDD).
Posted by rendar [send private reply] at December 10, 2002, 04:43:09 PM

thanks a lot!
another relative question - is it possible to write/compile
C program so, that it cause printing verbose debuging information in stderr under critical condidions ?

Posted by Neumann [send private reply] at December 10, 2002, 05:03:50 PM

Perhaps you could attach a gdb session to your program running as a daemon and make it crash. I don't know how it would work. unknown_lamer sure is better than me with Linux tools.

Posted by unknown_lamer [send private reply] at December 10, 2002, 09:03:26 PM

You can use assert.h and the NDEBUG macro and do


#ifndef NDEBUG
fprintf (stderr, "stuff");
#endif



This style is discouraged, but it works if you recompile the program between debugging cycles.

It would probably be better to have a configuration variable that let you set debug printing on and off at run time e.g. like Bobot++ does. e.g. "your_program --debug" would print debugging information. Since it appears that your program is a daemon, you may want to look at using the syslog functions to log to the system logs. A simple example:


#include <stdio.h>
#include <string.h>

int debugging = 0;

int main (int argc, char *argv[])
{
  if (!strcmp (argv[1], "--debug")
    debugging = 1;

/* code */
}

void some_func ()
{
  if (debugging)
    fprintf (stderr, "Debugging information");

   some_call_that_sets_errno ();
   if (errno != EOK)
     perror ("Some error happened in some_func ()");
}



If the user ran the program as "some_program --debug" then debugging would be set to 1 and then every function that had debug output would print it. You should use getopt or agrp instead of strcmp (it was just a stupid example anyway).
Posted by rendar [send private reply] at December 11, 2002, 03:08:01 AM

Neumann's answer very close to my question, under "critical conditions" i mean errors like signal SIGSEGV receiving, (Segmentation fault), etc. Debug information i need - number of line generated critical error, hope it would be anough

Posted by unknown_lamer [send private reply] at December 11, 2002, 12:57:51 PM

I don't know how to get the line number that generated the error in the debug output, but if you enable core dumps you should be able to attach the debugger to it to get the line number where the segfault occured. To print anything out, just use a signal handler that catches the signal you want and then print anything. You could even choose to ignore the segfault and continue running the program, although that usually doesn't work :) (wmaker uses a SEGV handler to ask you if you want to relaunch it).


#include <signal.h>

void seg_handler (int signal)
{
  fprintf (stderr, "Seg Fault!\n");
  abort (1);
}

int main (int argc, char *argv[])
{
  signal (SIGSEGV, seg_handler);
  /* other stuff ...*/

  return (0);
}


signal is ANSI C, POSIX provides the more robust (but more difficult to use) sigaction.
Posted by Mike_L [send private reply] at December 22, 2002, 01:57:17 PM

I like to use assert. The following program demonstrates the use of assert:

#include <assert.h>
void main() { assert( 0 ); }

Compile and run the program:
$ cc -g test.c -o test
$ ./test
assertion "0" failed: file "test.c", line 2
Abort (core dumped)

I use assert() to check data that is supplied to functions. Here is a snippet from one of my projects, ascorbic:

int Assembly_Statement( struct Ascorbic *asc, struct Particle *particle ) {
assert( asc );
assert( asc->out );
assert( particle );

Hope this helps!

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.