Mobile Ansicht | Klassische Ansicht

Institut für Mathematik - Local Info


Debugging: Monitoring variables and memory addresses in gdb/ddd

Problem: You would like to stop the debugger whenever a variable or memory address is accessed (read and/or write). This is not possible with breakpoints where you specify the location where the program execution is stopped. But you are looking for places in your code where this happens!

This is possible in gdb (and the graphical frontend ddd) via so called watchpoints.

First set a breakpoint somewhere before your point of interest ("break filename:line") and start your program ("run" or "run command_line_arguments"). As soon as your breakpoint is hit you type "watch variablename" and let the program continue ("cont"). As soon as the value of your variable is altered the debugger stops the execution right after the line where it happened. Now you can inspect whatever you're interested in. To continue execution just type "cont" again.

This procedure won't work if you want to monitor e.g. a specific location in an array or a C++-reference. It also fails for variables that are not directly visible in the scope where an access or change occurs (e.g. variables on the heap) as it often happens in more complex codes.

In this case you have to monitor the variables memory address. To obtain the address let gdb print your variable's address ("print variable" or "print &variable"). The output is of the form "$n = ...". Now you can type "watch $n" and proceed execution with "cont".

You can even distinguish the cases:

  • stop whenever the variable value changes ("watch")
  • stop whenever the variable value is read ("rwatch")
  • stop whenever the variable value is either read or written ("awatch")

Although ddd is more comfortable than plain gdb typing the "watch" commands is a lot faster.

Note: Watchpoints don't seem to work in multithreaded code.

w/ top: Load, JCPU, PCPU

What are JCPU and PCPU (reported by the "w" command), and what does the "load average" mean?

The JCPU is the CPU time used by the job (all processes and their children on that terminal), and PCPU is the CPU time used by the currently active processes.

The load average numbers give the number of jobs in the run queue, averaged over 5, 30, and 60 seconds. On a workstation, a load average of 2 to 5 is not that severe, but you normally see the load averaging less than one. On a larger machine like CCWF or UTS, the load normally stays around 3 to 5, but gets peaks of 10 to 15. The load average numbers give you a good idea of how busy a computer is and how busy it was.

There are several other commands that report usage statistics. For example, the command vmstat will report virtual memory statistics, including the number of jobs in the run queue.

For more information, type:

man w
man vmstat