Debugging TensorFlow tests: pdb or gdb? - python

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.

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

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")'

Run a python script from bamboo

I'm trying to run a python script from bamboo. I created a script task and wrote inline "python myFile.py". Should I be listing the full path for python?
I changed the working directory to the location of myFile.py so that is not a problem. Is there anything else I need to do within the configuration plan to properly run this script? It isn't running but I know it should be running because the script works fine from terminal on my local machine. Thanks
I run a lot of python tasks from bamboo, so it is possible. Using the Script task is generally painless...
You should be able to use your script task to run the commands directly and have stdout written to the logs. Since this is true, you can run:
'which python' -- Output the path of which python that is being ran.
'pip list' -- Output a list of which modules are installed with pip.
You should verify that the output from the above commands matches the output when ran from the server. I'm guessing they won't match up and once that is addressed, everything will work fine.
If not, comment back and we can look at a few other things.
For the future, there are a handful of different ways you can package things with python which could assist with this problem (e.g. automatically installing missing modules, etc).
You can also use the Script Task directly with an inline Python script to run your myFile.py:
/usr/bin/python <<EOF
print "Hello, World!"
EOF
Check this page for a more complex example:
https://www.langhornweb.com/display/BAT/Run+Python+script+as+a+Bamboo+task?desktop=true&macroName=seo-metadata

Running hybrid python/c++ code in gdb

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.

How to use pdb to debug module written by C?

I am using pdb to debug a python program, and the python program uses a module written by C. I want to use "step" command to enter the function in the module written by C, but I find this operation can't be successful. Is there any method to use pdb debug module written by C? Thanks in advance!
pdb won't allow you to debug modules written in C. You can however use gdb to debug errors you might be encountering in C code.
To launch a Python script using gdb you can use the following command:
gdb python
and then to execute your script:
(gdb) run <myscript>.py

Categories