| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../program.h" |
| 6 |
#include "../util.h" |
| 7 |
|
| 8 |
static Program *Thread_prog(Thread *self) |
| 9 |
{ |
| 10 |
return container_of(self->thread.prog, Program, prog); |
| 11 |
} |
| 12 |
|
| 13 |
PyObject *Thread_wrap(struct drgn_thread *thread) |
| 14 |
{ |
| 15 |
_cleanup_pydecref_ Thread *ret = call_tp_alloc(Thread); |
| 16 |
if (!ret) |
| 17 |
return NULL; |
| 18 |
struct drgn_error *err = |
| 19 |
drgn_thread_dup_internal(thread, &ret->thread); |
| 20 |
if (err) { |
| 21 |
ret->thread.prog = NULL; |
| 22 |
return set_drgn_error(err); |
| 23 |
} |
| 24 |
Py_INCREF(container_of(thread->prog, Program, prog)); |
| 25 |
return (PyObject *)no_cleanup_ptr(ret); |
| 26 |
} |
| 27 |
|
| 28 |
static void Thread_dealloc(Thread *self) |
| 29 |
{ |
| 30 |
PyObject_GC_UnTrack(self); |
| 31 |
if (self->thread.prog) { |
| 32 |
Program *prog = Thread_prog(self); |
| 33 |
drgn_thread_deinit(&self->thread); |
| 34 |
Py_DECREF(prog); |
| 35 |
} |
| 36 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 37 |
} |
| 38 |
|
| 39 |
static int Thread_traverse(Thread *self, visitproc visit, void *arg) |
| 40 |
{ |
| 41 |
if (self->thread.prog) |
| 42 |
Py_VISIT(Thread_prog(self)); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
static PyObject *Thread_get_tid(Thread *self) |
| 47 |
{ |
| 48 |
return PyLong_FromUInt32(self->thread.tid); |
| 49 |
} |
| 50 |
|
| 51 |
static DrgnObject *Thread_get_object(Thread *self) |
| 52 |
{ |
| 53 |
const struct drgn_object *object; |
| 54 |
struct drgn_error *err = drgn_thread_object(&self->thread, &object); |
| 55 |
if (err) |
| 56 |
return set_drgn_error(err); |
| 57 |
_cleanup_pydecref_ DrgnObject *ret = |
| 58 |
DrgnObject_alloc(Thread_prog(self)); |
| 59 |
if (!ret) |
| 60 |
return NULL; |
| 61 |
err = drgn_object_copy(&ret->obj, object); |
| 62 |
if (err) |
| 63 |
return set_drgn_error(err); |
| 64 |
return_ptr(ret); |
| 65 |
} |
| 66 |
|
| 67 |
static PyObject *Thread_get_name(Thread *self) |
| 68 |
{ |
| 69 |
_cleanup_free_ char *ret = NULL; |
| 70 |
struct drgn_error *err = drgn_thread_name(&self->thread, &ret); |
| 71 |
if (err) |
| 72 |
return set_drgn_error(err); |
| 73 |
if (!ret) { |
| 74 |
Py_RETURN_NONE; |
| 75 |
} |
| 76 |
return PyUnicode_DecodeFSDefault(ret); |
| 77 |
} |
| 78 |
|
| 79 |
static PyObject *Thread_stack_trace(Thread *self) |
| 80 |
{ |
| 81 |
struct drgn_error *err; |
| 82 |
struct drgn_stack_trace *trace; |
| 83 |
err = drgn_thread_stack_trace(&self->thread, &trace); |
| 84 |
if (err) |
| 85 |
return set_drgn_error(err); |
| 86 |
PyObject *ret = StackTrace_wrap(trace); |
| 87 |
if (!ret) |
| 88 |
drgn_stack_trace_destroy(trace); |
| 89 |
return ret; |
| 90 |
} |
| 91 |
|
| 92 |
static PyGetSetDef Thread_getset[] = { |
| 93 |
{"tid", (getter)Thread_get_tid, NULL, drgn_Thread_tid_DOC}, |
| 94 |
{"object", (getter)Thread_get_object, NULL, drgn_Thread_object_DOC}, |
| 95 |
{"name", (getter)Thread_get_name, NULL, drgn_Thread_name_DOC}, |
| 96 |
{}, |
| 97 |
}; |
| 98 |
|
| 99 |
static PyMethodDef Thread_methods[] = { |
| 100 |
{"stack_trace", (PyCFunction)Thread_stack_trace, METH_NOARGS, |
| 101 |
drgn_Thread_stack_trace_DOC}, |
| 102 |
{}, |
| 103 |
}; |
| 104 |
|
| 105 |
PyTypeObject Thread_type = { |
| 106 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 107 |
.tp_name = "_drgn.Thread", |
| 108 |
.tp_basicsize = sizeof(Thread), |
| 109 |
.tp_dealloc = (destructor)Thread_dealloc, |
| 110 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 111 |
.tp_doc = drgn_Thread_DOC, |
| 112 |
.tp_traverse = (traverseproc)Thread_traverse, |
| 113 |
.tp_getset = Thread_getset, |
| 114 |
.tp_methods = Thread_methods, |
| 115 |
}; |
| 116 |
|
| 117 |
static void ThreadIterator_dealloc(ThreadIterator *self) |
| 118 |
{ |
| 119 |
PyObject_GC_UnTrack(self); |
| 120 |
drgn_thread_iterator_destroy(self->iterator); |
| 121 |
Py_XDECREF(self->prog); |
| 122 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 123 |
} |
| 124 |
|
| 125 |
static int ThreadIterator_traverse(ThreadIterator *self, visitproc visit, |
| 126 |
void *arg) |
| 127 |
{ |
| 128 |
Py_VISIT(self->prog); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static PyObject *ThreadIterator_next(ThreadIterator *self) |
| 133 |
{ |
| 134 |
struct drgn_error *err; |
| 135 |
struct drgn_thread *thread; |
| 136 |
err = drgn_thread_iterator_next(self->iterator, &thread); |
| 137 |
if (err) |
| 138 |
return set_drgn_error(err); |
| 139 |
return thread ? Thread_wrap(thread) : NULL; |
| 140 |
} |
| 141 |
|
| 142 |
PyTypeObject ThreadIterator_type = { |
| 143 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 144 |
.tp_name = "_drgn._ThreadIterator", |
| 145 |
.tp_basicsize = sizeof(ThreadIterator), |
| 146 |
.tp_dealloc = (destructor)ThreadIterator_dealloc, |
| 147 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 148 |
.tp_traverse = (traverseproc)ThreadIterator_traverse, |
| 149 |
.tp_iter = PyObject_SelfIter, |
| 150 |
.tp_iternext = (iternextfunc)ThreadIterator_next, |
| 151 |
}; |