I'm debugging a C program with GDB. I've got an array which the values are updated many times (a little bit less than 4.000). I want to write thoses value in a different file for each iterations.
I tried to set the log file name directly in a GDB file:
break test.c:95
commands
silent
# Set the logging file
set $_file=(char*)malloc(64)
call snprintf($_file, 64, "~/path/to/logging/file_%04d.txt", i) # i is the for variable
set logging file $_file
call free($_file)
set logging redirect on
set pagination off
set logging overwrite on
set logging on
set $_data=array
set $_x=0
while ($_x<2000)
set $_y=0
while ($_y<2000)
printf "%f ", $_data[$_x][$_y]
set $_y++
end
printf "\n"
set $_x++
end
# Log default setup
set logging off
set logging redirect off
set logging overwrite off
set pagination on
continue
end
But gdb created a file $_file
in my working directory instead of reading the value of the variable.
I then tried to put the file name in which I want log the output in a variable fileName
in my code (also with snprintf
). And the result is the same, a fileName
file is created in my working directory.
Finaly I tried to put the debug code into a gdb function, and give the fileName
as an argument of the function, but as previously, it does not interpret the string.
My question
With GDB, how can I use the value of a C-string as a logging file name?
You can use eval
to insert variables value into gdb's commands. In your case, logging file name could be setted to $_file
by command:
eval "set logging file %s", $_file
Also you can use dump
, in case if you want to dump array
to fileName
by gdb builtin commands:
eval "dump value %s array", fileName
Note: dumped value is binary.