dprintf (dynamic printf )
https://sourceware.org/gdb/onlinedocs/gdb/Dynamic-Printf.html
This is the most convenient solution for a specific printing case:
dprintf <line>, "%u\n", variable
It can also be faster than commands , because it can compile and inject code instead of giving GDB control to interpret arbitrary command lines. TODO I don't know if this is really done. dprintf vs commands : What is the difference between dprintf and break + commands + continue?
Detailed example:
main.c
#include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> int main(void) { uint32_t i; uint32_t r = 0; for (i = 0; i < 10; ++i) { r += i*i + 13*r*i + 17; /* LINE 10. */ } printf("%" PRIu32 "\n", r); return EXIT_SUCCESS; }
Then:
gcc -ggdb3 -O0 -std=c99 -o main main.c gdb -batch -nh -q -ex 'dprintf 10, "%u %u\n", i, r' -ex 'run' ./main
Output:
Dprintf 1 at 0x400545: file main.c, line 10. 0 0 1 17 2 256 3 6933 4 277346 5 14699371 6 970158528 7 3628079733 8 3070853710 9 317092431 3057168588 [Inferior 1 (process 14305) exited normally]
Tested on Ubuntu 14.04.
Ciro Santilli 华 涌 低端 人口 六四 事件 法轮功 Mar 08 '17 at 14:12 2017-03-08 14:12
source share