30.01.2013 Views

TotalView Users Guide - CI Wiki

TotalView Users Guide - CI Wiki

TotalView Users Guide - CI Wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Defining Eval Points and Conditional Breakpoints<br />

In many cases, correcting an error means that you will do both operations:<br />

you use a goto to branch around incorrect lines and add corrections.<br />

Branching Around Code<br />

The following example contains a logic error where the program dereferences<br />

a null pointer:<br />

1 int check_for_error (int *error_ptr)<br />

2 {<br />

3 *error_ptr = global_error;<br />

4 global_error = 0;<br />

5 return (global_error != 0);<br />

6 }<br />

The error occurs because the routine that calls this function assumes that<br />

the value of error_ptr can be 0. The check_for_error() function, however,<br />

assumes that error_ptr isn’t null, which means that line 3 can dereference a<br />

null pointer.<br />

You can correct this error by setting an eval point on line 3 and entering:<br />

if (error_ptr == 0) goto 4;<br />

If the value of error_ptr is null, line 3 isn’t executed. Notice that you are not<br />

naming a label used in your program. Instead, you are naming one of the<br />

line numbers generated by <strong>TotalView</strong>.<br />

Adding a Function Call<br />

The example in the previous section routed around the problem. If all you<br />

wanted to do was monitor the value of the global_error variable, you can<br />

add a printf() function call that displays its value. For example, the following<br />

might be the eval point to add to line 4:<br />

printf ("global_error is %d\n", global_error);<br />

<strong>TotalView</strong> executes this code fragment before the code on line 4; that is,<br />

this line executes before global_error is set to 0.<br />

Correcting Code<br />

The following example contains a coding error: the function returns the<br />

maximum value instead of the minimum value:<br />

1 int minimum (int a, int b)<br />

2 {<br />

3 int result; /* Return the minimum */<br />

4 if (a < b)<br />

5 result = b;<br />

6 else<br />

7 result = a;<br />

8 return (result);<br />

9 }<br />

Correct this error by adding the following code to an eval point at line 4:<br />

if (a < b) goto 7; else goto 5;<br />

<strong>TotalView</strong> <strong>Users</strong> <strong>Guide</strong>: version 8.7 369

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!