跟踪嵌入式Python解释器中的代码执行

问题描述 投票:8回答:3

我想创建一个带有嵌入式python解释器和基本调试功能的应用程序。现在我在API中搜索可​​用于逐步运行代码的函数,并获取正在(或即将执行)的当前代码行的编号。

当来到tracing and profiling时,官方Python文档对我来说似乎有点不足。例如,没有关于Py_tracefunc的返回值含义的信息。

到目前为止,我已经汇总了以下内容:

#include <Python.h>

static int lineCounter = 0;

int trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
{
    if(what == PyTrace_LINE)
    {
        lineCounter += 1;
        printf("line %d\n", lineCounter);
    }
    return 0;
}

int main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyEval_SetTrace(trace, NULL);
    char *code = "def adder(a, b):\n"
                 " return a + b\n"
                 "x = 3\n"
                 "y = 4\n"
                 "print(adder(x, y))\n";
    PyRun_SimpleString(code);
    Py_Finalize();
    PyMem_RawFree(program);
    return 0;
}

但是,编译器输出以下错误:

hello.c:5:26: error: unknown type name ‘PyFrameObject’
 int trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
                          ^

我在ManjaroLinux上运行并使用以下代码编译以上内容:

gcc -o hello hello.c -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -Wunreachable-code -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong --param=ssp-buffer-size=4 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -L/usr/lib -lpython3.5m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic

我发现我可以用PyFrameObject替换struct _frame然后编程编译但是每个人都知道这是一个肮脏的黑客,而不是解决方案。

可执行文件输出以下内容:

line 1
line 2
line 3
line 4
line 5
7

但我希望跟踪脚本的执行流程(即:从第3行开始,然后是4,5,然后由于函数调用,2)。

我找不到任何关于逐步执行的事情。

您能否推荐一些关于Python C API的其他资源,包括更多信息和一些主题介绍?

我用赏金给了答案,因为它无论如何都会过期。但是,我仍在寻找,并将感谢上述其他问题的答案。

python c python-3.x python-c-api
3个回答
8
投票
hello.c:5:26: error: unknown type name ‘PyFrameObject’

此错误表示尚未声明PyFrameObject。我做了一个Google search,它显示我在Python源代码树中的frameobject.h是声明该结构的地方。

我希望你能添加这条线

#include <frameobject.h>

解决这个问题。


0
投票

pyFrameObject有一个

int f_lineno;

领域。你可以使用它。但显然,它并不总是存储正确的价值。所以,你应该使用这个功能:

/* Return the line of code the frame is currently executing. */
int PyFrame_GetLineNumber(PyFrameObject *);      

那么,你可以使用

frame->f_code->co_filename 

获取当前文件名

frame->f_code->co_name 

获取当前函数名称和

frame->f_back

在调用堆栈中降低一级。 。


-1
投票

PyFrameObject只是一个_frame结构。只需在函数签名中用PyFrameObject替换_frame,就不必包含任何额外的python头。

© www.soinside.com 2019 - 2024. All rights reserved.