There probably aren’t many people that care about debugging multiple languages, but I learned a new trick today that is worth making a note of, even if that note is for a future amnesiatic self.
Here’s a debug session where C code is calling COBOL, but in the COBOL frame, the language rules prohibit running print to show the results of a C function call (example: printf, strlen, strspn, …)
To make a function call in lldb, I used to go up the stack to a C language frame. For example, if this was the COBOL code I was debugging:
(lldb) n 12/13/23 19:27:26 LTE14039I Opening LzMQZ connection. QMGR: MQZ1 MQZCONN: 0x7ff920625170 API: 0x7fed0008e0e0 Process 1673776 stopped * thread #57, name = 'LZOCREG1', stop reason = step over frame #0: 0x00007ff9243b31f2 WINDC.NATIVE.LZPDS.A0116662(LTESVCXC).f3968a73`LTESVCXC at LTESVCXC.cbl:36:1 33 34 DISPLAY 'WSCHECK: "' WORK-VAR '"' 35 -> 36 EXEC CICS LINK PROGRAM ('LTESVCXC') 37 COMMAREA(WORK-COMMAREA) 38 LENGTH (LENGTH OF WORK-COMMAREA) 39 END-EXEC (lldb) p &WORK-VAR (*char [10]) $4 = 0x00007fadef810478 (lldb) p WORK-VAR (char [10]) WORK-VAR = "STORISOK " (lldb) fr v -format x WORK-VAR (char [10]) WORK-VAR = { [0] = 0xe2 [1] = 0xe3 [2] = 0xd6 [3] = 0xd9 [4] = 0xc9 [5] = 0xe2 [6] = 0xd6 [7] = 0xd2 [8] = 0x40 [9] = 0x40 }
Aside: If you object to the use of a C address-of operator against a COBOL variable, that’s just because our debugger has C like & notational shorthand for the COBOL ‘ADDRESS OF …’, which is very useful.
If I want to run a C function against that COBOL WORKING-STORAGE variable, like strchr, to look for the address of the first EBCDIC space (0x40) in that string, I used to do it by going up the stack into a C frame, like so:
(lldb) up 2 frame #2: 0x00007ff9243b3f7e WINDC.NATIVE.LZPDS.A0116662(LTESVCXC).f3968a73`pgm_ltesvcxc + 382 WINDC.NATIVE.LZPDS.A0116662(LTESVCXC).f3968a73`pgm_ltesvcxc: -> 0x7ff9243b3f7e <+382>: jmp 0x7ff9243b3f88 ; <+392> 0x7ff9243b3f80 <+384>: addq $0x128, %rsp ; imm = 0x128 0x7ff9243b3f87 <+391>: retq 0x7ff9243b3f88 <+392>: leaq 0x201039(%rip), %rdi (lldb) print (char *)strchr(0x00007fadef810478, 0x40) (char *) $6 = 0x00007fadef810480 "@@"
Sure enough, that space is found 8 bytes into the string, as expected. This is a very short string, and I could have seen that by inspection, but it’s just to illustrate that we can make calls to functions within the debugger, and they can even be functions that aren’t in the program or language that we are debugging.
I noticed today that ‘print’ is an alias for ‘expression –‘, and that expression takes a language option. This means that I can do cross language calls like this in any frame, provided I specify the language I want. Example:
(lldb) down 2 frame #0: 0x00007ff9243b31f2 WINDC.NATIVE.LZPDS.A0116662(LTESVCXC).f3968a73`LTESVCXC at LTESVCXC.cbl:36:1 33 34 DISPLAY 'WSCHECK: "' WORK-VAR '"' 35 -> 36 EXEC CICS LINK PROGRAM ('LTESVCXC') 37 COMMAREA(WORK-COMMAREA) 38 LENGTH (LENGTH OF WORK-COMMAREA) 39 END-EXEC (lldb) expression -l c -- (char *)strchr(0x00007fadef810478, 0x40) (char *) $7 = 0x00007fadef810480 "@@"
Ten points to me for learning yet another obscure debugger trick.