| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../helpers.h" |
| 6 |
#include "../kallsyms.h" |
| 7 |
#include "../program.h" |
| 8 |
|
| 9 |
PyObject *drgnpy_linux_helper_direct_mapping_offset(PyObject *self, PyObject *arg) |
| 10 |
{ |
| 11 |
struct drgn_error *err; |
| 12 |
if (!PyObject_TypeCheck(arg, &Program_type)) { |
| 13 |
return PyErr_Format(PyExc_TypeError, "expected Program, not %s", |
| 14 |
Py_TYPE(arg)->tp_name); |
| 15 |
} |
| 16 |
uint64_t ret; |
| 17 |
err = linux_helper_direct_mapping_offset(&((Program *)arg)->prog, &ret); |
| 18 |
if (err) |
| 19 |
return set_drgn_error(err); |
| 20 |
return PyLong_FromUInt64(ret); |
| 21 |
} |
| 22 |
|
| 23 |
PyObject *drgnpy_linux_helper_read_vm(PyObject *self, PyObject *args, |
| 24 |
PyObject *kwds) |
| 25 |
{ |
| 26 |
static char *keywords[] = {"prog", "pgtable", "address", "size", NULL}; |
| 27 |
struct drgn_error *err; |
| 28 |
Program *prog; |
| 29 |
struct index_arg pgtable = {}; |
| 30 |
struct index_arg address = {}; |
| 31 |
Py_ssize_t size; |
| 32 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&O&n:read_vm", |
| 33 |
keywords, &Program_type, &prog, |
| 34 |
index_converter, &pgtable, |
| 35 |
index_converter, &address, &size)) |
| 36 |
return NULL; |
| 37 |
|
| 38 |
if (size < 0) { |
| 39 |
PyErr_SetString(PyExc_ValueError, "negative size"); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
_cleanup_pydecref_ PyObject *buf = PyBytes_FromStringAndSize(NULL, size); |
| 43 |
if (!buf) |
| 44 |
return NULL; |
| 45 |
err = linux_helper_read_vm(&prog->prog, pgtable.uvalue, address.uvalue, |
| 46 |
PyBytes_AS_STRING(buf), size); |
| 47 |
if (err) |
| 48 |
return set_drgn_error(err); |
| 49 |
return_ptr(buf); |
| 50 |
} |
| 51 |
|
| 52 |
PyObject *drgnpy_linux_helper_follow_phys(PyObject *self, PyObject *args, |
| 53 |
PyObject *kwds) |
| 54 |
{ |
| 55 |
static char *keywords[] = {"prog", "pgtable", "address", NULL}; |
| 56 |
struct drgn_error *err; |
| 57 |
Program *prog; |
| 58 |
struct index_arg pgtable = {}; |
| 59 |
struct index_arg address = {}; |
| 60 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&O&:follow_phys", |
| 61 |
keywords, &Program_type, &prog, |
| 62 |
index_converter, &pgtable, |
| 63 |
index_converter, &address)) |
| 64 |
return NULL; |
| 65 |
|
| 66 |
uint64_t phys; |
| 67 |
err = linux_helper_follow_phys(&prog->prog, pgtable.uvalue, |
| 68 |
address.uvalue, &phys); |
| 69 |
if (err) |
| 70 |
return set_drgn_error(err); |
| 71 |
return PyLong_FromUInt64(phys); |
| 72 |
} |
| 73 |
|
| 74 |
DrgnObject *drgnpy_linux_helper_per_cpu_ptr(PyObject *self, PyObject *args, |
| 75 |
PyObject *kwds) |
| 76 |
{ |
| 77 |
static char *keywords[] = {"ptr", "cpu", NULL}; |
| 78 |
struct drgn_error *err; |
| 79 |
DrgnObject *ptr; |
| 80 |
struct index_arg cpu = {}; |
| 81 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&:per_cpu_ptr", |
| 82 |
keywords, &DrgnObject_type, &ptr, |
| 83 |
index_converter, &cpu)) |
| 84 |
return NULL; |
| 85 |
|
| 86 |
_cleanup_pydecref_ DrgnObject *res = DrgnObject_alloc(DrgnObject_prog(ptr)); |
| 87 |
if (!res) |
| 88 |
return NULL; |
| 89 |
err = linux_helper_per_cpu_ptr(&res->obj, &ptr->obj, cpu.uvalue); |
| 90 |
if (err) |
| 91 |
return set_drgn_error(err); |
| 92 |
return_ptr(res); |
| 93 |
} |
| 94 |
|
| 95 |
DrgnObject *drgnpy_linux_helper_cpu_curr(PyObject *self, PyObject *args) |
| 96 |
{ |
| 97 |
struct drgn_error *err; |
| 98 |
Program *prog; |
| 99 |
struct index_arg cpu = {}; |
| 100 |
if (!PyArg_ParseTuple(args, "O!O&:cpu_curr", &Program_type, &prog, |
| 101 |
index_converter, &cpu)) |
| 102 |
return NULL; |
| 103 |
|
| 104 |
_cleanup_pydecref_ DrgnObject *res = DrgnObject_alloc(prog); |
| 105 |
if (!res) |
| 106 |
return NULL; |
| 107 |
err = linux_helper_cpu_curr(&res->obj, cpu.uvalue); |
| 108 |
if (err) |
| 109 |
return set_drgn_error(err); |
| 110 |
return_ptr(res); |
| 111 |
} |
| 112 |
|
| 113 |
DrgnObject *drgnpy_linux_helper_idle_task(PyObject *self, PyObject *args) |
| 114 |
{ |
| 115 |
struct drgn_error *err; |
| 116 |
Program *prog; |
| 117 |
struct index_arg cpu = {}; |
| 118 |
if (!PyArg_ParseTuple(args, "O!O&:idle_task", &Program_type, &prog, |
| 119 |
index_converter, &cpu)) |
| 120 |
return NULL; |
| 121 |
|
| 122 |
_cleanup_pydecref_ DrgnObject *res = DrgnObject_alloc(prog); |
| 123 |
if (!res) |
| 124 |
return NULL; |
| 125 |
err = linux_helper_idle_task(&res->obj, cpu.uvalue); |
| 126 |
if (err) |
| 127 |
return set_drgn_error(err); |
| 128 |
return_ptr(res); |
| 129 |
} |
| 130 |
|
| 131 |
DrgnObject *drgnpy_linux_helper_task_thread_info(PyObject *self, PyObject *args, |
| 132 |
PyObject *kwds) |
| 133 |
{ |
| 134 |
static char *keywords[] = {"task", NULL}; |
| 135 |
struct drgn_error *err; |
| 136 |
DrgnObject *task; |
| 137 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!:task_thread_info", |
| 138 |
keywords, &DrgnObject_type, &task)) |
| 139 |
return NULL; |
| 140 |
|
| 141 |
_cleanup_pydecref_ DrgnObject *res = |
| 142 |
DrgnObject_alloc(DrgnObject_prog(task)); |
| 143 |
if (!res) |
| 144 |
return NULL; |
| 145 |
err = linux_helper_task_thread_info(&res->obj, &task->obj); |
| 146 |
if (err) |
| 147 |
return set_drgn_error(err); |
| 148 |
return_ptr(res); |
| 149 |
} |
| 150 |
|
| 151 |
PyObject *drgnpy_linux_helper_task_cpu(PyObject *self, PyObject *args, |
| 152 |
PyObject *kwds) |
| 153 |
{ |
| 154 |
static char *keywords[] = {"task", NULL}; |
| 155 |
struct drgn_error *err; |
| 156 |
DrgnObject *task; |
| 157 |
|
| 158 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!:task_cpu", keywords, |
| 159 |
&DrgnObject_type, &task)) |
| 160 |
return NULL; |
| 161 |
uint64_t cpu; |
| 162 |
err = linux_helper_task_cpu(&task->obj, &cpu); |
| 163 |
if (err) |
| 164 |
return set_drgn_error(err); |
| 165 |
return PyLong_FromUInt64(cpu); |
| 166 |
} |
| 167 |
|
| 168 |
PyObject *drgnpy_linux_helper_task_on_cpu(PyObject *self, PyObject *args, |
| 169 |
PyObject *kwds) |
| 170 |
{ |
| 171 |
static char *keywords[] = {"task", NULL}; |
| 172 |
struct drgn_error *err; |
| 173 |
DrgnObject *task; |
| 174 |
|
| 175 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!:task_on_cpu", keywords, |
| 176 |
&DrgnObject_type, &task)) |
| 177 |
return NULL; |
| 178 |
bool on_cpu; |
| 179 |
err = linux_helper_task_on_cpu(&task->obj, &on_cpu); |
| 180 |
if (err) |
| 181 |
return set_drgn_error(err); |
| 182 |
Py_RETURN_BOOL(on_cpu); |
| 183 |
} |
| 184 |
|
| 185 |
DrgnObject *drgnpy_linux_helper_xa_load(PyObject *self, PyObject *args, |
| 186 |
PyObject *kwds) |
| 187 |
{ |
| 188 |
static char *keywords[] = {"xa", "index", NULL}; |
| 189 |
struct drgn_error *err; |
| 190 |
DrgnObject *xa; |
| 191 |
struct index_arg index = {}; |
| 192 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&:xa_load", keywords, |
| 193 |
&DrgnObject_type, &xa, index_converter, |
| 194 |
&index)) |
| 195 |
return NULL; |
| 196 |
|
| 197 |
_cleanup_pydecref_ DrgnObject *res = DrgnObject_alloc(DrgnObject_prog(xa)); |
| 198 |
if (!res) |
| 199 |
return NULL; |
| 200 |
err = linux_helper_xa_load(&res->obj, &xa->obj, index.uvalue); |
| 201 |
if (err) |
| 202 |
return set_drgn_error(err); |
| 203 |
return_ptr(res); |
| 204 |
} |
| 205 |
|
| 206 |
DrgnObject *drgnpy_linux_helper_idr_find(PyObject *self, PyObject *args, |
| 207 |
PyObject *kwds) |
| 208 |
{ |
| 209 |
static char *keywords[] = {"idr", "id", NULL}; |
| 210 |
struct drgn_error *err; |
| 211 |
DrgnObject *idr; |
| 212 |
struct index_arg id = {}; |
| 213 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&:idr_find", keywords, |
| 214 |
&DrgnObject_type, &idr, |
| 215 |
index_converter, &id)) |
| 216 |
return NULL; |
| 217 |
|
| 218 |
_cleanup_pydecref_ DrgnObject *res = |
| 219 |
DrgnObject_alloc(DrgnObject_prog(idr)); |
| 220 |
if (!res) |
| 221 |
return NULL; |
| 222 |
err = linux_helper_idr_find(&res->obj, &idr->obj, id.uvalue); |
| 223 |
if (err) |
| 224 |
return set_drgn_error(err); |
| 225 |
return_ptr(res); |
| 226 |
} |
| 227 |
|
| 228 |
DrgnObject *drgnpy_linux_helper_find_pid(PyObject *self, PyObject *args) |
| 229 |
{ |
| 230 |
struct drgn_error *err; |
| 231 |
DrgnObject *ns; |
| 232 |
struct index_arg pid = {}; |
| 233 |
if (!PyArg_ParseTuple(args, "O!O&:find_pid", &DrgnObject_type, &ns, |
| 234 |
index_converter, &pid)) |
| 235 |
return NULL; |
| 236 |
|
| 237 |
_cleanup_pydecref_ DrgnObject *res = |
| 238 |
DrgnObject_alloc(DrgnObject_prog(ns)); |
| 239 |
if (!res) |
| 240 |
return NULL; |
| 241 |
err = linux_helper_find_pid(&res->obj, &ns->obj, pid.uvalue); |
| 242 |
if (err) |
| 243 |
return set_drgn_error(err); |
| 244 |
return_ptr(res); |
| 245 |
} |
| 246 |
|
| 247 |
DrgnObject *drgnpy_linux_helper_pid_task(PyObject *self, PyObject *args, |
| 248 |
PyObject *kwds) |
| 249 |
{ |
| 250 |
static char *keywords[] = {"pid", "pid_type", NULL}; |
| 251 |
struct drgn_error *err; |
| 252 |
DrgnObject *pid; |
| 253 |
struct index_arg pid_type = {}; |
| 254 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&:pid_task", keywords, |
| 255 |
&DrgnObject_type, &pid, |
| 256 |
index_converter, &pid_type)) |
| 257 |
return NULL; |
| 258 |
|
| 259 |
_cleanup_pydecref_ DrgnObject *res = DrgnObject_alloc(DrgnObject_prog(pid)); |
| 260 |
if (!res) |
| 261 |
return NULL; |
| 262 |
err = linux_helper_pid_task(&res->obj, &pid->obj, pid_type.uvalue); |
| 263 |
if (err) |
| 264 |
return set_drgn_error(err); |
| 265 |
return_ptr(res); |
| 266 |
} |
| 267 |
|
| 268 |
DrgnObject *drgnpy_linux_helper_find_task(PyObject *self, PyObject *args) |
| 269 |
{ |
| 270 |
struct drgn_error *err; |
| 271 |
DrgnObject *ns; |
| 272 |
struct index_arg pid = {}; |
| 273 |
if (!PyArg_ParseTuple(args, "O!O&:find_task", &DrgnObject_type, &ns, |
| 274 |
index_converter, &pid)) |
| 275 |
return NULL; |
| 276 |
|
| 277 |
_cleanup_pydecref_ DrgnObject *res = |
| 278 |
DrgnObject_alloc(DrgnObject_prog(ns)); |
| 279 |
if (!res) |
| 280 |
return NULL; |
| 281 |
err = linux_helper_find_task(&res->obj, &ns->obj, pid.uvalue); |
| 282 |
if (err) |
| 283 |
return set_drgn_error(err); |
| 284 |
return_ptr(res); |
| 285 |
} |
| 286 |
|
| 287 |
PyObject *drgnpy_linux_helper_kaslr_offset(PyObject *self, PyObject *arg) |
| 288 |
|
| 289 |
{ |
| 290 |
if (!PyObject_TypeCheck(arg, &Program_type)) { |
| 291 |
return PyErr_Format(PyExc_TypeError, "expected Program, not %s", |
| 292 |
Py_TYPE(arg)->tp_name); |
| 293 |
} |
| 294 |
Program *prog = (Program *)arg; |
| 295 |
if (!(prog->prog.flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) |
| 296 |
return PyErr_Format(PyExc_ValueError, "not Linux kernel"); |
| 297 |
return PyLong_FromUInt64(prog->prog.vmcoreinfo.kaslr_offset); |
| 298 |
} |
| 299 |
|
| 300 |
PyObject *drgnpy_linux_helper_pgtable_l5_enabled(PyObject *self, PyObject *arg) |
| 301 |
|
| 302 |
{ |
| 303 |
if (!PyObject_TypeCheck(arg, &Program_type)) { |
| 304 |
return PyErr_Format(PyExc_TypeError, "expected Program, not %s", |
| 305 |
Py_TYPE(arg)->tp_name); |
| 306 |
} |
| 307 |
Program *prog = (Program *)arg; |
| 308 |
if (!(prog->prog.flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) |
| 309 |
return PyErr_Format(PyExc_ValueError, "not Linux kernel"); |
| 310 |
Py_RETURN_BOOL(prog->prog.vmcoreinfo.pgtable_l5_enabled); |
| 311 |
} |
| 312 |
|
| 313 |
PyObject *drgnpy_linux_helper_load_proc_kallsyms(PyObject *self, PyObject *args, |
| 314 |
PyObject *kwds) |
| 315 |
|
| 316 |
{ |
| 317 |
static char *kwnames[] = {"filename", "modules", NULL}; |
| 318 |
const char *filename = "/proc/kallsyms"; |
| 319 |
int modules = 0; |
| 320 |
|
| 321 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sp:load_proc_kallsyms", |
| 322 |
kwnames, &filename, &modules)) |
| 323 |
return NULL; |
| 324 |
|
| 325 |
_cleanup_pydecref_ SymbolIndex *index = call_tp_alloc(SymbolIndex); |
| 326 |
if (!index) |
| 327 |
return set_drgn_error(&drgn_enomem); |
| 328 |
|
| 329 |
struct drgn_error *err = drgn_load_proc_kallsyms(filename, modules, &index->index); |
| 330 |
if (err) |
| 331 |
return set_drgn_error(err); |
| 332 |
return (PyObject *)no_cleanup_ptr(index); |
| 333 |
} |
| 334 |
|
| 335 |
PyObject * |
| 336 |
drgnpy_linux_helper_load_builtin_kallsyms(PyObject *self, PyObject *args, |
| 337 |
PyObject *kwds) |
| 338 |
{ |
| 339 |
static char *kwnames[] = {"prog", "names", "token_table", "token_index", "num_syms", |
| 340 |
"offsets", "relative_base", "addresses", "_stext", NULL}; |
| 341 |
struct kallsyms_locations kl; |
| 342 |
PyObject *prog_obj; |
| 343 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O&O&O&O&O&O&O&O&:load_builtin_kallsyms", |
| 344 |
kwnames, &Program_type, &prog_obj, |
| 345 |
u64_converter, &kl.kallsyms_names, |
| 346 |
u64_converter, &kl.kallsyms_token_table, |
| 347 |
u64_converter, &kl.kallsyms_token_index, |
| 348 |
u64_converter, &kl.kallsyms_num_syms, |
| 349 |
u64_converter, &kl.kallsyms_offsets, |
| 350 |
u64_converter, &kl.kallsyms_relative_base, |
| 351 |
u64_converter, &kl.kallsyms_addresses, |
| 352 |
u64_converter, &kl._stext)) |
| 353 |
return NULL; |
| 354 |
|
| 355 |
struct drgn_program *prog = &((Program *)prog_obj)->prog; |
| 356 |
_cleanup_pydecref_ SymbolIndex *index = call_tp_alloc(SymbolIndex); |
| 357 |
if (!index) |
| 358 |
return set_drgn_error(&drgn_enomem); |
| 359 |
|
| 360 |
struct drgn_error *err = drgn_load_builtin_kallsyms(prog, &kl, &index->index); |
| 361 |
if (err) |
| 362 |
return set_drgn_error(err); |
| 363 |
return (PyObject *)no_cleanup_ptr(index); |
| 364 |
} |