I'm a C newbie using gcc. I wrote a program but a part of it outputs gibberish onto the terminal. Its weird because identical parts of the program work correctly in another program I wrote . My program uses c99 + some POSIX headers. It compiles with no errors/warnings even though I have included all the flags I could get hold of.
I do not understand how to use -g so can anybody recommend a C debugger (that speaks english, lol)? At this point I am even willing to switch to Windows...
Yes, I have heard and used it before with no success. I'm trying to debug a CGI program written in C. It seems to segfault when using getenv. I tried running the program with Firefox and using sleep(30) to make time to attach PID but it still segfaults on this line.
Any ideas ?
char DOCUMENTROOT[128] = "";
strcat(DOCUMENTROOT, getenv("DOCUMENT_ROOT")); // This line is ok, right ? I mean it is far less than 128 chars long.
Actually, the problem is using "DOCUMENT_ROOT" because it is apache specific. Using "PATH" works.
So how could I debug my CGI program using apache and using GDB ??? (Like I said, running my CGI w/ Firefox and attaching its PID to GDB does not solve the problem.)
Seems I will have to temporarily substitute the getenv's with strings.
AFAIK, you can't directly pipe to a program being debugged. So Option 1 is to just paste the input with the mouse, if available. Option 2 uses a FIFO, description can be found here
Writing CGI scripts in C is kind of clumsy. You can artificially create the environment your application expects when running it in GDB.
Create the environment variables for the application that apache would usually set up. You would only need to create the environment variables that your application requires. Thus, when you run GDB make an environment variable to simulate apache.
$ export DOCUMENT_ROOT="foo"
$ gdb myscript.cgi
The output will be ugly because it will be spitting out HTML.
Indeed. You should do as Corona688 has said.
char *p;
if( !( p = getenv( "DOCUMENT_ROOT" ) ) ) {
// Do something
}
else
strcat(DOCUMENTROOT, p);
You should also make sure p is not larger than DOCUMENTROOT or you shall cause a buffer overflow.