5 Advanced Debugging Techniques

Ladebug provides advanced debugging techniques for debugging your multiprocess and multithread applications.

This chapter explains how to:

For more information about multithreaded application debugging, see Chapter 19. For more information on multiprocess application debugging, see Chapter 20.

5.1 Displaying and Selecting Available Processes and Threads

The Source View Context Panel contains menu buttons that list processes and threads. It allows you to change the process or thread context by selecting any process or thread on the menu and bringing it under debugger control.

To select a process or thread, pull down the Process or Thread menu button and select the process or thread you want. The contents of the Source View and the Call Stack and any displayed optional views are updated to reflect the current context.

For multithreaded applications, when you select a particular process, the thread option menu button then displays the thread list of the current process.

5.2 Attaching and Detaching a Process

To attach to a process not currently under debugger control, choose Command:Attach to Process in the Main Window. This displays a Process Selection dialog box that shows you the list of active processes to which you have access (Figure 5-1).

Figure 5-1 Process Selection Dialog Box

Select the process you want to attach to. The Source View reflects the new process. If you have optional views displayed, they will be updated to reflect the new process.

If no breakpoint is set in the process to which you are attaching, the process will run to completion. However, you can set the $stoponattach variable to stop the process right after the debugger attaches to it by default. For more information, see Part V, Command Reference.

The Source View will not reflect the new process if the debugger cannot find the path to the actual source file. See Section 4.1.3 for information about specifying the location of source files.

You can also attach to a process using the load command at the command prompt. The views are not updated until the run command is executed. If a break is not set, the process will run to completion. Use the stop in or stop at command to set a break. For more information, see Section 9.13.

To detach a process, choose Commands:Detach Process in the Main Window. This displays a dialog box showing all the current processes under debugger control.

Click on the desired process to remove it from debugger control. If the process is current (displayed in the Source View and optional views), the process will run to completion and the views are reinitialized (blanked out). If the process you detach is not current, it will run to completion and the current process will continue to display in the Source View.

The views do not close automatically. To close the views, use the Views menu or exit the debugger.

5.3 Debugging a Multithreaded Application

This section describes how to select among several threads in a DECthreads multithreaded application and change the current thread context.

5.3.1 Setting the Thread Mode

The debugger thread display defaults to DECthreads if the application is multithreaded and uses DECthreads. Otherwise, the $threadlevel is set to native.

For more information on identifying and setting the thread mode, see Chapter 19.

5.3.2 Steps for Debugging a Multithreaded Application

This section shows how to debug a single process multithreaded application.

The general steps for debugging a multithreaded application are as follows:

  1. Start the application as described in Chapter 3.

  2. Pull down the Thread menu. The Thread menu displays the current thread on top. If the application contains native threads, the location for each thread displays next to each thread. If the application contains DECthreads, only the thread number displays.

  3. Select a different thread by clicking on the thread you want to debug. (The Source View and the Call Stack are updated.)

Ladebug displays the current thread in the Source View.

The Register and Instruction Views show information pertaining to the selected thread. The Breakpoint View does not change when the thread is changed because it displays breakpoints for all processes.

5.4 Debugging a Multiprocess Application

This section describes how to use the debugger window interface to debug a multiprocess application. It presents an example application that forks a child process and execs a program.

Ladebug provides predefined debugger variables that you can set in order to debug multiprocess applications that fork and/or exec. In the example in Figure 5-2, the debugger variables $catchforks, $catchexecs, and $stopparentonfork have been set from the prompt as follows:

(ladebug)set $catchforks=1
(ladebug)set $catchexecs=1
(ladebug)set $stopparentonfork=1

Setting $catchforks to 1 instructs Ladebug to notify the user when a program forks a child process. Setting $catchexecs to 1 instructs Ladebug to notify the user when a program execs. Setting $stopparentonfork to 1 stops the parent process when a program forks a child process.

For more information on debugger variables, see Section 20.7.1. For different scenarios in which these variables are used, see Section 20.7.2 and Section 20.7.3.

The general steps for debugging a multiprocess application are as follows:

  1. Start the application as described in Chapter 3.

  2. Pull down the Process menu. The Process menu displays the current process on top. The image name for each process displays next to each process.

  3. Select a different process by clicking on the process you want to debug. (The Source View and the Call Stack are updated.)

Figure 5-2 Multiprocess Application

  1. In Figure 5-2, a parent process (200) forks a child process (201).

  2. When the parent process forks, the Source View shows that the parent process has stopped.

  3. The child process is selected from the process menu pulldown (process 201).

  4. Entering the cont command continues running the child process which execs the program factorial.

You can continue debugging the child process or return to the parent process by selecting it in the process menu pulldown. When the process has finished executing, a message displays that process has exited and the Source View reinitializes (blanks out).