1 /* radare - LGPL - Copyright 2009-2017 - pancake */
2 
3 #include <r_debug.h>
4 
r_debug_pid_new(const char * path,int pid,int uid,char status,ut64 pc)5 R_API RDebugPid *r_debug_pid_new(const char *path, int pid, int uid, char status, ut64 pc) {
6 	RDebugPid *p = R_NEW0 (RDebugPid);
7 	if (!p) {
8 		return NULL;
9 	}
10 	p->path = strdup (path);
11 	p->pid = pid;
12 	p->uid = uid;
13 	p->status = status;
14 	p->runnable = true;
15 	p->pc = pc;
16 	return p;
17 }
18 
r_debug_pid_free(RDebugPid * pid)19 R_API RDebugPid *r_debug_pid_free(RDebugPid *pid) {
20 	free (pid->path);
21 	free (pid);
22 	return NULL;
23 }
24 
r_debug_pids(RDebug * dbg,int pid)25 R_API RList *r_debug_pids(RDebug *dbg, int pid) {
26 	if (dbg && dbg->h && dbg->h->pids) {
27 		return dbg->h->pids (dbg, pid);
28 	}
29 	return NULL;
30 }
31 
32 // TODO: deprecate list/iterate functions from core apis? keep them for easiness?
r_debug_pid_list(RDebug * dbg,int pid,char fmt)33 R_API int r_debug_pid_list(RDebug *dbg, int pid, char fmt) {
34 	RList *list;
35 	RListIter *iter;
36 	RDebugPid *p;
37 	if (dbg && dbg->h && dbg->h->pids) {
38 		list = dbg->h->pids (dbg, R_MAX (0, pid));
39 		if (!list) {
40 			return false;
41 		}
42 		PJ *j = pj_new ();
43 		pj_a (j);
44 		r_list_foreach (list, iter, p) {
45 			switch (fmt) {
46 			case 'j':
47 				pj_o (j);
48 				pj_kb (j, "current", dbg->pid == p->pid);
49 				pj_ki (j, "ppid", p->ppid);
50 				pj_ki (j, "pid", p->pid);
51 				pj_ki (j, "uid", p->uid);
52 				pj_ks (j, "status", &p->status);
53 				pj_ks (j, "path", p->path);
54 				pj_end (j);
55 				break;
56 			default:
57 				dbg->cb_printf (" %c %d ppid:%d uid:%d %c %s\n",
58 					dbg->pid == p->pid? '*': '-',
59 					p->pid, p->ppid, p->uid, p->status, p->path);
60 				break;
61 			}
62 		}
63 		pj_end (j);
64 		if (fmt == 'j') {
65 			dbg->cb_printf ("%s", pj_string (j));
66 		}
67 		pj_free (j);
68 		r_list_free (list);
69 	}
70 	return false;
71 }
72 
r_debug_thread_list(RDebug * dbg,int pid,char fmt)73 R_API int r_debug_thread_list(RDebug *dbg, int pid, char fmt) {
74 	RList *list;
75 	RListIter *iter;
76 	RDebugPid *p;
77 	RAnalFunction *fcn = NULL;
78 	RDebugMap *map = NULL;
79 	RStrBuf *path = NULL;
80 	if (pid == -1) {
81 		return false;
82 	}
83 	if (dbg && dbg->h && dbg->h->threads) {
84 		list = dbg->h->threads (dbg, pid);
85 		if (!list) {
86 			return false;
87 		}
88 		PJ *j = pj_new ();
89 		pj_a (j);
90 		r_list_foreach (list, iter, p) {
91 			path = r_strbuf_new ("");
92 			if (p->pc != 0) {
93 				map = r_debug_map_get (dbg, p->pc);
94 				if (map && map->name && map->name[0]) {
95 					r_strbuf_appendf (path, "%s ", map->name);
96 				}
97 
98 				r_strbuf_appendf (path, "(0x%" PFMT64x ")", p->pc);
99 
100 				fcn = r_anal_get_fcn_in (dbg->anal, p->pc, 0);
101 				if (fcn) {
102 					r_strbuf_appendf (path, " in %s+0x%" PFMT64x, fcn->name, (p->pc - fcn->addr));
103 				}
104 			}
105 			switch (fmt) {
106 			case 'j':
107 				pj_o (j);
108 				pj_kb (j, "current", dbg->tid == p->pid);
109 				pj_ki (j, "pid", p->pid);
110 				pj_ks (j, "status", &p->status);
111 				pj_ks (j, "path", r_strbuf_get (path));
112 				pj_end (j);
113 				break;
114 			default:
115 				dbg->cb_printf (" %c %d %c %s\n",
116 					dbg->tid == p->pid? '*': '-',
117 					p->pid, p->status, r_strbuf_get (path));
118 				break;
119 			}
120 			r_strbuf_free (path);
121 		}
122 		pj_end (j);
123 		if (fmt == 'j') {
124 			dbg->cb_printf ("%s", pj_string (j));
125 		}
126 		pj_free (j);
127 		r_list_free (list);
128 	}
129 	return false;
130 }
131 
132 /* processes */
r_debug_pid_parent(RDebugPid * pid)133 R_API int r_debug_pid_parent(RDebugPid *pid) {
134 	// fork in child
135 	return 0;
136 }
137 
138 #if 0
139 R_API int r_debug_pid_del(struct r_debug_t *dbg) {
140 	// kill da child
141 	return true;
142 }
143 
144 /* threads */
145 R_API int r_debug_pid_add_thread(struct r_debug_t *dbg) {
146 	// create a thread in process
147 	return true;
148 }
149 
150 R_API int r_debug_pid_del_thread(struct r_debug_t *dbg) {
151 	// kill a thread in process
152 	return true;
153 }
154 #endif
155 
156 /* status */
r_debug_pid_set_state(struct r_debug_t * dbg,int status)157 R_API int r_debug_pid_set_state(struct r_debug_t *dbg, int status) {
158 	return true;
159 }
160 
161 /* status */
r_debug_pid_get_status(struct r_debug_t * dbg,int pid)162 R_API struct r_debug_pid_t *r_debug_pid_get_status(struct r_debug_t *dbg, int pid) {
163 	return NULL;
164 }
165