Hacks

From nsnam
Jump to: navigation, search

This page tries to keep a list of hacks people used to get rid of problems or in order to track them down.

C++ Hacks

Tracking calls to exit() and abort() methods

If ns-2 seems to quit early without any error message, chances are that the code hit an exit(), abort() method and no comment was printed.

Pedro Vale Estrela proposed the following approach to track this problem:

  1. in config.h undefine both exit() and abort()
  2. redirect both to a single macro called my_abort() which records the file and line number where it is called
  3. code the my_abort() function to display the line and filename parameters (with printf)
  4. and then cause a segmentation fault "by hand" (to stop execution in the c++ debugger like DDD)

Note: it seems that the independent utilities programs that are in /ns/indep-utils also include config.h, so they will require that you add "somewhere.cc" to the link part of the makefile.


Details and code:

config.h:

extern "C" {
	void my_abort(int line, char *file);
}

void my_abort(int line, char *file);

// step 1
#undef abort
#undef exit

//step 2
#define abort()   my_abort(__LINE__, __FILE__ )
#define exit(A)   my_abort(__LINE__, __FILE__ )





somewhere.cc:

void my_abort(int line, char *file)
{
        //step 3
	printf("___ ABORT ___ on file %s, line %d \n", file, line);

        //step 4
	assert(1==2);
}