我们有时需要调试TensorFlow的C++代码,本文提供了一套相关调试流程。
文章目录- 一、源码安装TensorFlow
- 二、卸载TensorFlow
- 三、编译并安装Debug版本的TensorFlow
- 四、开始Debug
$ git clone https://github.com/tensorflow/tensorflow.git $ cd tensorflow $ git checkout r1.15 $ ./configure $ bazel build --config=v1 //tensorflow/tools/pip_package:build_pip_package $ ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg $ pip3 install /tmp/tensorflow_pkg/tensorflow-1.15.4-cp36-cp36m-linux_x86_64.whl二、卸载TensorFlow
$ pip3 uninstall tensorflow三、编译并安装Debug版本的TensorFlow
$ ./configure $ bazel clean $ bazel build --config=v1 --strip=never --copt="-DNDEBUG" --copt="-march=native" --copt="-Og" --copt="-g3" //tensorflow/tools/pip_package:build_pip_package $ ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg $ pip3 install /tmp/tensorflow_pkg/tensorflow-1.15.4-cp36-cp36m-linux_x86_64.whl四、开始Debug
- 创建一个Python文件并添加如下内容
import tensorflow as tf
import sys
import os
input("pid:" + str(os.getpid()) + ", enter to continue")
a = tf.Variable([[100, 100],[200,200]])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(a))
- 运行Python代码,程序会暂停并打印pid
- 在另一个终端运行GDB并关联刚刚的pid
- 在GDB中添加源码路径
- 在GDB中设置断点
- 返回运行Python代码的终端并按回车
- 在GDB中按c继续执行Python进程,此时可以看到GDB成功进入到了TensorFlow的C++代码中,执行到断点处。



