@@ -14,6 +14,8 @@ void uninit_plugin(void *);
14
14
#include " osi/osi_types.h"
15
15
#include " osi/osi_ext.h"
16
16
}
17
+ bool debug = false ;
18
+ #define dprintf (...) if (debug) { printf (__VA_ARGS__); fflush (stdout); }
17
19
18
20
#include < map>
19
21
#include < vector>
@@ -24,62 +26,76 @@ using namespace std;
24
26
typedef target_ulong Asid;
25
27
26
28
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
+ }
30
38
}
31
39
32
- const char * program_name;
40
+ const char * program_name;
33
41
34
42
uint64_t get_libs_count = 0 ;
35
43
uint64_t get_libs_failed_count = 0 ;
36
44
37
45
void get_libs (CPUState *env) {
38
-
39
- get_libs_count ++;
46
+ get_libs_count++;
40
47
bool fail = false ;
41
- OsiProc *current = get_current_process (env);
48
+ OsiProc * current = get_current_process (env);
42
49
if (current == NULL ) {
50
+ dprintf (" [loaded_libs] get_current_process returned NULL\n " );
43
51
fail = true ;
44
52
}
45
53
if (program_name && strcmp (current->name , program_name)) {
54
+ dprintf (" [loaded_libs] program_name did not match %s\n " , program_name);
46
55
fail = true ;
47
56
}
48
57
if (current->pid == 0 ) {
58
+ dprintf (" [loaded_libs] current process pid is 0\n " );
49
59
fail = true ;
50
60
}
51
61
GArray *ms = get_mappings (env, current);
52
62
if (ms == NULL ) {
63
+ dprintf (" [loaded_libs] get_mappings failed \n " );
53
64
fail = true ;
54
65
}
55
66
OsiThread *thread = get_current_thread (env);
56
67
if (thread == NULL ) {
68
+ dprintf (" [loaded_libs] get_current_thread is NULL\n " );
57
69
fail = true ;
58
70
}
59
71
assert (pandalog);
60
72
61
73
if (fail) {
62
- get_libs_failed_count ++;
74
+ get_libs_failed_count++;
63
75
}
64
76
else {
77
+ dprintf (" [loaded_libs] get_libs succeeded\n " );
65
78
Panda__LogEntry ple = PANDA__LOG_ENTRY__INIT;
66
79
Panda__LoadedLibs ll = PANDA__LOADED_LIBS__INIT;
67
80
Panda__Module** m = (Panda__Module **) malloc (sizeof (Panda__Module *) * ms->len );
68
81
for (int i = 0 ; i < ms->len ; i++) {
69
82
OsiModule *module = &g_array_index (ms, OsiModule, i);
70
83
m[i] = (Panda__Module *) malloc (sizeof (Panda__Module));
71
84
*(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 ) {
78
92
m[i]->file = strdup (" none" );
79
- else
93
+ }
94
+ else {
80
95
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;
83
99
}
84
100
ll.modules = m;
85
101
ll.n_modules = ms->len ;
@@ -105,19 +121,20 @@ void get_libs(CPUState *env) {
105
121
}
106
122
free (m);
107
123
}
108
-
109
124
cleanup_osi (current, thread, ms);
110
125
}
111
126
112
127
// 9 long sys_mmap(
113
128
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 " );
114
130
get_libs (cpu);
115
131
}
116
132
117
133
uint64_t bb_count = 0 ;
118
134
void before_block (CPUState *env, TranslationBlock *tb) {
119
135
// check up on module list every 50 bb
120
- bb_count ++;
136
+ dprintf (" [loaded_libs] bb_count = %lu\n " , bb_count);
137
+ bb_count++;
121
138
if ((bb_count % 100 ) == 0 ) {
122
139
get_libs (env);
123
140
}
@@ -137,6 +154,7 @@ bool init_plugin(void *self) {
137
154
panda_arg_list *args;
138
155
args = panda_get_args (" loaded_libs" );
139
156
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" );
140
158
return true ;
141
159
#else
142
160
/* #error "No on_sys_mmap_return for target" */
@@ -146,7 +164,7 @@ bool init_plugin(void *self) {
146
164
}
147
165
148
166
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) ;
152
170
}
0 commit comments