Debugging ns-2

From nsnam
Jump to: navigation, search

In order to determine why ns-2 does not work as you expect or or why it crashes, you have to use a debugger like gdb to figure out what happened.

If you have never worked with a debugger before, it might look a little bit difficult at first but it's an absolute necessity for any serious programmer to master it. People on the mailing list won't be willing to help you unless you have not tried seriously to fix the problem yourself.

To get you started, there are some excellent tutorials on the Internet for both gdb (like [1], [2]) and ddd, a GUI to gdb (PDF slideshow). Install either of them, make sure you have enabled debugging symbols in ns-2 (see the relevant part in the user tips), and re-compile everything. Afterwards, start ns through the debugger, pass your tcl-script as argument using set args <path to script> and either choose a reasonable breakpoint or let ns-2 crash and watch the backtrace, depending on whether after adding your code the simulator just does not work appropriately or crashes, respectively. When you managed to narrow down the responsible piece of code, use the debugger's capabilities to investigate even further (print, watchpoints, change variable values, etc.) in order to fix the bug(s).

What makes things a little more complicated is the fact that ns-2 not only uses C++ but OTcl as well. When the simulator crashes and leaves you with an OTcl backtrace, try to figure out what happened by watching at that. If that does not suffices and there is no valuable C++ backtrace, take a look at the ns-2 debugging page to learn how to include and use the Tcl debugger. Since it's not as sophisticated as gdb, you might want to consider setting debug 1 at different points within the Tcl simulation script first and see from what point on it crashes. Apart from that, the page tells you how to determine a node's (string's) name from C++, jump into Tcl space and use the tcl debugger from there before possibly returning to C++ space.

If you require further help on debugging, please consider some of the links under Other tips from users: Pedro Vale Estrela has provided some additional hints and tools, particularly useful with OTcl.

Setting up a debugger environment

Due to the duality of C++ and OTcl in ns-2 debugging can be quite a hassle since you need two debugging systems to keep track of the whole process.

C++

You can use any C++ debugger for ns-2. gdb or ddd are some options for Linux.

Beside that there is always the options of printfs and other C++ Hacks.

OTcl

You can find an overview on tcl debugger installation here in the wiki: Installing_tcl-debug_for_ns-allinone-2.31 Pedro Vale Estrela has a website with step by step instructions on how to set up an OTcl debuuging environment: http://tagus.inesc-id.pt/~pestrela/ns2/ns2_debugging2.html