How to open a source file in GDB TUI - gdb

When I'm GDB TUI can I load arbitrary source file for browsing without jumping into it using breakpoint?
Thanks.

You can't list any arbitrary file, but if it's part of your program, you can try something like
list sourcefile.c:1
That :1 at the end says to list from line number 1, and lets gdb know that it's file, not a function.

Related

Display matlab's program's output calling for another c++ program

I'm not sure if my question is clear:
I have a C++ project that I run from Qtcreator, this program call another c++ script outside the project. This last one runs a shell script calling a bunch of matlab scripts.
I want to display output from the matlab scripts, disp doesn't work.
I tried to write the values I want to read in a .txt file. The file is created but stay empty.
I tried these lines to write in a file:
fileId= fopen('imagename.txt','a');
fprintf(fileId, 'test : %s',imageName);
fclose(fileId);
I also tried assignin with the values I want to show but they're not kept within matlab's workspace.
I can't change the architecture of the whole program because it's a big project made by someone else I have to continue.
Do you have another way to watch what is going on the matlab scripts?
It's difficult to launch them directly from Matlab as I don't have access to their inputs values. I can hardly change the c++ script calling them to display theses values because I have a 'reference to ofstream is ambiguous' issue when I try to build it, so I will have to debug something made by someone else and non commented.
The fact that the file you write to stays empty is weird and I would try to find the reason regardless of your question.
Anyway, since you call Matlab from a shell script I guess you use 'matlab -r' option with your script name, or something similar. In that case you can use the -f option which tells Matlab to write the command line output to a log file:
matlab -logfile output.log

How to modify the line of a breakpoint in gdb?

I set a breakpoint and set its conditions and some other commands. Now I realize that I should had set it a few lines ahead. How can I change the line of the breakpoint without deleting it and losing its settings?
How can I change the line of the breakpoint without deleting it and losing its settings?
You can't.
What you can do is use save breakpoints /tmp/bp.txt command to save current settings for all breakpoints, edit the /tmp/bp.txt file to update the line info (or anything else), and finally delete to remove current breakpoints and source /tmp/bp.txt to reload them.
Considering that you will probably want to do that on many occasions, I suggest adding the following to your .gdbinit:
define loadbp
delete breakpoints
source .gdbbp
end
document loadbp
Set stored breakpoints after deleting any current breakpoints.
The breakpoints to load (set) are expected in ./.gdbbp which is the file created by savebp, but can
be edited manually.
Some breakpoints may not be set because the needed shared object hasn't been loaded yet. gdb
doesn't prompt when such breakpoint commands are not set interactively. Consequently, it may be
necessary to run this command again, once the shared object has been loaded. To account for cases
in which shared objects are loaded automatically by the dynamic loader, this command also sets a
breakpoint in main. This ensures that there is an early opportunity to call this command again to
set shared object breakpoints.
end
define savebp
save breakpoints .gdbbp
end
document savebp
Store current breakpoints in .gdbbp for retrieval using loadbp.
end
To use those commands, you need to source .gdbinit or restart gdb. Then, type savebp and hit Enter at the gdb command prompt, edit ./.gdbbp as desired, then type loadbp and hit Enter at the gdb command prompt.
Note that, as written, those commands save and load .gdbbp relative to the current directory. Usually, that's the directory from which you started gdb, but you can change it from within gdb, so pay attention to where the file is saved. (You can run the pwd command at the gdb command prompt to see what the current directory is.)

How to require gdb to save both user input and its output to the same file?

I know that I can
ask gdb to save my command history to a file, say gdb.history, by set history filename gdb.history and set history save on, and
ask it to log its output to another file, say gdb.output, by set logging file gdb.output and set logging on.
But how to require gdb to save both my command line inputs and its responding outputs to the same file, in the order of they occur in that GDB session?
I tried using the same file name in both history and logging command, it does not work, contents of that file will be overwrite by GDB command history when I quit from gdb.
You can try playing with logging plus "set trace-commands on". I don't know whether that will do what you want though. There's a bug open for this functionality: https://sourceware.org/bugzilla/show_bug.cgi?id=7219. I see I commented on it but I don't remember anything about it any more :)

Is there a way to test for whether we are on a breakpoint, using pure gdb scripting?

I am attempting to use this answer to generate an instruction trace between two lines of code.
Unfortunately, the condition in the while loop is a simple count, and I need to keep running the loop until a particular line of code in the source code is reached.
Does there exists a way to check whether we are either on a particular line of code, or at a particular breakpoint, within a pure gdb script?
I am aware of the solution here which uses the Python API. I am also aware of pin-instat, but I want to know whether this can done with pure gdb.
What if do what you want in this way
1) Get information about pc for the line which you would like to reach
Use info line or use disas /m to get information about addresses of the particular line of code.
2) Write the similar loop as in Tracing/profiling instructions
while $pc != ADDRESS-FROM-FIRST-STEP
si
end.
This way you will keep running the loop until a particular line of code in the source code is reached

How to break into code when a file has been touched

I inherited an application using a large number of text based files for configuration.
The file's names are constructed dynamically in the software, so I can't search directly for a file name in the source code.
Is there any way to break into a program running in the debugger when it touches a particular text file?
In your debugger, set a breakpoint at the CreateFile (kernel32.dll) import.
OllyDbg specific:
Press CTRL + G, type in the function name and press F2.
You can set a breakpoint at a specific location (such as calling the function to open the file) if a variable has a specific value.
If you know the place where the files are being open or if the dynamically created file names are assigned to some variable, create an conditional breakpoint that breaks the code execution only if the filename is matching the file that you're interested in.