I would like to write a bat script which call gdb. I want to use script variables in debugging session (like paths, variable names etc.) For example name and location of the config, address ranges to dump or even command.
Is there easy way to do that? I know that there are gdb scripts but I'm not sure how to do that in easy way.
I see that I can access to script variables with "environment" specifier but I can't use it in other way than show and modify this data.
Without considering the sense of this example I would like to have behavior like below.
Bat script:
SET MY_COMMAND=run
SET MY_COMMAND2=quit
SET MY_FUNCTION=main
:: GDB CALL ??
And expected gdb behavior:
break MY_FUNCTION
MY_COMMAND
MY_COMMAND2
Maybe this will be solution for you:
~/.gdbinit
You can put gdb commands there, and whenever gdb is started it will execute them.
Well, in fact, in your case it would be easier to use another approach:
--command=FILE
Related
I need to disassemble some C code, but I want to also show the source lines mixed in with the assembly code. The issue I have is that I'm using gdb through the MI interface, which calls disassemble (or equivalent, perhaps -data-disassemble?), and I have no control over the options sent with the command. If I did have access to the command, I could probably just use
-data-disassemble ... -- 1.
I've tried making a user-defined command, e.g.:
define dis
disassemble /s
end
but I don't know how to force using that command through gdb/mi. I also tried making an alias, but it creates a naming conflict ("Alias disassemble is the name of an existing command") and probably wouldn't work anyway because of the MI connection.
I'd like to just add a line to my .gdbinit file that forces the /s option to be a default for disassemble, but I can't find any documentation that shows how this can be done.
As a side note, I'm trying to accomplish this in MSVC 2019, and I can sort of do it manually using the instructions from Microsoft's MIEngine custom command doc. But this doesn't go through the usual gdb/mi interface, and just dumps text into the Command Window. I was able to set disassembly-flavor intel, which works in changing the assembly into Intel format, so it seems like what I want to accomplish is at least plausible.
I would like to assign the "info sharedlibrary" value to a variable in user define function.
Such as,
define customFunction
set $i = info sharedlibrary
end
But it seems impossible in gdb
And also i cannot use python script too...
Is there any way to do this??
Thank you
ps. I trying to do this because i want to print only selected library's instructions.
You don't mention why you can't use a Python function. That's by far the simplest way to program gdb.
However, if you really must do it, and really must avoid Python, there is a way. It's gross! But it does work. It's like this:
Use the various set logging commands to redirect output to a temporary file.
Invoke the command you want.
Use set logging again to disable logging.
Use the shell command to run sed or perl or what-have-you on this temporary file to turn it into a sequence of gdb commands, say commands to set a variable, or commands to print exactly the output you want.
source the resulting file
How can I append to the contents of solib-search-path?
I would like to do smothing like:
set solib-search-path $(solib-search-path):some/other/path
There isn't a simple built-in way to do this. There are two less simple ways, though.
The simplest of the two ways is to use Python scripting to change the setting. From Python it is easy to get the current value and modify it as a string.
The harder way is to use a combination of set logging, shell, and source to do it. The idea is to print the current value to a file, rewrite the file to be the new set command, and then source the result.
I'm trying to write a GDB script (legacy, not Python) that will print information on members of a local variable (a C or C++ struct), but only if that local variable exists. Something like:
# 'magic' should be evaluate to "if 'info locals' has a variable named foo, then
# evaluate to true, otherwise evaluate to false.
if (magic)
print foo->member
end
I know this is somewhat contrived, because the locals are dependent on the stack frame (so I'm probably better off making it conditional on the frame), but I'd still like to know if something along these lines is possible.
First -- Python is just far superior for this kind of thing. That's why we added it to gdb!
However, this can still be done with an older gdb. However, it's awful, and after doing it I think you'll appreciate the Python approach even more. What you do is: first, use the various "set logging" commands to redirect the output to a temporary file . Then use gdb commands to print the information you need, in this case something like "info local". Then, use the "shell" command to shell out to rewrite the temporary file into a file that is itself a gdb script. For example, use "sed" to detect that the variable exists in your output, and then emit "set $var_exists=1". Finally, "source" the result of this scripting and test the convenience variable that was set.
Eww. But it works.
Is there a way to store the output of the last command in gdb to a string? What I would like to do is store the address information of selected machine level instructions. Redirecting the output is not a solution as it would generate too much output. A simulator would also be a solution, but I'd like to see if it would be possible with gdb as I only want the analysis on a small chunk of code.
So I would need something like this:
(gdb) display/i $pc
(gdb) 1: x/i $pc 0x100000d2e <main+61>: jle 0x100000d02 <main+17
(gdb) set $foo = ??? somehow set this to display line 1
(gdb) call myFunc($foo)
(I excluded the looping controls to keep the example simple)
Or would there be another way of doing this?
Not possible as far as I know, which is kind of surprising considering all the Lisp background of the author :) You'd need either redirection (grep, sed, and awk make wonders on large files, and there's always perl), or your own instruction decoding based on $pc, which I assume is not an option.
Then I don't really understand what you are trying to do. Figure out jump targets? Relocation correctness? What is that you don't know about the code until the runtime? More details can probably point into better direction.
Edit:
Just some links - haven't tried it yet - you might want to play with script-extension setting and see if you can make Python command files work for you:
see Extending GDB and Python in GDB.