Re: Compile time string literal substitution to external data file

From: muchan (usenet_at_usenet.usenet)
Date: 05/21/04


Date: Fri, 21 May 2004 10:43:44 +0200

Pieter wrote:

> Using TRACE type macros to output debug information at runtime is a powerful
> debugging technique.
> This does however have the drawback of leaving the string literals in the
> compiled binary, and these string literals may expose sensitive information
> e.g. function names etc.
> Although not a prime motivator, the strings also increase the binary size
> without any runtime value.
>
> E.g.
> TRACE("Some sensitive context text e.g. a function name %s\n",
> szSomeParameter);
> Where TRACE will resolve to some form of printf like debug output.
> Will result in the compiled binary containing the following string "Some
> sensitive context e.g. a function name %s\n".
> (snip)

>
> I want to use the compile time macros for file name, line number, and
> function name in my debug output statement.
> E.g. __FILE__, __LINE__, and __FUNCTION__:
> (snip)

Probably I didn't understand your question properly, since I don't see
too much difficulty for solving?

I'd put a macro and a function like:

// somewhere in .h
#ifdef DEBUG // some predefined flag of debug build and release build
#define DEBUGTRACE_ON 1
#endif

#ifdef DEBUGTRACE_ON
void DebugTraceFunc(const string& str, char* file, char* line, char* func);
#define DebugTrace(str) (DebugTraceFunc(str,__FILE__,__LINE__,__FUNCTION__))
#else
#define DebugTrace(str) {} // do nothing
#endif

// somewhere in .cpp
#ifdef DEBUGTRACE_ON
   void DebugTraceFunc(const string& str, char* file, char* line, char* func)
   {
     string msg("");
     //... format the msg
     Trace(msg.c_str());
   }
#endif

I didn't test the code, so there may need a modification, but the idea is
in relase mode (or DEBUG not defined) the macro makes nothing for compiler to see,
and in debug mode, it expands to a msg, with info you want to see in Trace,
and call to the function which is linked only in debug mode.

A little more work, you can add printf like vaiable numbers parameters,
or your developer may format the msg just before it like

const string format(char *fmtstring, ...); // defined somewhere else

... // inside the code

   string tracemsg = format("blablabla %s %d", some_str, some_int);
   DebugTrace( tracemsg );

Ha! in this last case blablabla will be in compiled binary...
Then you sould also put a DebugFormat macro beside DebugTrace macro...

#ifdef DEBUGTRACE_ON
#define DebugFormat(fmtstr,prm1,prm2,prm3,prm4) (DebugFormatFunc(fmtstr,prm1,prm2,prm3)) // 4 is 'enough, isn't it?
#else
#define DebugFormat(fmtstr,prm1,prm2,prm3,prm4) {} // do nothing
#endif

// somewhere in .cpp
#ifdef DEBUGTRACE_ON
   void DebugFormatFunc(char* fmtstring, ...)
   {
     //... format the msg
   }
#endif

You can integrate them into one macro and one function...

#define DFTrace(fmtstr,prm1,prm2,prm3,prm4) (DebugFormatTraceFunc(__FILE__,__LINE__,__FUNCTION__,fmtstr,prm1,prm2,prm3))

and user (programer call

DFTrace("blablabla %s %d", "somestring", some_int, 0,0); // 0,0 are filler,

Release build binary won't contain blablabla, in this case

Maybe I missed your point. Just hope it helps.

muchan



Relevant Pages

  • Re: Rebuilding functions at run-time
    ... (deftemplate foo () ... But, AFAIK, this can't be a macro, since it will be expanded at macro- ... Another option could be compile, ... (let ((string initial-string)) ...
    (comp.lang.lisp)
  • Re: DISFAVORED Was: name for 3 PICK finally?
    ... of someyhing you lose if you give up POSTPONE [COMPILE] etc. ... macro, that's less efficient than doing all that once and then just ... POSTPONE lets you do it and macros don't let you. ... require string parsing and dictionary look-ups when it executes. ...
    (comp.lang.forth)
  • Re: Macro to cause build failure if parameter not defined?
    ... this in a macro: ... What I want is a way to test if a feature flag is defined to 1 such ... compile flags, or they just typed it wrong. ...
    (comp.lang.c)
  • RE: do { } while (0) question
    ... #define MACRO ... How do you want to define KILLER, ... Does it compile? ...
    (Linux-Kernel)
  • Re: do { } while (0) question
    ... How do you want to define KILLER, ... perhaps mistakenly forgotten ';' after MACRO will not stop your example ... it will compile and do ...
    (Linux-Kernel)