using STDOUT from within gdb - c

I have a function that pretty prints a data structure, its function prototype is:
void print_mode(FILE *fp, Mode *mode);
the FILE* allows you to redirect the output to anywhere you want, e.g. stdout, stderr, a file etc. Mode is the data structure
I am trying to call this function from within gdb and want the output to be directed to the gdb console window, stdout?
I have tried:
(gdb) p print_mode(STDOUT,fragment_mode)
No symbol "STDOUT" in current context.
(gdb) p print_mode(stdout,fragment_mode)
$17 = void
neither of which work
any ideas how i can get the output of the function to display in the gdb console?
should add - I am using gdb within emacs 24.2.1 under linux

STDOUT seems to be macro, which is not know to GDB, as handled prior to compilation by the pre-preprocessor.
Using stdout should do the job.
However the function print_mode() simply does not seem to print out anything.
In terms what's being printed to the console by the program being debugged, GDB's commands printand call should not make a difference.
For details on this you might like to read here: https://sourceware.org/gdb/onlinedocs/gdb/Calling.html
An issue might be that stdout by default is line buffered, so output would not occur before detecting a linefeed and print_mode() perhaps does not send a linefeed (\n).
To test this just use stderr as output file, as the latter isn't buffered:
p print_mode(stderr, fragment_mode)

Oh dear - silly mistake. You're right, stdout does do the job.
I forgot that having upgraded from emacs 23 to 24, the way gdb works has changed in as much as it now opens a separate buffer *input/output of program-name* to which it redirects the output of the program being debugged. In the prior version of emacs it was all displayed in the same, single gdb buffer.
So my second attempt was actually working, I was just looking in the wrong place so didn't see the output

Related

Writing in the executable while running the program

I'm writing a C program and I would like to be able to store data inside the executable file.
I tried making a function to write a single byte at the end of the file but it looks like it can't open the file because it reaches the printf and then gives "segmentation fault".
void writeByte(char c){
FILE *f;
f = fopen("game","wb");
if(f == 0)
printf("\nFile not found\n");
fseek(f,-1,SEEK_END);
fwrite(&c,1,sizeof(char),f);
fclose(f);
}
The file is in the correct directory and the name is correct. When I try to read the last byte instead of writing it works without problems.
Edit: I know I should abort the program instead of trying to write anyway but my main problem is that the program can't open the file despite being in the same directory.
There are several unrelated problems in your code and the problem you're trying to solve.
First you lack proper error handling. If any function that can fail (like e.g. fopen) fails, you should act accordingly. If, for example you did
#include <error.h>
#include <errno.h>
...
f = fopen("game","wb");
if ( f == NULL ) {
error(1,errno,"File could not be opened");
}
...
You would have recieved an useful error message like
./game: File could not be opened: Text file busy
You printed a message, which is not even correct (the file not beeing able to be opened is somthing different, than not beeing found) and continued the program which resulted in a segmentation fault because you dereferenced the NULL pointer stored in f after the failure of fopen.
Second As the message tells us (at least on my linux machine), the file is busy. That means, that my operating system does not allow me to open the executable I'm running in write mode. The answers to this question lists numerous source of the explanation of this error message. There might be ways to get around this and open a running executable in write mode, but I doubt this is easy and I doubt that this would solve your problem because:...
Third Executable files are stored in a special binary format (usually ELF on Linux). They are not designed to be manually modified. I don't know what happens if you just append data to it, but you could run into serious problems if your not very careful and know what you're doing.
If you just try to store data, use another plain and fresh file. If you're hoping to append code to an executable, you really should gather some background information about ELF files (e.g. from man elf) before continuing.

c code is inexplicably skipping lines

When I compile this small program for some reason the "printf("done")" line is not executing and I cannot figure out why. There was originally an if block before my print statement and that also was not executing. I tried running it through gdb and stepping through it. It found the line but it did not execute it. I am trying to compile it using gcc 4.7.2 and I'm using gedit 3.4.2 as my text editor.
Here is the command I'm using to compile it.
gcc teststuff.c -o test
And here is the code
#include <stdio.h>
void testmethod()
{
int sign = 1;
printf("hello\n");
printf("%d\n",sign);
printf("done");
}
main(void)
{
testmethod();
return 0;
}
Anybody have any ideas why it is skipping that part of the code? Thanks!
Put a line break at the end.
printf("done\n");
Standard output is often line-buffered, that means that the data you write only gets flushed to the screen once you finish a line (unless you explicitly flush it, such as with fflush()). However, it should be flushed when the program exits (but not necessarily before the program exits).
Also, depending on your shell, if a program writes data without a linebreak you might get your command prompt on top of the output:
my-computer ~/projects/my-program $ ./a.out
hello
1
donemy-computer ~/projects/my-program $
This is because your program finishes and ends before the Done line gets the chance to be printed to the console.
Console output is generally buffered. The string "done" stays in the buffer of stdout until the stream is flushed. You can do it manually by calling fflush(stdout), or by adding \n to the end of the "done" string. Printing the end-of-line character causes console stream to empty the buffer onto the screen (unless you set a special output mode that lets \n to be buffered until an explicit flush).

printf not printing on console

I’m getting started in the C language. I am using eclipse (juno) as my IDE and installed CDT plugin. I have also unpacked mingw64 (GCC Compiler). I wrote a very simple program to see if it works. This is my code:
#include <stdio.h>
int main()
{
int age;
printf("Hello, please enter your age:\n");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}
The problem is that the output buffer is filled with the string value of the first printf but does not output it to the console. I have to enter a number, and only then the buffer pours all the data to the console so I see the console something like this:
1
Hello, please enter your age:
Your age is 1
instead of what is expected that is:
Hello, please enter your age:
1
Your age is 1
Now, I found that I can use fflush(stdout) after the first printf but I don't think that this solution is elegant and even necessary. Any ideas on how I can overcome this?
EDIT - because I'm learning this in my university, I can't use anything that wasn't learned in the course so I can only use printf and scanf
NEW EDIT - I think I have found an explanation for this. As I said, I am outputting to the console view inside Eclipse. The strange thing is that if I compile and run the program from the command line of Windows, I get the wanted result. Therefore, I think that eclipse is actually writing the output to a file and presenting it in the console window. How can I force eclipse to open a real command line window in my run configurations?
Output is buffered.
stdout is line-buffered by default, which means that '\n' is supposed to flush the buffer. Why is it not happening in your case? I don't know. I need more info about your application/environment.
However, you can control buffering with setvbuf():
setvbuf(stdout, NULL, _IOLBF, 0);
This will force stdout to be line-buffered.
setvbuf(stdout, NULL, _IONBF, 0);
This will force stdout to be unbuffered, so you won't need to use fflush().
Note that it will severely affect application performance if you have lots of prints.
Apparently this is a known bug of Eclipse. This bug is resolved with the resolution of WONT-FIX. I have no idea why though. here is the link:
Eclipse C Console Bug.
You could try writing to stderr, rather than stdout.
fprintf(stderr, "Hello, please enter your age\n");
You should also have a look at this relevant thread.
Try setting this before you print:
setvbuf (stdout, NULL, _IONBF, 0);
As others have pointed out, output can be buffered within your program before a console or shell has a chance to see it.
On unix-like systems, including macs, stdout has line-based buffering by default. This means that your program empties its stdout buffer as soon as it sees a newline.
However, on windows, newlines are no longer special, and full buffering is used. Windows doesn't support line buffering at all; see the msdn page on setvbuf.
So on windows, a good approach is to completely shut off stdout buffering like so:
setvbuf (stdout, NULL, _IONBF, 0);
Add c:\gygwin\bin to PATH environment variable either as a system environment variable or in your eclipse project (properties-> run/debug-> edit)
In your project folder, create a “.gdbinit” text file. It will contain your gdb debugger configuration
Edit “.gdbinit”, and add the line (without the quotes) : “set new-console on”
After building the project right click on the project Debug > “Debug Configurations”, as shown below
In the “debugger” tab, ensure the “GDB command file” now points to your “.gdbinit” file. Else, input the path to your “.gdbinit” configuration file :
Click “Apply” and “Debug”. A native DOS command line should be launched as shown below

Where would a driver "output" to if using printf?

If I were to create a driver, which, say hooks the windows function for opening a file. If in my driver I told the hook to printf("something"), when the driver is switched on and I opened a file, where would printf display the text to?
If you want to output text from a driver for debugging and experimental purposes, use DbgPrintEx. The output can be viewed via Sysinternals DebugView or a debugger.
printf is written to the stream stdout which is declared in stdio.h. It is opened the first time you touch one of the standard streams stdin, stdout, stderr. The standard streams stdin, stdout, and stderr are macros that call a stdio library function which opens the streams and returns an array those streams. The macro definitions index the array to get the right stream out. If the application has no console, the output goes to the "null" device.
You can view the debug output of your driver (compiled with debug config)
using DbgView
or WinDbg utility available on microsofts website
also to give print statements you could use following functions defined in wdm.h
DbgPrint(...)
DbgPrintEx(...)

Using libunwind on HP-UX and getting stacktrace

I have a C application that is executing in an HP-UX environment and I need to get the stacktrace.
I am attempting to use U_STACK_TRACE, but output to stderr is going somewhere else and I need it printed to a string.
How can I do this?
I.E. How can I take the output from U_STACK_TRACE and put it in a string instead of it being written to stderr.
U_STACK_TRACE() prints a formatted stack trace to standard error. _UNW_STACK_TRACE() produces a formatted stack trace on the output stream indicated by parameter out_file. The stream must be a writable stream for output to be produced.
So, open a file using fopen() and call _UNW_STACK_TRACE() instead of U_STACK_TRACE().

Resources