Source Mode May Be Unavailable After Switching Threads

ID Number: Q72179

3.00 3.10 3.11 3.12 3.50

OS/2

Summary:

When debugging a multithreaded C program in CodeView, you can change

between executing threads by using either the thread command (~) or

the Thread command on the Run menu. If you switch to a thread right

after it has been created with a call to _beginthread(), CodeView may

display assembly code instead of the C source code. In this situation,

any attempt to switch back to source mode will result in the following

CodeView message:

Source not available for source mode

The inability to display the code in source mode results from

selecting the thread while its still executing the thread

initialization code. Since there is no symbolic information associated

with the actual _beginthread() code, CodeView cannot display source

code at this time.

You can step through the assembly code in order to reach the first

line of source code associated with the thread, but this is rather

inconvenient. A more direct approach is to "go" to the function in the

thread or to set a breakpoint on a source line near the beginning of

the thread and then execute to it.

More Information:

To observe the behavior described above, do the following:

1. Compile and link the sample program below and load it into

CodeView.

2. Trace (F8) through the code until the _beginthread() statement has

been executed.

3. From the Run menu, choose Thread and switch to the thread just

created (002). (The prompt in the command window will now appear as

"002>" to indicate that the current thread is thread 2.)

The source window will now be displaying assembly code. The only way

to get back to your source is to step (F10) until it reappears. This

will occur on the first executable statement of the second thread,

which is the opening brace ({) of the thread2() function.

To be able to switch directly to the newly created thread, yet avoid

having to step through the assembly code, enter a command into the

command window to "go" to the function specified in the _beginthread()

call. In this particular case, you would type the following command in

the command window:

g thread2

The program will then break at the beginning of the function

specified.

An alternative method is to set a breakpoint on the first executable

line of the thread to be observed. With the sample code below, this

would be done as follows:

1. Load the program into CodeView.

2. Set a breakpoint on the first executable line of the thread to be

observed, which is the opening brace of the function thread2().

3. Trace (F8) through the code until the _beginthread() statement has

been executed.

4. From the Run menu, choose Thread and switch to the thread just

created (002).

5. Do a go (F5) to execute to the breakpoint. This will make the first

line of executable code in the thread the next statement to be

executed.

Note: You could also press the F5 key to run immediately after

performing step 2 above, since this will also place you in the

thread at the breakpoint.

Sample Code

-----------

/* Compile options needed: /MT /Zi /Od

*/

#define INCL_DOS

#include <os2.h>

#include <stdio.h>

#include <process.h>

void thread2(void);

void main(void)

{

printf("Starting main.\n");

_beginthread(thread2, NULL, 4096, NULL);

printf("New thread started.\n");

while ( 1 )

{

printf("Inside main.\n");

DosSleep(500L);

}

}

void thread2(void)

{

while ( 1 )

{

printf("Inside thread2.\n");

DosSleep(500L);

}

}