0

I have small question. Is it possible in C/C++ to put bit of codes that would interact a bit more with GDB ?

Let say I have a function like

void gdb_print(const char*);

This would print information in gdb while it executes! If it's not possible it would be awesome. Would be simple to track some information, and faster in some way!

I need something like this, because we're writing some plugins, and info from cout or cerr are not going to the console at all. So it would be something discrete. Also, could add some stuff like:

#ifdef __DEBUG__
  #define debug_msg(x) gdb_print(x)
#else
  #define debug_msg(x)
#endif

If it doesn't exist, let me know what you think about this!

widgg
  • 1,358
  • 2
  • 16
  • 35
  • You could write into syslog. Wouldn't that help? – vrk001 May 11 '12 at 18:00
  • I tried that too. But it just that something that would interact with gdb would be nice too. To put instruction in the code specific to gdb to help debugging. If it doesn't exist, I think someone should work on this. Could be very useful. Or simply give even more control over the debuggin process. – widgg May 11 '12 at 18:23
  • It sounds like you want the equivalent of Windows' `OutputDebugString`, which unfortunately doesn't exist. [This question](http://stackoverflow.com/questions/417745/os-x-equivalent-to-outputdebugstring) gives a useful answer for Mac OS X, though I don't think it translates directly to other POSIX systems. – Adam Rosenfield May 12 '12 at 03:07

3 Answers3

1

try redirecting the stderr and stdout to a file using freopen. see this

This is a sample code to redirect stdout to a file in runtime:

/* freopen example: redirecting stdout */
#include <stdio.h>

int main ()
{
  freopen ("myfile.txt","w",stdout);
  printf ("This sentence is redirected to a file.");
  fclose (stdout);
  return 0;
}
Community
  • 1
  • 1
Kamyar Souri
  • 1,871
  • 19
  • 29
1
static int gdb = 0;
void gdb_print(char const * msg) {
    if(gdb) printf("\tGDB: %s\n", msg);
}

When you load your program up in gdb, set a breakpoint in main, then set gdb to a non-zero value. This isn't the cleanest solution (and certainly not automated) but I think it'll give you what you're looking for. Be sure to use the per-processor to remove the calls in non-debug builds (no sense in having all those extra compares that'll never evaluate to true).

Stephen Newell
  • 7,330
  • 1
  • 24
  • 28
  • I just had another thought. You can make an alias to gdb that sets some environment variable and have gdp_print check for that instead of a local static. You'd have to invoke using your gdb alias, but this would eliminate the mandatory break and set. – Stephen Newell May 11 '12 at 19:05
1

I need something like this, because we're writing some plugins, and info from cout or cerr are not going to the console at all.

You can always write to console with:

FILE *console = fopen("/dev/tty", "w");
if (console != NULL) fprintf(console, "Your debug message\n");

I don't know of a method to write specifically to the terminal where GDB is running (which could well be different terminal from which the program itself was invoked).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • but your solution will print in the terminal that called the program ? If it is, that would be a clean solution because for my debugging purposed, that terminal is only launching the app! – widgg May 14 '12 at 13:33
  • It's almost perfect... for a reason I don't understand, I have to reload the plugin for the messages to show up! But I think that's a Softimage issue because I had to do the same thing to just have my normal messages print in the script interface. Thanks again. – widgg May 14 '12 at 15:00