Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Watchpoints in Visual Studio

Watchpoints in Visual Studio

What are Watchpoints ?

Watchpoints are a special kind of breakpoint available when debugging native COBOL code. Rather than being associated with a specific line of code in your application they are associated with a COBOL data item. They allow you to break every time a data item's value changes. Without watchpoints you would have to set a breakpoint on each line where the item, or any redefinition of the item, was used in order to monitor changes to that item. In a simple program that may not be a problem but for a large, complex application that would not be practical as it may not be obvious where the item is used.

Setting a watchpoint

Let's assume that in the following program we want to track when the value of item2 changes.

       identification division.
       program-idProgram1.

       environment division.
       configuration section.

       data division.
       working-storage section.
       01 items.
         03 item1.
           05 item1-x pic x occurs 2.
         03 item2 pic 99.
         03 item2-redef redefines item2 pic xx.
         03 item3 pic zzz9.

       procedure division.
           display "Starting watchpoint test"
           initialize items
           display "about to modify item2"
           move 12 to item2
           display "about to modify item2-redef"
           move "99" to item2-redef
           display "about to modify item2 via reference modification"
           move "1234" to item1(1:4)
           display "end of test"
           goback.

       end program Program1.

To set the Watchpoint position the mouse over the data item in the source code, and that can be the item's definition or anywhere it is used, and then press the right mouse button to display a context menu and select Add COBOL Watchpoint. Alternatively, open the Watchpoints window via Debug -> Windows -> Watchpoints (Native COBOL) and select New. You can then enter the name of the item, in this case item2. Although it is not used in this example, also set a watchpoint on item3 to illustrate that multiple watchpoints can be set.

Now you can start debugging either by pressing F5 or from the Visual Studio menus by Debug -> Start Debugging.

Execution will stop after

           initialize items

because the initialize verb modified the contents of item2. In the Watchpoints window the value of item2 will be shown as 00. Note that item2 is displayed in bold indicating that it is the item that has caused execution to be interrupted.

Continue execution by pressing F5 or Debug -> Continue

Execution will stop after

           move 12 to item2

as the item has been directly modified and the updated value can be seen in the Watchpoints window.

Continue execution. Execution will now stop after

           move "99" to item2-redef

because item2-redef is a redefinition of item2 and assigning a value to it has therefore changed the value of item2. The Watchpoints window will show item2's new value of 99.

Continue execution. Execution will now stop after

           move "1234" to item1(1:4)

Using the Watchpoint has caught the situation where the value of item2 has been modified as a result of a buffer overrun, a common cause of corruption of data. It now has value "34".

 

Although this is a very simple example it shows how useful Watchpoints can be in monitoring a data item as it may be changed without it being obvious from the source code that it has occurred.

Using Watchpoints on arrays

In the above sample the Watchpoint used was a simple data item. But Watchpoints can also be used to monitor more complex cases. Here is a sample program that contains a loop which populates the contents of an array.

       identification division.
       program-idProgram1.

       environment division.
       configuration section.

       data division.
       working-storage section.
       01 fillchar                 pic x value "a".
       01 fillchar-cx redefines fillchar pic x comp-x .
       01 myarray.
           03 array-element        pic x occurs 20.
       01 i                        binary-long.
       procedure division.
           perform varying i from 1 by 1 until 
                           i > length of myarray
               move fillchar to array-element(i)
               add 1 to fillchar-cx
           end-perform
           goback.

       end program Program1.

Select a New watchpoint in the Watchpoint window and enter array-element(11). Then press F5 to start debugging. You will see that the debugger only breaks when the eleventh element of the array is modified.

Using Watchpoints on elements of a data structure

You can also specify watchpoints for items that are not uniquely named ie items in a group.

       identification division.
       program-idProgram1.

       environment division.
       configuration section.

       data division.
       working-storage section.
       01 struct1.
           03 item1            pic xx.
       01 struct2.
           03 item1            binary-long.
       procedure division.
           move spaces to item1 of struct1
           move zero to item1 of struct2
           goback.

       end program Program1.

In this sample there are two instances of item1. To watch the item1 that is in struct2 you simply select a New watchpoint in the Watchpoints window and enter item1 of struct2. If you start debugging execution will only stop after

           move zero to item1 of struct2

These examples give a simple illustration of how Watchpoints can be used and hopefully demonstrate how useful they can be.  

 

 

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Version history
Revision #:
1 of 1
Last update:
‎2012-06-21 16:57
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.