Skip to content

Commit

Permalink
libdrgn: Add program_from_fd
Browse files Browse the repository at this point in the history
All the other Program.set_xxx() methods have a corresponding
program_from_xxx(), so it makes sense to keep things consistent.

Signed-off-by: Stephen Brennan <stephen@brennan.io>
  • Loading branch information
brenns10 committed Aug 17, 2023
1 parent ca93d4e commit 628f1dc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions _drgn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,15 @@ def program_from_core_dump(path: Path) -> Program:
"""
...

def program_from_fd(path: int) -> Program:
"""
Create a :class:`Program` from a core dump file descriptor. The type of
program (e.g., userspace or kernel) is determined automatically.
:param fd: Core dump file descriptor.
"""
...

def program_from_kernel() -> Program:
"""
Create a :class:`Program` from the running operating system kernel. This
Expand Down
2 changes: 2 additions & 0 deletions drgn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
host_platform,
offsetof,
program_from_core_dump,
program_from_fd,
program_from_kernel,
program_from_pid,
reinterpret,
Expand Down Expand Up @@ -134,6 +135,7 @@
"host_platform",
"offsetof",
"program_from_core_dump",
"program_from_fd",
"program_from_kernel",
"program_from_pid",
"reinterpret",
Expand Down
15 changes: 15 additions & 0 deletions libdrgn/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,21 @@ struct drgn_error *drgn_program_init_core_dump(struct drgn_program *prog,
return err;
}

struct drgn_error *drgn_program_init_fd(struct drgn_program *prog, int fd)
{
struct drgn_error *err;

err = drgn_program_set_fd(prog, fd);
if (err)
return err;
err = drgn_program_load_debug_info(prog, NULL, 0, true, true);
if (err && err->code == DRGN_ERROR_MISSING_DEBUG_INFO) {
drgn_error_destroy(err);
err = NULL;
}
return err;
}

struct drgn_error *drgn_program_init_kernel(struct drgn_program *prog)
{
struct drgn_error *err;
Expand Down
6 changes: 6 additions & 0 deletions libdrgn/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ void drgn_program_set_platform(struct drgn_program *prog,
struct drgn_error *drgn_program_init_core_dump(struct drgn_program *prog,
const char *path);

/**
* Implement @ref drgn_program_from_fd() on an initialized @ref
* drgn_program.
*/
struct drgn_error *drgn_program_init_fd(struct drgn_program *prog, int fd);

/**
* Implement @ref drgn_program_from_kernel() on an initialized @ref
* drgn_program.
Expand Down
1 change: 1 addition & 0 deletions libdrgn/python/drgnpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ bool Program_hold_reserve(Program *prog, size_t n);
int Program_type_arg(Program *prog, PyObject *type_obj, bool can_be_none,
struct drgn_qualified_type *ret);
Program *program_from_core_dump(PyObject *self, PyObject *args, PyObject *kwds);
Program *program_from_fd(PyObject *self, PyObject *args, PyObject *kwds);
Program *program_from_kernel(PyObject *self);
Program *program_from_pid(PyObject *self, PyObject *args, PyObject *kwds);

Expand Down
2 changes: 2 additions & 0 deletions libdrgn/python/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ static PyMethodDef drgn_methods[] = {
METH_VARARGS | METH_KEYWORDS, drgn_container_of_DOC},
{"program_from_core_dump", (PyCFunction)program_from_core_dump,
METH_VARARGS | METH_KEYWORDS, drgn_program_from_core_dump_DOC},
{"program_from_fd", (PyCFunction)program_from_fd,
METH_VARARGS | METH_KEYWORDS, drgn_program_from_fd_DOC},
{"program_from_kernel", (PyCFunction)program_from_kernel,
METH_NOARGS, drgn_program_from_kernel_DOC},
{"program_from_pid", (PyCFunction)program_from_pid,
Expand Down
20 changes: 20 additions & 0 deletions libdrgn/python/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,26 @@ Program *program_from_core_dump(PyObject *self, PyObject *args, PyObject *kwds)
return_ptr(prog);
}

Program *program_from_fd(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"fd", NULL};
struct drgn_error *err;
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"i:program_from_fd", keywords,
&fd))
return NULL;

_cleanup_pydecref_ Program *prog =
(Program *)PyObject_CallObject((PyObject *)&Program_type, NULL);
if (!prog)
return NULL;
err = drgn_program_init_fd(&prog->prog, fd);
if (err)
return set_drgn_error(err);
return_ptr(prog);
}

Program *program_from_kernel(PyObject *self)
{
struct drgn_error *err;
Expand Down

0 comments on commit 628f1dc

Please sign in to comment.