Blog

Introducing GDB-COBOL: Source-Level Debugging for COBOL Developers

Introducing GDB-COBOL: Source-Level Debugging for COBOL Developers

GDB-COBOL brings the power of the GNU Debugger (GDB) to COBOL developers, providing source-level debugging capabilities that understand COBOL's language constructs, variable names, and program structure.

Eventually, GDB-COBOL's features will be incorporated into the mainline GDB development stream. For now, however, GDB-COBOL is distributed as a separate package that operates independently of any existing GDB installation on your system.

Getting Started with GDB-COBOL

The compiler and debugger work together, making version compatibility particularly important while both are under active development.

You can find packages for the latest development versions of the compiler and debugger on the COBOLworx Downloads page.

We recommend installing the latest GCOBOL compiler package whenever you install or update GDB-COBOL. This helps ensure compatibility between the compiler and debugger.

Debugging with COBOL Awareness

What makes GDB-COBOL particularly useful is its understanding of COBOL-specific language features.

COBOL Variables and Naming Conventions

GDB-COBOL recognizes COBOL variable names, including names containing hyphens. This allows developers to inspect variables using the same names that appear in their source code.

The debugger also supports:

  • COBOL variable comparisons: Compare variable values and perform mathematical operations.
  • Level 88 condition names: Work with COBOL's named conditions.
  • Conditional breakpoints: Set breakpoints based on variable values or comparisons between variables.
  • Watchpoints: Monitor COBOL variables for changes during program execution.

Paragraphs, Sections, and PERFORM Statements

GDB-COBOL understands COBOL paragraphs, sections, and PERFORM statements.

This means developers can navigate program execution using familiar COBOL structures. Single-stepping through PERFORM statements behaves much like stepping through CALL statements, providing a more natural debugging experience.

A Simple GDB-COBOL Example

A comprehensive guide to GDB is beyond the scope of this article, as is a detailed exploration of every GDB-COBOL capability.

However, COBOL programmers who want to perform source-level debugging will find GDB-COBOL a valuable addition to their development toolkit. Developers with even a little GDB experience should find the interface familiar and intuitive.

Let's look at a small, deliberately simple COBOL program and a sample GDB-COBOL debugging session.

Sample COBOL Program

The following program defines a group of COBOL variables, initializes them with values, and performs a paragraph named say-hello.

       identification              division.
       program-id.                 hello.
       data                        division.
       working-storage             section.
           01 the-message.
            05 parts.
             10 first-part     pic x(4)  value "Good".
             10 second-part    pic x(7)  value "morning".
            05 sources.
             10 first-source   pic x(4)  value "From".
             10 second-source  pic x(5)  value "COBOL".
            05 the-number      pic 999v9 value 123.4.
       procedure                   division.
           perform say-hello
           goback.
       say-hello.
           display "Nothing much to say.".
       end program                 hello.

A Sample Debugging Session

The following session demonstrates how GDB-COBOL can set breakpoints, inspect COBOL variables, display group structures, and report variable definitions.

$ gdb-cobol hello
(gdb) start
Temporary breakpoint 1 at 0x555555556534: file hello.cbl, line 14.
Starting program: ~/repos/gdb-cobol/gcobol/hello
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Temporary breakpoint 1, hello () at hello.cbl:14
14                  perform say-hello
(gdb) b say-hello
Breakpoint 2 at 0x555555556548: file hello.cbl, line 16.
(gdb) continue
Continuing.

Breakpoint 2, hello () at hello.cbl:16
16              say-hello.
(gdb) print the-message
$1 = "GoodmorningFromCOBOL1234"
(gdb) print/p the-message
$2 = {
01 the-message "GoodmorningFromCOBOL1234"
 05 parts "Goodmorning"
  10 first-part "Good"
  10 second-part "morning"
 05 sources "FromCOBOL"
  10 first-source "From"
  10 second-source "COBOL"
 05 the-number 123.4
}
(gdb) print the-number
$3 = 123.4
(gdb) ptype the-number
01 the-message
 05 the-number PIC 999V9 DISPLAY VALUE 123.4 (4 bytes)
(gdb)

Understanding the Debugging Session

Several GDB-COBOL capabilities are illustrated in this short example.

Setting breakpoints using COBOL paragraph names

The command b say-hello sets a breakpoint at the COBOL paragraph named say-hello. The debugger recognizes the paragraph name directly, without requiring a line number or machine-level address.

Inspecting COBOL group items

The command print the-message displays the contents of the entire group item as a single value:

$1 = "GoodmorningFromCOBOL1234"

Displaying the COBOL data hierarchy

The command print/p the-message provides a more detailed view, displaying the group structure, level numbers, variable names, and individual values.

This makes it easier to inspect complex COBOL data structures without manually examining each field.

Inspecting numeric variables

The command print the-number displays the numeric value in a human-readable form:

$3 = 123.4

Examining COBOL data definitions

Finally, ptype the-number displays the variable's COBOL declaration information, including its level number, picture clause, usage, initial value, and storage size.

Bringing Modern Debugging to COBOL

GDB-COBOL makes source-level debugging more accessible to COBOL programmers by combining familiar GDB commands with an understanding of COBOL's language structures and data definitions.

Whether you're stepping through a PERFORM statement, inspecting a group item, or tracking changes to a variable, GDB-COBOL provides tools for understanding what your program is doing as it executes.

Ready to explore GDB-COBOL?

Visit the COBOLworx Downloads page to find the latest available GCOBOL and GDB-COBOL packages.