Skip to content

Add drgn.Program.main_thread() #144

@osandov

Description

@osandov

Follow-up to #92. It'd be nice to have a way to get the drgn.Thread object for the main thread of a program. This probably only makes sense for userspace programs. (You could make a case that for the Linux kernel, some special kernel thread, e.g. kthreadd, is the "main thread", but I think asking for the main thread of the kernel should just be an error. This is up for debate.) So, the API is simply something like this:

class Program:
    def main_thread(self) -> Thread:
        """
        Get the main thread of the program.

        :raises ValueError: if the program is the Linux kernel
        """
        ...
  • For core dumps, the main thread can be identified by the the pid field of the PRPSINFO note, which will match the pid field of the PRSTATUS note for the main thread:
$ eu-readelf -n crashme_multithreaded_core | grep -A 3 PRPSINFO
  CORE                 136  PRPSINFO
    state: 2, sname: D, zomb: 0, nice: 0, flag: 0x0000000040400404
    uid: 1000, gid: 985, pid: 39143, ppid: 39053, pgrp: 39143, sid: 39053
    fname: crashme_multith, psargs: ./crashme_multithreaded 
$ eu-readelf -n crashme_multithreaded_core | grep -A 4 PRSTATUS
  CORE                 336  PRSTATUS
    info.si_signo: 11, info.si_code: 0, info.si_errno: 0, cursig: 11
    sigpend: <>
    sighold: <>
    pid: 39144, ppid: 39053, pgrp: 39143, sid: 39053
--
  CORE                 336  PRSTATUS
    info.si_signo: 11, info.si_code: 0, info.si_errno: 0, cursig: 11
    sigpend: <>
    sighold: <>
    pid: 39143, ppid: 39053, pgrp: 39143, sid: 39053

The implementation needs to:

  1. Add a struct drgn_thread *drgn_program_main_thread(struct drgn_program *prog) function to libdrgn.
  2. Get the main thread as described above. This could either be cached (e.g., as part of drgn_program_cache_core_dump_notes() for core dumps and on the first call to drgn_program_main_thread() for live processes) or recreated every time.
  3. Add the drgn.Program.main_thread() method to the Python bindings.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions