Skip to content

Commit

Permalink
Improve linux native debug dp output ##debug
Browse files Browse the repository at this point in the history
Previously, the command didn't show the pid's path, replaced the path
field with current/ppid, and showed the ppid instead of only showing the
requested process and the children of the requested process.
  • Loading branch information
yossizap authored and radare committed Nov 29, 2019
1 parent e1648b8 commit a99c802
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 88 deletions.
71 changes: 1 addition & 70 deletions libr/debug/p/debug_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,76 +772,7 @@ static RList *r_debug_native_pids (RDebug *dbg, int pid) {
#elif __WINDOWS__
return w32_pid_list (dbg, pid, list);
#elif __linux__
list->free = (RListFree)&r_debug_pid_free;
DIR *dh;
struct dirent *de;
char *ptr, st, buf[1024];
int i, uid;
if (pid) {
/* add the requested pid. should we do this? we don't even know if it's valid still.. */
r_list_append (list, r_debug_pid_new ("(current)", pid, 0, 's', 0));
}
dh = opendir ("/proc");
if (!dh) {
r_sys_perror ("opendir /proc");
r_list_free (list);
return NULL;
}
while ((de = readdir (dh))) {
uid = 0;
st = ' ';
/* for each existing pid file... */
i = atoi (de->d_name);
if (i <= 0) {
continue;
}

/* try to read the status */
buf[0] = 0;
if (procfs_pid_slurp (i, "status", buf, sizeof (buf)) == -1) {
continue;
}
buf[sizeof (buf) - 1] = 0;

// get process State
ptr = strstr (buf, "State:");
if (ptr) {
st = ptr[7];
}
/* look for the parent process id */
ptr = strstr (buf, "PPid:");
if (pid && ptr) {
int ppid = atoi (ptr + 5);

/* if this is the requested process... */
if (i == pid) {
// append it to the list with parent
r_list_append (list, r_debug_pid_new (
"(ppid)", ppid, uid, st, 0));
}

/* ignore it if it is not one of our children */
if (ppid != pid) {
continue;
}
}

// get process Uid
ptr = strstr (buf, "Uid:");
if (ptr) {
uid = atoi (ptr + 4);
}
// TODO: add support for gid in RDebugPid.new()
// ptr = strstr (buf, "Gid:");
// if (ptr) {
// gid = atoi (ptr + 4);
// }
if (procfs_pid_slurp (i, "cmdline", buf, sizeof (buf)) == -1) {
continue;
}
r_list_append (list, r_debug_pid_new (buf, i, uid, st, 0));
}
closedir (dh);
return linux_pid_list (pid, list);
#else /* rest is BSD */
#ifdef __NetBSD__
# define KVM_OPEN_FLAG KVM_NO_FILES
Expand Down
47 changes: 44 additions & 3 deletions libr/debug/p/native/linux/linux_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ RDebugPid *fill_pid_info(const char *info, const char *path, int tid) {
break;
}
}
ptr = strstr (info, "PPid:");
if (ptr) {
pid_info->ppid = atoi (ptr + 5);
}
ptr = strstr (info, "Uid:");
if (ptr) {
pid_info->uid = atoi (ptr + 5);
Expand All @@ -611,9 +615,46 @@ RDebugPid *fill_pid_info(const char *info, const char *path, int tid) {
return pid_info;
}

RList *linux_pid_list(int pid, RList *list) {
list->free = (RListFree)&r_debug_pid_free;
DIR *dh = NULL;
struct dirent *de = NULL;
char path[PATH_MAX], info[PATH_MAX];
int i = -1;
RDebugPid *pid_info = NULL;
dh = opendir ("/proc");
if (!dh) {
r_sys_perror ("opendir /proc");
r_list_free (list);
return NULL;
}
while ((de = readdir (dh))) {
path[0] = 0;
info[0] = 0;
// For each existing pid file
if ((i = atoi (de->d_name)) <= 0) {
continue;
}

procfs_pid_slurp (i, "cmdline", path, sizeof (path));
if (!procfs_pid_slurp (i, "status", info, sizeof (info))) {
// Get information about pid (status, pc, etc.)
pid_info = fill_pid_info (info, path, i);
} else {
pid_info = r_debug_pid_new (path, i, 0, R_DBG_PROC_STOP, 0);
}
// Only add the request pid and it's child processes
if (i == pid || pid_info->ppid == pid) {
r_list_append (list, pid_info);
}
}
closedir (dh);
return list;
}

RList *linux_thread_list(int pid, RList *list) {
int i, thid = 0;
char *ptr, buf[1024];
char *ptr, buf[PATH_MAX];

if (!pid) {
r_list_free (list);
Expand All @@ -631,7 +672,7 @@ RList *linux_thread_list(int pid, RList *list) {
continue;
}
int tid = atoi (de->d_name);
char info[1024];
char info[PATH_MAX];
int uid = 0;
if (!procfs_pid_slurp (tid, "status", info, sizeof (info))) {
ptr = strstr (info, "Uid:");
Expand All @@ -645,7 +686,7 @@ RList *linux_thread_list(int pid, RList *list) {
// If we want to attach to just one thread, don't attach to the parent
continue;
}
}
}
}

if (procfs_pid_slurp (tid, "comm", buf, sizeof (buf)) == -1) {
Expand Down
31 changes: 16 additions & 15 deletions libr/debug/p/native/linux/linux_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,19 @@ typedef ut64 mips64_regs_t [274];
#endif

//API
bool linux_set_options (RDebug *dbg, int pid);
int linux_step (RDebug *dbg);
RDebugReasonType linux_ptrace_event (RDebug *dbg, int pid, int status);
int linux_attach (RDebug *dbg, int pid);
RDebugInfo *linux_info (RDebug *dbg, const char *arg);
RList *linux_thread_list (int pid, RList *list);
bool linux_select_thread (RDebug *dbg, int pid, int tid);
RDebugPid *fill_pid_info (const char *info, const char *path, int tid);
int linux_reg_read (RDebug *dbg, int type, ut8 *buf, int size);
int linux_reg_write (RDebug *dbg, int type, const ut8 *buf, int size);
RList *linux_desc_list (int pid);
int linux_handle_signals (RDebug *dbg);
int linux_dbg_wait (RDebug *dbg, int pid);
char *linux_reg_profile (RDebug *dbg);
int match_pid (const void *pid_o, const void *th_o);
bool linux_set_options(RDebug *dbg, int pid);
int linux_step(RDebug *dbg);
RDebugReasonType linux_ptrace_event(RDebug *dbg, int pid, int status);
int linux_attach(RDebug *dbg, int pid);
RDebugInfo *linux_info(RDebug *dbg, const char *arg);
RList *linux_pid_list(int pid, RList *list);
RList *linux_thread_list(int pid, RList *list);
bool linux_select_thread(RDebug *dbg, int pid, int tid);
RDebugPid *fill_pid_info(const char *info, const char *path, int tid);
int linux_reg_read(RDebug *dbg, int type, ut8 *buf, int size);
int linux_reg_write(RDebug *dbg, int type, const ut8 *buf, int size);
RList *linux_desc_list(int pid);
int linux_handle_signals(RDebug *dbg);
int linux_dbg_wait(RDebug *dbg, int pid);
char *linux_reg_profile(RDebug *dbg);
int match_pid(const void *pid_o, const void *th_o);
1 change: 1 addition & 0 deletions libr/include/r_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ typedef struct r_debug_plugin_t {
// TODO: rename to r_debug_process_t ? maybe a thread too ?
typedef struct r_debug_pid_t {
int pid;
int ppid;
char status; /* stopped, running, zombie, sleeping ,... */
int runnable; /* when using 'run', 'continue', .. this proc will be runnable */
bool signalled;
Expand Down

0 comments on commit a99c802

Please sign in to comment.