Setting up a Debug flag during program execution

Good evening! I have a problem that I can't seem to find a simple solution for.

I have a program that has a boolean isDebugOn. If this bool is TRUE, debug packets are printed.

I would like the ability to toggle this boolean to TRUE or FALSE at will (whatever method is viable). I have tried setting up environmental variables and changing it during program execution (... does not work b/c shells do not inherit the new env var).

I tried thinking of using a keyboard input... but I can't have a cin waiting for input. The program must be still running it's main loop and still be able to toggle this debug boolean.

What is a viable solution, friends?
Somehow provide a means to pause the program by catching some keystrokes and then get the flag using cin.
It's hard to toggle program in real time,

now that we have GDB, why do you still want to implement this... Confusion
But it is necessary sometimes e.g. for opting to print traces while a critical program is running which can not be stopped. Traces can not be kept on all the time because they may take up large space on the hosting machine.
Last edited on
Thanks for the responses.

Just to clarify--- this is a program that establishes a socket connection with another machine. The program receives packets from the other machine, and I need a mechanism to toggle printing packets on the fly.

mgupta: About your response..... pausing the program by catching some keystrokes sound like a viable solution. Can you explain more? I can only think that when we push a key, the program stops abruptly (though this momentary stop may cause us to lose some packets).
What do you mean by "printing packets"? Are you displaying their contents on the screen?

Perhaps you could find the size of the file attached to standard input. If there is a character in there, you read it. If it's, say, a 'p' character then you set the boolean to false.
But he wants to switch this printing depending on user input.

@cdel :
I dont have knowledge about coding for catching keystrokes, but I guess that would consist of running a loop and listening for keystrokes.
you could use the _kbhit() function from the conio.h header.
http://msdn.microsoft.com/en-us/library/58w7c94c(VS.80).aspx
If your program is already signal-aware, just catch SIGHUP (or SIGUSR1, or whatever signal you want)
and toggle the flag in the handler for it.

Then "kill -SIGHUP <pid-of-my-process>" (or whatever signal you chose) can be used to flip the debug on/off.
@christname: when I say "printing packets," I mean printing packet contents (data inside the packet). I am displaying the contents on stdout.

@Moooce: I'm on a POSIX system.

@jsmith: Your idea sounds good. The program (luckily) already handles several signals. I'll give SIGHUP a look to see if I can do something with it. Any good tutorial sites for learning POSIX signals?
Whenever a signal is caught by a program, I read somewhere that program execution is halted. If for example I had some software listening in for packets ... and a signal is caught, then would we lose some packets in the process during that momentary system halt?
No. Your program isn't handling receiving the packets, your program basically just receiving from a buffer that the OS is keeping. Otherwise you might even lose packets while trying to receive a different one.
well, POSIX signal mechanism is strongly depended on each specific OS, you'd better check the document with you OS.

A little suggestion, why not try condition compile macro + wireshark?
condition compile macro would just be usefull at the time of compilation... not at the run time...
@cdel OK, I know nothing about POSIX:-( Would this help though, it's for UNIX?
http://www.pwilson.net/kbhit.html
Last edited on
1
2
3
4
5
6
7
8
9
10
11
static volatile sig_atomic_t debugMode = 0;

void handle_sighup( int ) {
    debugMode = 1 - debugMode;
}

int main() {
    signal( SIGHUP, &handle_sighup );
}

// Then just check if( debugMode ) // print packet 


is all you need.


EDIT: In your case, if debugMode needs to be accessible to other source files, remove the "static".
Last edited on
Yup! Thanks jsmith. That's exactly what I did! Thanks for the help. I got it working using SIGUSR1 and just performing a `kill -SIGUSR1 `pgrep 'fileName'`

Topic archived. No new replies allowed.