Skip to content

Commit 3bb1e81

Browse files
committed
Updates to loaded libs
1 parent c16ffdb commit 3bb1e81

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

panda/plugins/loaded_libs/loaded_libs.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ void uninit_plugin(void *);
1414
#include "osi/osi_types.h"
1515
#include "osi/osi_ext.h"
1616
}
17+
bool debug = false;
18+
#define dprintf(...) if (debug) { printf(__VA_ARGS__); fflush(stdout); }
1719

1820
#include<map>
1921
#include<vector>
@@ -24,62 +26,76 @@ using namespace std;
2426
typedef target_ulong Asid;
2527

2628
void cleanup_osi(OsiProc *current, OsiThread *thread, GArray *ms) {
27-
if (current) free_osiproc(current);
28-
if (thread) free_osithread(thread);
29-
if (ms) cleanup_garray(ms);
29+
if (current) {
30+
free_osiproc(current);
31+
}
32+
if (thread) {
33+
free_osithread(thread);
34+
}
35+
if (ms) {
36+
cleanup_garray(ms);
37+
}
3038
}
3139

32-
const char* program_name;
40+
const char * program_name;
3341

3442
uint64_t get_libs_count = 0;
3543
uint64_t get_libs_failed_count = 0;
3644

3745
void get_libs(CPUState *env) {
38-
39-
get_libs_count ++;
46+
get_libs_count++;
4047
bool fail = false;
41-
OsiProc *current = get_current_process(env);
48+
OsiProc * current = get_current_process(env);
4249
if (current == NULL) {
50+
dprintf("[loaded_libs] get_current_process returned NULL\n");
4351
fail = true;
4452
}
4553
if (program_name && strcmp(current->name, program_name)) {
54+
dprintf("[loaded_libs] program_name did not match %s\n", program_name);
4655
fail = true;
4756
}
4857
if (current->pid == 0) {
58+
dprintf("[loaded_libs] current process pid is 0\n");
4959
fail = true;
5060
}
5161
GArray *ms = get_mappings(env, current);
5262
if (ms == NULL) {
63+
dprintf("[loaded_libs] get_mappings failed \n");
5364
fail = true;
5465
}
5566
OsiThread *thread = get_current_thread(env);
5667
if (thread == NULL) {
68+
dprintf("[loaded_libs] get_current_thread is NULL\n");
5769
fail = true;
5870
}
5971
assert (pandalog);
6072

6173
if (fail) {
62-
get_libs_failed_count ++;
74+
get_libs_failed_count++;
6375
}
6476
else {
77+
dprintf("[loaded_libs] get_libs succeeded\n");
6578
Panda__LogEntry ple = PANDA__LOG_ENTRY__INIT;
6679
Panda__LoadedLibs ll = PANDA__LOADED_LIBS__INIT;
6780
Panda__Module** m = (Panda__Module **) malloc (sizeof (Panda__Module *) * ms->len);
6881
for (int i = 0; i < ms->len; i++) {
6982
OsiModule *module = &g_array_index(ms, OsiModule, i);
7083
m[i] = (Panda__Module *) malloc (sizeof (Panda__Module));
7184
*(m[i]) = PANDA__MODULE__INIT;
72-
if (module->name == 0x0)
73-
m[i]->file = strdup("none");
74-
else
75-
m[i]->name = strdup(module->name);
76-
77-
if (module->file == 0x0)
85+
if (module->name == 0x0) {
86+
m[i]->file = strdup("none");
87+
}
88+
else {
89+
m[i]->name = strdup(module->name);
90+
}
91+
if (module->file == 0x0) {
7892
m[i]->file = strdup("none");
79-
else
93+
}
94+
else {
8095
m[i]->file = strdup(module->file);
81-
m[i]->base_addr = module->base;
82-
m[i]->size = module->size;
96+
}
97+
m[i]->base_addr = module -> base;
98+
m[i]->size = module -> size;
8399
}
84100
ll.modules = m;
85101
ll.n_modules = ms->len;
@@ -105,19 +121,20 @@ void get_libs(CPUState *env) {
105121
}
106122
free(m);
107123
}
108-
109124
cleanup_osi(current, thread, ms);
110125
}
111126

112127
// 9 long sys_mmap(
113128
void mmap_return(CPUState *cpu, target_ulong pc, unsigned long addr, unsigned long length, unsigned long prot, unsigned long flags, unsigned long fd, unsigned long offset) {
129+
dprintf("[loaded_libs] mmap_return\n");
114130
get_libs(cpu);
115131
}
116132

117133
uint64_t bb_count = 0;
118134
void before_block(CPUState *env, TranslationBlock *tb) {
119135
// check up on module list every 50 bb
120-
bb_count ++;
136+
dprintf("[loaded_libs] bb_count = %lu\n", bb_count);
137+
bb_count++;
121138
if ((bb_count % 100) == 0) {
122139
get_libs(env);
123140
}
@@ -137,6 +154,7 @@ bool init_plugin(void *self) {
137154
panda_arg_list *args;
138155
args = panda_get_args("loaded_libs");
139156
program_name = panda_parse_string_opt(args, "program_name", NULL, "program name to collect libraries for");
157+
debug = panda_parse_bool_opt(args, "debug", "enable debug output");
140158
return true;
141159
#else
142160
/* #error "No on_sys_mmap_return for target" */
@@ -146,7 +164,7 @@ bool init_plugin(void *self) {
146164
}
147165

148166
void uninit_plugin(void *self) {
149-
cout << "get_libs_count = " << get_libs_count << "\n";
150-
cout << "get_libs_failed_count = " << get_libs_failed_count << "\n";
151-
cout << "frac = " << ((float) get_libs_failed_count) / get_libs_count << "\n";
167+
printf("get_libs_count = %ld\n", get_libs_count);
168+
printf("get_libs_failed_count = %ld\n", get_libs_failed_count);
169+
printf("frac = %.2f%%\n", (float) get_libs_failed_count / get_libs_count);
152170
}

0 commit comments

Comments
 (0)