Hi everyone.

I'm trying to make a simple jump on C funcion trace by frame->f_lineno.
Example is simple, but not working.


if (frame->f_lineno == 12){

frame->f_lineno = 8;

}

attached files C and python to run test.
the line 12 I need to jump to line 8.




Att.

Leandro Müller
import importlib
import tracesys
import sys
import os

def teste(): 
    print("7") 
    print("8")
    print("9")
    print("10")
    print("11")
    print("12")
    print("13")
    print("14")


 
def get_module_to_file( ):
    SY_BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)) + 
"/%s" % ("W64"))
    SY_BASE_DIR = SY_BASE_DIR.replace("\\", "/") 
    path1 = os.path.join(SY_BASE_DIR)
    path1 = path1 +  "/check.py" 
    filename = os.path.realpath(path1) 
    filename = filename.replace("\\", "/")      
    filename = filename.replace(SY_BASE_DIR, ".")
    return filename

file = get_module_to_file()
# print(file)
# from threading import Thread
# t = Thread(target=teste)
# t.start()

tracesys.sys_settrace("lmuller", "session_login" )
tracesys.add_debug("lmuller", file,8, "lmuller", "session_login" )
teste()
print("fim")
# t = input()
from distutils.core import setup, Extension

module = Extension("tracesys",sources = ["trace.cpp"])
setup(name="Traca Package", version="1.0",description="Trace process",
ext_modules= [module]) 
#include "Python.h"
#include "frameobject.h" 
#include <vector>
#include <iostream>
#include <exception>
using namespace std;
typedef struct t_debug
{
    char *user_debug;
    char *user_console;
    char *file;
    int line;
    char *id_wp;
    char *session_login;
    char *sessionid;
    int *last;

} * st_debug;

static vector<t_debug> *list_debug = new vector<t_debug>();
static vector<st_debug> *list_debug_urgent = new vector<st_debug>();

char *user_console;
char *session_login;
char *sessionid;
static int conta = 0;

char *ReplaceAll(char *str1, const std::string &from, const std::string &to)
{
    size_t start_pos = 0;
    std::string str = str1;
    while ((start_pos = str.find(from, start_pos)) != std::string::npos)
    {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length();
    }
    char *cstr = new char[str.length() + 1];
    strcpy(cstr, str.c_str());

    return cstr;
}

char *get_variable_char(PyObject *obj, int position)
{
    PyObject *repr = PyObject_Repr(PyTuple_GetItem(obj, position));
    PyObject *str = PyUnicode_AsEncodedString(repr, "utf-8", NULL);
    char *s = PyBytes_AS_STRING(str);
    string st = s;
    if (st.length() < 2)
    {
        return "";
    }
    st = st.substr(1, st.length() - 2);
    s = new char[st.length() + 1];
    strcpy(s, st.c_str());
    printf("%s \n", s);
    return s;
}
int get_variable_int(PyObject *obj, int position)
{

    return PyLong_AsLong(PyTuple_GetItem(obj, position));
}

int has_debug_session(char *user_console, char *file, int line, char 
*session_login)
{
    int ret = -1;
    try
    {
        size_t size = list_debug->size();
        for (size_t i = 0; i < size; i++)
        {
            t_debug objl = list_debug->at(i);

            if (strcmp(objl.user_console, user_console) == 0 &&
                strcmp(objl.file, file) == 0 && (objl.line == line) &&
                strcmp(objl.session_login, session_login) == 0)

            {
                ret = 0;
                break;
            }
        }
    }
    catch (exception &e)
    {
        cout << "error process " << e.what() << '\n';
    }
    return ret;
}

int can_debug_session(char *user_c, char *file, int line, char *session_login)
{

    try
    {
        size_t size = list_debug->size();
        for (size_t i = 0; i < size; i++)
        {
            t_debug objl = list_debug->at(i);

            if (strcmp(objl.user_console, user_c) == 0 &&
                strcmp(objl.user_debug, user_c) == 0 &&
                strcmp(objl.file, file) == 0 && (objl.line == line) &&
                strcmp(objl.session_login, session_login) == 0)

            {
                return 0;
            }

            if (strcmp(objl.user_console, user_c) == 0 &&
                !strcmp(objl.user_debug, user_c) == 0 &&
                strcmp(objl.file, file) == 0 && (objl.line == line) &&
                !strcmp(objl.session_login, session_login) == 0)

            {
                return 0;
            }
        }
    }
    catch (exception &e)
    {
        cout << "error process " << e.what() << '\n';
    }
    return -1;
}

int has_debug_urgent(char *sessionid)
{
    int ret = -1;
    try
    {
        size_t size = list_debug_urgent->size();
        for (size_t i = 0; i < size; i++)
        {
            st_debug objl = list_debug_urgent->at(i);
            if (strcmp(objl->sessionid, sessionid))

            {
                ret = 0;
                break;
            }
        }
    }
    catch (exception &e)
    {
        cout << "error process " << e.what() << '\n';
    }
    return ret;
}

PyObject *add_debug_urgent(PyObject *mod, PyObject *obj)
{
    Py_Initialize();
    st_debug dict;
    dict->sessionid = get_variable_char(obj, 0);
    dict->session_login = get_variable_char(obj, 1);

    if (has_debug_urgent(dict->sessionid) == -1)
    {
        list_debug_urgent->push_back(dict);
    }
    else
    {
        size_t size = list_debug_urgent->size();
        for (size_t i = 0; i < size; i++)
        {
            st_debug objl = list_debug_urgent->at(i);
            if (strcmp(objl->sessionid, sessionid))

            {
                objl->session_login = session_login;
                break;
            }
        }
    }
    Py_Finalize();
    return Py_None;
}

//char *user_console ,   char *file, int *line,  char *user_debug, char 
*session_login
static PyObject *add_debug(PyObject *mod, PyObject *obj)
{
    t_debug dict;

    dict.user_console = get_variable_char(obj, 0);
    dict.file = get_variable_char(obj, 1);
    dict.line = get_variable_int(obj, 2);
    dict.user_debug = get_variable_char(obj, 3);
    dict.session_login = get_variable_char(obj, 4);

    if (has_debug_session(dict.user_console, dict.file, dict.line, 
dict.session_login) == -1)
    {
        list_debug->push_back(dict);
    }

    return Py_BuildValue("s", "");
}

static PyObject *remove_debug_urgent(char *sessionid)
{

    size_t size = list_debug_urgent->size();
    for (size_t i = 0; i < size; i++)
    {
        st_debug objl = list_debug_urgent->at(i);
        if (strcmp(objl->sessionid, sessionid))
        {
            list_debug_urgent->erase(list_debug_urgent->begin() + i - 1);
            break;
        }
    }
    return Py_None;
}

//char *user_console, char *file, int *line
static PyObject *remove_debug(PyObject *mod, PyObject *obj)
{

    char *user_console = get_variable_char(obj, 0);
    char *file = get_variable_char(obj, 1);
    int line = get_variable_int(obj, 2);
    char *session_login = get_variable_char(obj, 3);
    size_t size = list_debug->size();

    for (size_t i = 0; i < size; i++)
    {
        t_debug objl = list_debug->at(i);
        if (strcmp(objl.user_console, user_console) == 0 && strcmp(objl.file, 
file) == 0 && (objl.line == line) &&
            strcmp(objl.session_login, session_login) == 0)
        {
            list_debug->erase(list_debug->begin() + i - 1);
            break;
        }
    }

    return Py_BuildValue("s", "");
}

int trace_trampoline(
    PyObject *obj, PyFrameObject *frame, int event, PyObject *arg)
{
    

    PyObject *rfile = PyObject_Repr(frame->f_code->co_filename);
    PyObject *sfile = PyUnicode_AsEncodedString(rfile, "utf-8", "strict");
    char *file = PyBytes_AS_STRING(sfile);
    file = ReplaceAll(file, "\\", "/");
    file = ReplaceAll(file, "//", "/");
    int l = frame->f_lineno;
    if (l == 12 && strcmp(file, "'./check.py'") == 0 && conta == 0)
    {
        printf("last %i %i \n", frame->f_lineno, frame->f_lasti);
        frame->f_lineno = 8; 
        conta++;
        frame_setlineno
        return 0;
    }
     
    return 0;
}

PyObject *sys_settrace(PyObject *mod, PyObject *obj)
{
    user_console = get_variable_char(obj, 0);
    session_login = get_variable_char(obj, 1);

    PyEval_SetTrace(trace_trampoline, obj);
    // Py_CLEAR(pName);
    // Py_CLEAR(pModule);

    return Py_None;
}

PyObject *clear_trace(PyObject *mod)
{
    // Py_CLEAR(trace_dispatch_c);
    return Py_None;
}

static PyMethodDef myMethods[] = {
    {"sys_settrace", (PyCFunction)sys_settrace, METH_VARARGS, "settrace"},
    {"add_debug", (PyCFunction)add_debug, METH_VARARGS, "add_debug"},
    {"remove_debug", (PyCFunction)remove_debug, METH_VARARGS, ""},
    {"add_debug_urgent", (PyCFunction)add_debug_urgent, METH_VARARGS, ""},

    {NULL, NULL, 0, NULL}};

static struct PyModuleDef trace_module = {
    PyModuleDef_HEAD_INIT,
    "tracesys",
    "Trace",
    -1,
    myMethods};
PyMODINIT_FUNC PyInit_tracesys(void)
{
    PyObject *m = PyModule_Create(&trace_module);
    PyModule_AddObject(m, "version", PyLong_FromLong(2));
    return m;
}
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/7RZX4MUF6GLPU4DVXLRTQ534TDRXRL36/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to