Skip to content

Commit

Permalink
Merge pull request #10 from doronz88/feature/darwin_process_taskinfo
Browse files Browse the repository at this point in the history
darwin_processes: add get_task_all_info
  • Loading branch information
doronz88 authored Feb 3, 2022
2 parents faab0f2 + 738f8c5 commit dbc618e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/pyzshell/pyzshell/darwin_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pyzshell.exceptions import ZShellError
from pyzshell.processes import Processes
from pyzshell.structs.darwin import pid_t, MAXPATHLEN, PROC_PIDLISTFDS, proc_fdinfo, PROX_FDTYPE_VNODE, \
vnode_fdinfowithpath, PROC_PIDFDVNODEPATHINFO
vnode_fdinfowithpath, PROC_PIDFDVNODEPATHINFO, proc_taskallinfo, PROC_PIDTASKALLINFO

Process = namedtuple('Process', 'pid path')
Fd = namedtuple('Fd', 'fd path')
Expand Down Expand Up @@ -43,6 +43,12 @@ def get_fds(self, pid: int) -> Optional[list]:

return result

def get_task_all_info(self, pid: int):
with self._client.safe_malloc(proc_taskallinfo.sizeof()) as pti:
if not self._client.symbols.proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, pti, proc_taskallinfo.sizeof()):
raise ZShellError('proc_pidinfo(PROC_PIDTASKALLINFO) failed')
return proc_taskallinfo.parse_stream(pti)

def list(self) -> list:
n = self._client.symbols.proc_listallpids(0, 0)
pid_buf_size = pid_t.sizeof() * n
Expand Down
55 changes: 55 additions & 0 deletions src/pyzshell/pyzshell/structs/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,61 @@
PROX_FDTYPE_PIPE = 6
PROX_FDTYPE_FSEVENTS = 7

MAXCOMLEN = 16

proc_bsdinfo = Struct(
'pbi_flags' / Int32ul, # 64bit emulated etc
'pbi_status' / Int32ul,
'pbi_xstatus' / Int32ul,
'pbi_pid' / Int32ul,
'pbi_ppid' / Int32ul,
'pbi_uid' / uid_t,
'pbi_gid' / gid_t,
'pbi_ruid' / uid_t,
'pbi_rgid' / gid_t,
'pbi_svuid' / uid_t,
'pbi_svgid' / gid_t,
'rfu_1' / Int32ul, # reserved
'_pbi_comm' / Bytes(MAXCOMLEN),
'pbi_comm' / Computed(lambda x: x._pbi_comm.split(b'\x00', 1)[0].decode()),
'_pbi_name' / Bytes(2 * MAXCOMLEN), # empty if no name is registered
'pbi_name' / Computed(lambda x: x._pbi_name.split(b'\x00', 1)[0].decode()),
'pbi_nfiles' / Int32ul,
'pbi_pgid' / Int32ul,
'pbi_pjobc' / Int32ul,
'e_tdev' / Int32ul, # controlling tty dev
'e_tpgid' / Int32ul, # tty process group id
'pbi_nice' / Int32sl,
'pbi_start_tvsec' / Int64ul,
'pbi_start_tvusec' / Int64ul,
)

proc_taskinfo = Struct(
'pti_virtual_size' / Int64ul, # virtual memory size (bytes)
'pti_resident_size' / Int64ul, # resident memory size (bytes)
'pti_total_user' / Int64ul, # total time
'pti_total_system' / Int64ul,
'pti_threads_user' / Int64ul, # existing threads only
'pti_threads_system' / Int64ul,
'pti_policy' / Int32sl, # default policy for new threads
'pti_faults' / Int32sl, # number of page faults
'pti_pageins' / Int32sl, # number of actual pageins
'pti_cow_faults' / Int32sl, # number of copy-on-write faults
'pti_messages_sent' / Int32sl, # number of messages sent
'pti_messages_received' / Int32sl, # number of messages received
'pti_syscalls_mach' / Int32sl, # number of mach system calls
'pti_syscalls_unix' / Int32sl, # number of unix system calls
'pti_csw' / Int32sl, # number of context switches
'pti_threadnum' / Int32sl, # number of threads in the task
'pti_numrunning' / Int32sl, # number of running threads
'pti_priority' / Int32sl, # task priority
)

proc_taskallinfo = Struct(
'pbsd' / proc_bsdinfo,
'ptinfo' / proc_taskinfo,
)

proc_regioninfo = Struct(
'pri_protection' / Int32ul,
'pri_max_protection' / Int32ul,
Expand Down

0 comments on commit dbc618e

Please sign in to comment.