Both GDB and LLDB cannot reliably execute breakpoint commands in a simple C file

As part of a research project, I am trying to write a gdb batch file that outputs specific information to each line of code in arbitrary C source files until the program terminates. This seems to be easily accomplished with a while loop, outputting whatever data I want in the loop, and then calling "next" at the end of the loop. (I know that I need a “step” for entering function calls, this does not concern me at the moment.)

However, in addition to the data that I output to each line, I also want to execute special commands at specific control points. It seems easy to do with the command. However, I ran into a problem when the while and breakpoint commands would not work.

Here is a very simple C file I'm working with for testing:

int global;

int main() {
  int x;
  x=-1;
  global = 5;
  return(0);
}

I will compile it with gcc -g -o simple simple.c. Then I launched gdb -x commands.txt. If the contents of the .txt commands is as follows:

set confirm off

exec-file simple
file simple

set logging file gdb_output.txt
set logging on
set pagination off

#Special commands I want to execute on certain breakpoints
break 5
command
  echo COMMAND 1 ACTIVATED\n
end

break 6
command
  echo COMMAND 2 ACTIVATED\n
end

break 7
command
  echo COMMAND 3 ACTIVATED\n
end

run

next
next
next
continue

quit

... then the contents of gdb_output.txt is as follows:

Breakpoint 1 at 0x4004da: file simple.c, line 5.
Breakpoint 2 at 0x4004e1: file simple.c, line 6.
Breakpoint 3 at 0x4004eb: file simple.c, line 7.

Breakpoint 1, main () at simple.c:5
5     x=-1;
COMMAND 1 ACTIVATED

Breakpoint 2, main () at simple.c:6
6     global = 5;
COMMAND 2 ACTIVATED

Breakpoint 3, main () at simple.c:7
7     return(0);
COMMAND 3 ACTIVATED
8   }
[Inferior 1 (process 29631) exited normally]

However, if I edit the batch file to try to execute it as a loop, replacing

next
next
next
continue

with

while true
  next
end

but the rest of the script is exactly the same, the commands that I specified for the breakpoints on lines 6 and 7 are never executed, as evidenced by the contents of gdb_output.txt after running the modified command file:

Breakpoint 1 at 0x4004da: file simple.c, line 5.
Breakpoint 2 at 0x4004e1: file simple.c, line 6.
Breakpoint 3 at 0x4004eb: file simple.c, line 7.

Breakpoint 1, main () at simple.c:5
5     x=-1;
COMMAND 1 ACTIVATED

Breakpoint 2, main () at simple.c:6
6     global = 5;

Breakpoint 3, main () at simple.c:7
7     return(0);
8   }
__libc_start_main (main=0x4004d6 <main()>, argc=1, argv=0x7fffffffe128, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe118) at ../csu/libc-start.c:325
325 ../csu/libc-start.c: No such file or directory.
[Inferior 1 (process 29652) exited normally]
commands.txt:30: Error in sourced command file:
The program is not being run.

, , "next" , ( "quit" script), , t , , , , , . ( , while , , C.)

GDB, - ? , GDB , , , , - GDB script?

( gdb - 7.11.1, , - Linux.)


UPDATE

lldb ( C, , ). lldb script:

target create --no-dependents --arch x86_64 simple

breakpoint set --file simple.c --line 5
breakpoint command add
  script print "COMMAND 1 ACTIVATED"
DONE

breakpoint set --file simple.c --line 6
breakpoint command add
  script print "COMMAND 2 ACTIVATED"
DONE

breakpoint set --file simple.c --line 7
breakpoint command add
  script print "COMMAND 3 ACTIVATED"
DONE

run

frame variable x
continue

frame variable x
continue

frame variable x
continue

quit

. , , . , frame variable x, continue, 1 3 . 1- 3- frame variable x, continue . , , .

- , ? , , ? gdb, ?

+4
1

, gdb lldb , , , , . script lldb , stdout script lldb stdin , script lldb (frame variable -L, bt, step ..), lldb output . script , , , , , gdb lldb.

0

Source: https://habr.com/ru/post/1681221/


All Articles