Running hybrid python/c++ code in gdb - python

I have a system that uses both c++ and python codes. For example I want to run python script which calls c++ function. I want to run this python script in gdb (or any possible debuger) and step into c++ function it calls. I found this:
gdb -ex r --args python <programname>.py <arguments>
, but the whole system runs till its end. I would like it to stop in the beginning of the <programname>.py code, and then I would use gdb commands like next or step. Is this possible somehow?

Sure. To start GDB:
gdb --args python <programname>.py <arguments>
Then you'll be inside GDB, and can do start to launch the program and then step through it, set breakpoints, etc.

Related

writing a c++ script for using GDB commands and GDB results (like variable values) while running another c++ program

I need a c++ script to automate GDB debugging another c++ program. the c++ script has to be able to run gdb commands and get the results from it and use it inside the script to save variable values for later usage, my main questions are:
is there a c++ library for using GDB programmatically (running gdb commands like
continue
step into
) and getting value variables
if there is no library, how can I implement the c++ script myself?
can I use a python script instead of a c++ script to debug the c++ program?
if it is possible to use python, what libraries can I use for using GDB programmatically in python?
giving an implementation example for scripts would be a good guideline and help
thanks

Cannot run MATLAB from bash script under Windows/MSYS2

I need to run a MATLAB script from a Python script. I don't care about the output of it nor do I need to give it any arguments.
However, MATLAB R2016B's "engine" does not support Python 3.7 (Upgrading Matlab or down-grading python is not an option at this time)
So, I decided to make a shell script that runs it:
#!matlab -nodisplay -nodesktop -r 'try; myMatlabScript; catch; end; quit'
Now I need to run a bash script from Python. To do so, I did:
import subprocess
subprocess.call("./mybashscript.sh")
(And yes, the python script is at the same level as the shell script)
The python script does not complain directly. However, I do get the following:
'.' is not recognized as an internal or external command, operable program or batch file.
Which to me means that since Windows doesn't directly have bash, it doesn't know what to do with this shell script. I am not sure how to handle this. Some way to tell Python to use MSYS instead of Windows for the shell?
And thus the MATLAB script does not appear to run at all.
When I attempt under Linux (just for testing, I can't run it here for performance reasons), I get:
./mybashscript.sh: matlab: bad interpreter: No such file or directory
Is it possible this is because I didn't do the command addpath(genpath('.'))? If so, I'm not sure how I would do that in the shell script, and some help would be appreciated.
Or some other better solution would also be great.
1: Needed to re-name mybashscript.sh to mybashscript.bat
2: Needed to change the sub-process call to subprocess.call("mybashscript.bat") (as ./ was confusing the windows shell)
3: Needed to add the path properly. Here is what the batch script looked like:
matlab -nodisplay -nodesktop -r "addpath(genpath('C:/path/to/myscript')); myMatlabScript"
The double quotes are neccesary so the single quotes inside genpath do not cause it to end early.
And that was it!
EDIT: You can add -wait in the batch file to get the script to wait until it is complete before handing back to the Python script.

Execute python code from within a shell script

I want to have some python code run within a shell script. I don't want to rely on an external file to be ran. Is there any way to do that?
I did a ton of googling, but there aren't any clear answers. This code is what I find... But it relies on the external python script to be ran. I want it all within one file.
python python_script.py
You can use a so-called "here document":
#!/usr/bin/env bash
echo "hello from bash"
python3 - <<'EOF'
print("hello from Python 3")
EOF
The single quotes around the first EOF prevent the usual expansions and command substitions in a shell script.
If you want those to happen, simply remove them.
If you mean within a BASH shell script without executing any external dependencies, I am afraid you're out of luck, since BASH only interprets its own scripting language.
Your question is somewhat like asking "Can I run a Java .class file without the JVM"? Obviously, you will always have the external dependency of the JRE/JVM. This is the same case, you depend on the external Python compiler and interpreter.
Optionally, you have the option of including the python script inline, but it would still require the python executable.
This works:
python -c 'print("Hi")'
Or this with BASH redirection:
python <<< 'print("Hi")'

Debugging TensorFlow tests: pdb or gdb?

I am debugging decode_raw_op_test from TensorFlow. The test file is written in python however it executes code from underlying C++ files.
Using pdb, I could debug python test file however it doesn't recognize c++ file. Is there a way in which we can debug underlying c++ code?
(I tried using gdb on decode_raw_op_test but it gives "File not in executable format: File format not recognized")
Debugging a mixed Python and C++ program is tricky. You can use gdb to debug the C++ parts of TensorFlow, however. There are two main ways to do this:
Run python under gdb, rather than the test script itself. Let's say that your test script is in bazel-bin/tensorflow/python/kernel_tests/decode_raw_op_test. You would run the following command:
$ gdb python bazel-bin/tensorflow/python/kernel_tests/decode_raw_op_test
(gdb) run
Note that gdb does not have great support for debugging the Python parts of the code. I'd recommend narrowing down the test case that you run to a single, simple test, and setting a breakpoint on a TensorFlow C API method, such as TF_Run, which is the main entry point from Python into C++ in TensorFlow.
Attach gdb to a running process. You can get the process ID of the Python test using ps and then run (where $PID is the process ID):
$ gdb -p $PID
You will probably need to arrange for your Python code to block so that there's time to attach. Calling the raw_input() function is an easy way to do this.
Could debug using below steps:
gdb python
then on gdb prompt, type
run bazel-bin/tensorflow/python/kernel_tests/decode_raw_op_test
Adding on mrry's answer, in today's TF2 environment, the main entry point would be TFE_Execute, this should be where you add the breakpoint.

run python script in parallel to matlab

I have 2 scripts:
python script
matlab script
I need to run this two scripts in parallel (no output for both of them). I was thinknig to call to the python scirpt, from the matlab script.
I know it is possible to run python script from matlab like:
systemCommand='my_script.py'
system(systemCommand)
however in this way, the matlab script will wait to the return of the python script, and the rest of my matlab script will not be executed.
any ideas?
As mentioned near the end of MATLAB's system documentation in the "Tips" section, to run a system command in the background (on *nix), you can append an ampersand(&) to the end of your command to tell it to run in the background.
system('my_script.py &')
If you're on Windows, you'll want to use the following to prevent a command window from opening.
system('start /b my_script.py');

Categories