Debian Codegraph @

libdrgn/python/source_location.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include "drgnpy.h"
5 #include "../util.h"
6
7 PyObject *SourceLocation_type;
8
9 int add_SourceLocation(PyObject *m)
10 {
11 _cleanup_pydecref_ PyObject *globals = PyDict_New();
12 if (!globals)
13 return -1;
14
15 {
16 _cleanup_pydecref_ PyObject *module_name =
17 PyUnicode_FromString("_drgn");
18 if (!module_name)
19 return -1;
20
21 if (PyDict_SetItemString(globals, "__name__", module_name))
22 return -1;
23 }
24
25 _cleanup_pydecref_ PyObject *res = PyRun_String(
26 "from typing import Callable, NamedTuple, Optional\n"
27 "\n"
28 "\n"
29 "class SourceLocation(\n"
30 " NamedTuple(\n"
31 " 'SourceLocationBase', [('filename', str), ('line', int), ('column', int)]\n"
32 " )\n"
33 "):\n"
34 " def __new__(\n"
35 " cls,\n"
36 " filename: str,\n"
37 " line: int,\n"
38 " column: int,\n"
39 " name: Callable[[], Optional[str]] = lambda: None,\n"
40 " ) -> 'Foo':\n"
41 " ret = super().__new__(cls, filename, line, column)\n"
42 " ret.name = name\n"
43 " return ret\n"
44 "\n"
45 " # Keep this in sync with drgn_format_stack_frame_source_impl().\n"
46 " def __str__(self) -> str:\n"
47 " name = self.name()\n"
48 " if name is None:\n"
49 " name = '\?\?\?'\n"
50 " if self.filename:\n"
51 " if self.column:\n"
52 " return f'{name} at {self.filename}:{self.line}:{self.column}'\n"
53 " else:\n"
54 " return f'{name} at {self.filename}:{self.line}'\n"
55 " else:\n"
56 " return f'{name} at \?\?:\?'\n"
57 , Py_file_input, globals, globals);
58 if (!res)
59 return -1;
60
61 SourceLocation_type = PyDict_GetItemString(globals, "SourceLocation");
62 return PyModule_AddObjectRef(m, "SourceLocation", SourceLocation_type);
63 }
64
65 PyObject *SourceLocationList_wrap(struct drgn_source_location_list *locs)
66 {
67 SourceLocationList *ret = call_tp_alloc(SourceLocationList);
68 if (!ret)
69 return NULL;
70 Py_INCREF(container_of(drgn_source_location_list_program(locs), Program,
71 prog));
72 ret->locs = locs;
73 return (PyObject *)ret;
74 }
75
76 static void SourceLocationList_dealloc(SourceLocationList *self)
77 {
78 PyObject_GC_UnTrack(self);
79 if (self->locs) {
80 struct drgn_program *prog =
81 drgn_source_location_list_program(self->locs);
82 drgn_source_location_list_destroy(self->locs);
83 Py_DECREF(container_of(prog, Program, prog));
84 }
85 Py_TYPE(self)->tp_free((PyObject *)self);
86 }
87
88 static int SourceLocationList_traverse(SourceLocationList *self,
89 visitproc visit, void *arg)
90 {
91 if (self->locs) {
92 Py_VISIT(container_of(drgn_source_location_list_program(self->locs),
93 Program, prog));
94 }
95 return 0;
96 }
97
98 static Program *SourceLocationList_get_prog(SourceLocationList *self, void *arg)
99 {
100 Program *prog =
101 container_of(drgn_source_location_list_program(self->locs),
102 Program, prog);
103 Py_INCREF(prog);
104 return prog;
105 }
106
107 static PyObject *SourceLocationList_str(SourceLocationList *self)
108 {
109 struct drgn_error *err;
110 _cleanup_free_ char *str = NULL;
111 err = drgn_format_source_location_list(self->locs, &str);
112 if (err)
113 return set_drgn_error(err);
114 return PyUnicode_FromString(str);
115 }
116
117 static Py_ssize_t SourceLocationList_length(SourceLocationList *self)
118 {
119 return drgn_source_location_list_length(self->locs);
120 }
121
122 static PyObject *SourceLocationList_name(PyObject *self)
123 {
124 SourceLocationList *locs =
125 (SourceLocationList *)PyTuple_GET_ITEM(self, 0);
126 Py_ssize_t i = PyLong_AsSize_t(PyTuple_GET_ITEM(self, 1));
127 if (i == -1 && PyErr_Occurred())
128 return NULL;
129 _cleanup_free_ char *str = NULL;
130 struct drgn_error *err = drgn_source_location_list_name_at(locs->locs,
131 i, &str);
132 if (err)
133 return set_drgn_error(err);
134 if (!str)
135 Py_RETURN_NONE;
136 return PyUnicode_FromString(str);
137 }
138
139 static PyObject *SourceLocationList_item(SourceLocationList *self, Py_ssize_t i)
140 {
141 if (i < 0 || i >= drgn_source_location_list_length(self->locs)) {
142 PyErr_SetString(PyExc_IndexError,
143 "source location list index out of range");
144 return NULL;
145 }
146 int line, column;
147 const char *filename = drgn_source_location_list_source_at(self->locs,
148 i, &line,
149 &column);
150 if (!filename) {
151 filename = "";
152 line = column = 0;
153 }
154
155 static PyMethodDef meth = {
156 .ml_name = "name",
157 .ml_meth = (PyCFunction)SourceLocationList_name,
158 .ml_flags = METH_NOARGS,
159 };
160 _cleanup_pydecref_ PyObject *name_arg = Py_BuildValue("On", self, i);
161 if (!name_arg)
162 return NULL;
163 return PyObject_CallFunction(SourceLocation_type, "siiN", filename,
164 line, column,
165 PyCFunction_New(&meth, name_arg));
166 }
167
168 static PyMethodDef SourceLocationList_methods[] = {
169 {"_repr_pretty_", (PyCFunction)repr_pretty_from_str,
170 METH_VARARGS | METH_KEYWORDS},
171 {},
172 };
173
174 static PySequenceMethods SourceLocationList_as_sequence = {
175 .sq_length = (lenfunc)SourceLocationList_length,
176 .sq_item = (ssizeargfunc)SourceLocationList_item,
177 };
178
179 static PyGetSetDef SourceLocationList_getset[] = {
180 {"prog", (getter)SourceLocationList_get_prog, NULL,
181 drgn_SourceLocationList_prog_DOC},
182 {},
183 };
184
185 PyTypeObject SourceLocationList_type = {
186 PyVarObject_HEAD_INIT(NULL, 0)
187 .tp_name = "_drgn.SourceLocationList",
188 .tp_basicsize = sizeof(SourceLocationList),
189 .tp_dealloc = (destructor)SourceLocationList_dealloc,
190 .tp_as_sequence = &SourceLocationList_as_sequence,
191 .tp_str = (reprfunc)SourceLocationList_str,
192 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
193 .tp_doc = drgn_SourceLocationList_DOC,
194 .tp_traverse = (traverseproc)SourceLocationList_traverse,
195 .tp_methods = SourceLocationList_methods,
196 .tp_getset = SourceLocationList_getset,
197 };