benibenj / vscode-pythonCpp

A Visual Studio Code Debug Extension for debugging mixed Python and C++ code. The extension starts a Python debug session and attaches the C++ debugger to it. This extension is useful for debugging Python programs that call functions from shared libraries (.so/.dll).
Other
47 stars 7 forks source link

Can't step into c++ function #3

Closed Ward-zzZ closed 2 years ago

Ward-zzZ commented 2 years ago

I write a small test,but c++ debugger was not stopping at the breakpoints. adder_plus.cpp

#include <stdio.h>
#include <iostream>

using namespace std;

extern "C"
{

  int add_integer_plus(int a, int b)
  {
    return a+b;
  }

  float add_float_plus(float a, float b)
  {
    return a+b;
  }
}

CompileCommand:g++ adder_plus.cpp -fPIC -g -shared -o adder_plus.so

test.py

from ctypes import *

add_plus = CDLL('adder_plus.so')
sum = add_plus.add_integer_plus(4, 5)
print("sum: " + str(sum))

a = c_float(5.5)
b = c_float(4.1)
add_float = add_plus.add_float_plus
add_float.restype = c_float
sum_float = add_plus.add_float_plus(a, b)
print(sum_float)

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python C++ Debugger",
      "type": "pythoncpp",
      "request": "launch",
      "pythonConfig": "default",
      "cppConfig": "default (gdb) Attach"
    }
  ]
}

It works well when running directly. But I can not step into the cpp file when using debugger. What I do wrong? Help is very much appreciated.

Ward-zzZ commented 2 years ago

After much searching,I am finally able to debug.Telling GDB what paths to search for .so files. is the key. launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python C++ Debugger",
      "type": "pythoncpp",
      "request": "launch",
      "pythonLaunchName": "Python: Current File",
      "cppAttachName": "(gdb) Attach"
    },
    {
      "name": "(gdb) Attach",
      "type": "cppdbg",
      "request": "attach",
      "program": "/usr/bin/python3.6",
      "processId": "${command:pickProcess}",
      "MIMode": "gdb",
      "additionalSOLibSearchPath": "${workspaceFolder}",// adder_plus.so in current folder
      "targetArchitecture": "x86_64",
      "logging": { "engineLogging": true, "traceResponse": true
      },//show gdb log
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    },
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    }
  ]
}

BTW,runecho 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope in linux terminal,after that you can run without entering a password