1 /* radare - LGPL - Copyright 2009-2017 pancake */
2 
3 #include <r_debug.h>
4 #include <config.h>
5 
6 static RDebugPlugin *debug_static_plugins[] = {
7 	R_DEBUG_STATIC_PLUGINS
8 };
9 
r_debug_plugin_init(RDebug * dbg)10 R_API void r_debug_plugin_init(RDebug *dbg) {
11 	int i;
12 	dbg->plugins = r_list_newf (free);
13 	for (i = 0; debug_static_plugins[i]; i++) {
14 		r_debug_plugin_add (dbg, debug_static_plugins[i]);
15 	}
16 }
17 
r_debug_use(RDebug * dbg,const char * str)18 R_API bool r_debug_use(RDebug *dbg, const char *str) {
19 	if (dbg && str) {
20 		RDebugPlugin *h;
21 		RListIter *iter;
22 		r_list_foreach (dbg->plugins, iter, h) {
23 			if (h->name && !strcmp (str, h->name)) {
24 				dbg->h = h;
25 				if (dbg->anal && dbg->anal->cur) {
26 					r_debug_set_arch (dbg, dbg->anal->cur->arch, dbg->bits);
27 				}
28 				dbg->bp->breakpoint = dbg->h->breakpoint;
29 				dbg->bp->user = dbg;
30 			}
31 		}
32 	}
33 	if (dbg && dbg->h && dbg->h->reg_profile) {
34 		char *p = dbg->h->reg_profile (dbg);
35 		if (p) {
36 			r_reg_set_profile_string (dbg->reg, p);
37 			if (dbg->anal && dbg->reg != dbg->anal->reg) {
38 				r_reg_free (dbg->anal->reg);
39 				dbg->anal->reg = dbg->reg;
40 			}
41 			if (dbg->h->init) {
42 				dbg->h->init (dbg);
43 			}
44 			r_reg_set_profile_string (dbg->reg, p);
45 			free (p);
46 		} else {
47 			eprintf ("Cannot retrieve reg profile from debug plugin (%s)\n", dbg->h->name);
48 		}
49 	}
50 	return (dbg && dbg->h);
51 }
52 
r_debug_plugin_list(RDebug * dbg,int mode)53 R_API bool r_debug_plugin_list(RDebug *dbg, int mode) {
54 	char spaces[16];
55 	int count = 0;
56 	memset (spaces, ' ', 15);
57 	spaces[15] = 0;
58 	RDebugPlugin *h;
59 	RListIter *iter;
60 	PJ *pj = NULL;
61 	if (mode == 'j') {
62 		pj = dbg->pj;
63 		if (!pj) {
64 			return false;
65 		}
66 		pj_a (pj);
67 	}
68 	r_list_foreach (dbg->plugins, iter, h) {
69 		int sp = 8-strlen (h->name);
70 		spaces[sp] = 0;
71 		if (mode == 'q') {
72 			dbg->cb_printf ("%s\n", h->name);
73 		} else if (mode == 'j') {
74 			pj_o (pj);
75 			pj_ks (pj, "name", h->name);
76 			pj_ks (pj, "license", h->license);
77 			pj_end (pj);
78 		} else {
79 			dbg->cb_printf ("%d  %s  %s %s%s\n",
80 					count, (h == dbg->h)? "dbg": "---",
81 					h->name, spaces, h->license);
82 		}
83 		spaces[sp] = ' ';
84 		count++;
85 	}
86 	if (mode == 'j') {
87 		pj_end (pj);
88 		dbg->cb_printf ("%s\n", pj_string (pj));
89 	}
90 	return true;
91 }
92 
r_debug_plugin_add(RDebug * dbg,RDebugPlugin * foo)93 R_API bool r_debug_plugin_add(RDebug *dbg, RDebugPlugin *foo) {
94 	if (!dbg || !foo || !foo->name) {
95 		return false;
96 	}
97 	RDebugPlugin *dp = R_NEW (RDebugPlugin);
98 	memcpy (dp, foo, sizeof (RDebugPlugin));
99 	r_list_append (dbg->plugins, dp);
100 	return true;
101 }
102 
r_debug_plugin_set_reg_profile(RDebug * dbg,const char * profile)103 R_API bool r_debug_plugin_set_reg_profile(RDebug *dbg, const char *profile) {
104 	char *str = r_file_slurp (profile, NULL);
105 	if (!str) {
106 		eprintf ("r_debug_plugin_set_reg_profile: Cannot find '%s'\n", profile);
107 		return false;
108 	}
109 	if (dbg && dbg->h && dbg->h->set_reg_profile) {
110 		return dbg->h->set_reg_profile (str);
111 	}
112 	free (str);
113 	return false;
114 }
115