@@ -1039,7 +1039,7 @@ void pri_dwarf_plog(const char *file_callee, const char *fn_callee, uint64_t lno
1039
1039
1040
1040
Panda__LogEntry ple = PANDA__LOG_ENTRY__INIT;
1041
1041
// create a call or ret message
1042
- if (isCall){
1042
+ if (isCall) {
1043
1043
ple.dwarf2_call = dwarf;
1044
1044
}
1045
1045
else {
@@ -1850,6 +1850,7 @@ bool load_debug_info(const char *dbg_prefix, const char *basename, uint64_t base
1850
1850
return true ;
1851
1851
}
1852
1852
1853
+ // You need this code to run to fill out TaintQueryPri
1853
1854
bool read_debug_info (const char * dbg_prefix, const char *basename, uint64_t base_address, bool needs_reloc) {
1854
1855
1855
1856
printf (" read_debug_info %s\n " , dbg_prefix);
@@ -1879,6 +1880,7 @@ bool looking_for_libc=false;
1879
1880
const char *libc_host_path=NULL ;
1880
1881
std::string libc_name;
1881
1882
1883
+ // Call back to the loaded plugin
1882
1884
void on_library_load (CPUState *cpu, target_ulong pc, char *guest_lib_name, target_ulong base_addr, target_ulong size) {
1883
1885
printf (" on_library_load guest_lib_name=%s\n " , guest_lib_name);
1884
1886
if (!correct_asid (cpu)) {
@@ -1890,7 +1892,7 @@ void on_library_load(CPUState *cpu, target_ulong pc, char *guest_lib_name, targe
1890
1892
// printf("Trying to load symbols for %s at %#x.\n", lib_name, base_addr);
1891
1893
std::string lib = std::string (guest_lib_name);
1892
1894
std::size_t found = lib.find (guest_debug_path);
1893
- if (found == std::string::npos){
1895
+ if (found == std::string::npos) {
1894
1896
char *lib_name = strdup ((host_mount_path + lib).c_str ());
1895
1897
printf (" access(%s, F_OK): %x\n " , lib_name, access (lib_name, F_OK));
1896
1898
if (access (lib_name, F_OK) == -1 ) {
@@ -1899,9 +1901,7 @@ void on_library_load(CPUState *cpu, target_ulong pc, char *guest_lib_name, targe
1899
1901
}
1900
1902
if (looking_for_libc &&
1901
1903
lib.find (libc_name) != std::string::npos) {
1902
- // if (lib.find("libc-2.13") != std::string::npos) {
1903
1904
lib_name = strdup (libc_host_path);
1904
- // lib_name = strdup("/mnt/lava-32-qcow/usr/lib/debug/lib/i386-linux-gnu/i686/cmov/libc-2.13.so");
1905
1905
printf (" actually loading lib_name = %s\n " , lib_name);
1906
1906
bool needs_reloc = true ; // elf_base != base_addr;
1907
1907
read_debug_info (lib_name, basename (lib_name), base_addr, needs_reloc);
@@ -1931,34 +1931,50 @@ void on_library_load(CPUState *cpu, target_ulong pc, char *guest_lib_name, targe
1931
1931
}
1932
1932
1933
1933
// We want to catch all loaded modules, but don't want to
1934
- // check every single call. This is a compromise -- check
1935
- // every 1000 calls. If we had a callback in OSI for
1936
- // on_library_load we could do away with this hack.
1937
- int mod_check_count = 0 ;
1934
+ // check every single call. We use a callback in OSI for
1935
+ // on_library_load.
1938
1936
bool main_exec_initialized = false ;
1939
- #define MOD_CHECK_FREQ 1000
1940
1937
bool ensure_main_exec_initialized (CPUState *cpu) {
1941
1938
// if (!correct_asid(cpu)) return;
1942
1939
OsiProc *p = get_current_process (cpu);
1943
1940
GArray *libs = NULL ;
1944
1941
libs = get_mappings (cpu, p);
1945
1942
free_osiproc (p);
1946
- if (!libs)
1943
+ if (!libs) {
1944
+ printf (" get_mappings failed\n " );
1947
1945
return false ;
1948
-
1949
- // printf("[ensure_main_exec_initialized] looking at libraries\n");
1946
+ }
1947
+ printf (" [ensure_main_exec_initialized] looking at libraries for %s \n " , proc_to_monitor );
1950
1948
1951
1949
for (unsigned i = 0 ; i < libs->len ; i++) {
1952
1950
char fname[260 ] = {};
1953
1951
OsiModule *m = &g_array_index (libs, OsiModule, i);
1954
- if (!m->file ) continue ;
1955
- if (!m->name ) continue ;
1956
- std::string lib = std::string (m->file );
1957
1952
if (debug) {
1958
- printf (" [ensure_main_exec_initialized] looking at file %s\n " , m->file );
1953
+ printf (" Iteration %d within the for loop of libraries in main_exec_initialized\n " , i);
1954
+ }
1955
+ if (!m->file ) {
1956
+ if (debug) {
1957
+ printf (" Invalid file from OsiModule\n " );
1958
+ }
1959
+ continue ;
1960
+ }
1961
+ if (!m->name ) {
1962
+ if (debug) {
1963
+ printf (" Invalid name from OsiModule\n " );
1964
+ }
1965
+ continue ;
1966
+ }
1967
+ std::string lib = std::string (m->file );
1968
+
1969
+ if (0 != strncmp (m->name , proc_to_monitor, strlen (m->name ))) {
1970
+ if (debug) {
1971
+ printf (" [ensure_main_exec_initialized] looking at file %s, skip this\n " , m->file );
1972
+ printf (" [ensure_main_exec_initialized] looking at name %s, skip this\n " , m->name );
1973
+ }
1974
+ continue ;
1959
1975
}
1960
- if ( 0 != strncmp (m-> name , proc_to_monitor, strlen ( m->name ))) continue ;
1961
- // printf("[ensure_main_exec_initialized] looking at file %s\n", m->file);
1976
+ printf ( " [ensure_main_exec_initialized] Found that file, time to try loading... %s \n " , m->file ) ;
1977
+
1962
1978
// std::size_t found = lib.find(guest_debug_path);
1963
1979
// if (found == std::string::npos) continue;
1964
1980
// std::string host_name = lib.substr(0, found) +
@@ -2062,12 +2078,15 @@ void on_call(CPUState *cpu, target_ulong pc) {
2062
2078
if (it == line_range_list.end () || pc < it->lowpc ){
2063
2079
auto it_dyn = addr_to_dynl_function.find (pc);
2064
2080
if (it_dyn != addr_to_dynl_function.end ()){
2065
- if (debug) printf (" CALL: Found line info for 0x" TARGET_FMT_lx " \n " , pc);
2081
+ if (debug) {
2082
+ printf (" CALL: Found line info for 0x" TARGET_FMT_lx " \n " , pc);
2083
+ }
2066
2084
pri_runcb_on_fn_start (cpu, pc, NULL , it_dyn->second .c_str ());
2067
2085
}
2068
2086
else {
2069
- if (debug)
2087
+ if (debug) {
2070
2088
printf (" CALL: Could not find line info for 0x" TARGET_FMT_lx " \n " , pc);
2089
+ }
2071
2090
}
2072
2091
return ;
2073
2092
}
@@ -2129,7 +2148,7 @@ void on_ret(CPUState *cpu, target_ulong pc_func) {
2129
2148
std::string file_name = it->filename ;
2130
2149
std::string funct_name = funcaddrs[cur_function];
2131
2150
cur_line = it->line_number ;
2132
- // printf("RET: [%s] [0x%llx]-%s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(),cur_function, funct_name.c_str(),cur_line,pc_func);
2151
+ // printf("RET: [%s] [0x%llx]-%s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(),cur_function, funct_name.c_str(),cur_line,pc_func)
2133
2152
if (logCallSites) {
2134
2153
dwarf_log_callsite (cpu, file_name.c_str (), funct_name.c_str (), cur_line, false );
2135
2154
}
@@ -2141,10 +2160,10 @@ void __livevar_iter(CPUState *cpu,
2141
2160
std::vector<VarInfo> vars,
2142
2161
liveVarCB f,
2143
2162
void *args,
2144
- target_ulong fp){
2163
+ target_ulong fp) {
2145
2164
// printf("size of vars: %ld\n", vars.size());
2146
- for (auto it : vars){
2147
- std::string var_name = it.var_name ;
2165
+ for (auto it : vars) {
2166
+ std::string var_name = it.var_name ;
2148
2167
DwarfVarType var_type {type_map[it.fname ][it.cu ][it.var_type ], it.dec_line , var_name};
2149
2168
// enum LocType { LocReg, LocMem, LocConst, LocErr };
2150
2169
target_ulong var_loc;
@@ -2179,14 +2198,14 @@ int livevar_find(CPUState *cpu,
2179
2198
std::vector<VarInfo> vars,
2180
2199
liveVarPred pred,
2181
2200
void *args,
2182
- VarInfo &ret_var){
2201
+ VarInfo &ret_var) {
2183
2202
2184
2203
target_ulong fp = dwarf2_get_cur_fp (cpu, pc);
2185
- if (fp == (target_ulong) -1 ){
2204
+ if (fp == (target_ulong) -1 ) {
2186
2205
printf (" Error: was not able to get the Frame Pointer for the function %s at @ 0x" TARGET_FMT_lx " \n " , funcaddrs[cur_function].c_str (), pc);
2187
2206
return 0 ;
2188
2207
}
2189
- for (auto it : vars){
2208
+ for (auto it : vars) {
2190
2209
target_ulong var_loc;
2191
2210
// process_dwarf_locs(locdesc[i]->ld_s, locdesc[i]->ld_cents);
2192
2211
// printf("\n");
@@ -2206,7 +2225,7 @@ int livevar_find(CPUState *cpu,
2206
2225
* end PPPs
2207
2226
******************************************************************** */
2208
2227
int compare_address (void *var_ty, const char *var_nm, LocType loc_t , target_ulong loc, void *query_address){
2209
- switch (loc_t ){
2228
+ switch (loc_t ) {
2210
2229
case LocReg:
2211
2230
break ;
2212
2231
case LocMem:
@@ -2238,7 +2257,7 @@ void dwarf_get_vma_symbol (CPUState *cpu, target_ulong pc, target_ulong vma, cha
2238
2257
2239
2258
// VarInfo ret_var = VarInfo(NULL, NULL, NULL, 0);
2240
2259
VarInfo ret_var;
2241
- if (livevar_find (cpu, pc, funcvars[fn_address], compare_address, (void *) &vma, ret_var)){
2260
+ if (livevar_find (cpu, pc, funcvars[fn_address], compare_address, (void *) &vma, ret_var)) {
2242
2261
*symbol_name = (char *)ret_var.var_name .c_str ();
2243
2262
return ;
2244
2263
}
@@ -2257,7 +2276,7 @@ void dwarf_get_pc_source_info(CPUState *cpu, target_ulong pc, SrcInfo *info, int
2257
2276
return ;
2258
2277
}
2259
2278
auto it = std::lower_bound (line_range_list.begin (), line_range_list.end (), pc, CompareRangeAndPC ());
2260
- if (it == line_range_list.end () || pc < it->lowpc ) {
2279
+ if (it == line_range_list.end () || pc < it->lowpc ) {
2261
2280
auto it_dyn = addr_to_dynl_function.find (pc);
2262
2281
if (it_dyn != addr_to_dynl_function.end ()){
2263
2282
// printf("In a a plt function\n");
@@ -2272,7 +2291,7 @@ void dwarf_get_pc_source_info(CPUState *cpu, target_ulong pc, SrcInfo *info, int
2272
2291
return ;
2273
2292
}
2274
2293
2275
- if (it->lowpc == it->highpc ){
2294
+ if (it->lowpc == it->highpc ) {
2276
2295
// printf("In a a plt function\n");
2277
2296
*rc = 1 ;
2278
2297
return ;
@@ -2289,11 +2308,11 @@ void dwarf_get_pc_source_info(CPUState *cpu, target_ulong pc, SrcInfo *info, int
2289
2308
void dwarf_all_livevar_iter (CPUState *cpu,
2290
2309
target_ulong pc,
2291
2310
liveVarCB f,
2292
- void *args){
2311
+ void *args) {
2293
2312
// void (*f)(const char *var_ty, const char *var_nm, LocType loc_t, target_ulong loc)){
2294
- if (inExecutableSource){
2313
+ if (inExecutableSource) {
2295
2314
target_ulong fp = dwarf2_get_cur_fp (cpu, pc);
2296
- if (fp == (target_ulong) -1 ){
2315
+ if (fp == (target_ulong) -1 ) {
2297
2316
printf (" Error: was not able to get the Frame Pointer for the function %s at @ 0x" TARGET_FMT_lx " \n " ,
2298
2317
funcaddrs[cur_function].c_str (), pc);
2299
2318
return ;
@@ -2307,8 +2326,10 @@ void dwarf_all_livevar_iter(CPUState *cpu,
2307
2326
void dwarf_funct_livevar_iter (CPUState *cpu,
2308
2327
target_ulong pc,
2309
2328
liveVarCB f,
2310
- void *args){
2311
- // printf("iterating through live vars\n");
2329
+ void *args) {
2330
+ if (debug) {
2331
+ printf (" iterating through live vars\n " );
2332
+ }
2312
2333
if (inExecutableSource) {
2313
2334
target_ulong fp = dwarf2_get_cur_fp (cpu, pc);
2314
2335
if (fp == (target_ulong) -1 ){
@@ -2328,13 +2349,15 @@ void dwarf_global_livevar_iter(CPUState *cpu,
2328
2349
}
2329
2350
2330
2351
bool translate_callback_dwarf (CPUState *cpu, target_ulong pc) {
2331
- if (!correct_asid (cpu)) return false ;
2332
-
2352
+ if (!correct_asid (cpu)) {
2353
+ return false ;
2354
+ }
2333
2355
auto it2 = std::lower_bound (line_range_list.begin (), line_range_list.end (), pc, CompareRangeAndPC ());
2334
2356
// after the call to lower_bound the `pc` should be between it2->lowpc and it2->highpc
2335
2357
// if it2 == line_range_list.end() we know we definitely didn't find out pc in our line_range_list
2336
- if (it2 == line_range_list.end () || pc < it2->lowpc )
2358
+ if (it2 == line_range_list.end () || pc < it2->lowpc ) {
2337
2359
return false ;
2360
+ }
2338
2361
return true ;
2339
2362
/*
2340
2363
// This is just the linear search to confirm binary search (lower_bound) is
@@ -2350,10 +2373,13 @@ bool translate_callback_dwarf(CPUState *cpu, target_ulong pc) {
2350
2373
2351
2374
int exec_callback_dwarf (CPUState *cpu, target_ulong pc) {
2352
2375
inExecutableSource = false ;
2353
- if (!correct_asid (cpu)) return 0 ;
2376
+ if (!correct_asid (cpu)) {
2377
+ return 0 ;
2378
+ }
2354
2379
auto it2 = std::lower_bound (line_range_list.begin (), line_range_list.end (), pc, CompareRangeAndPC ());
2355
- if (it2 == line_range_list.end () || pc < it2->lowpc )
2380
+ if (it2 == line_range_list.end () || pc < it2->lowpc ) {
2356
2381
return 0 ;
2382
+ }
2357
2383
inExecutableSource = true ;
2358
2384
if (it2->lowpc == it2->highpc ) {
2359
2385
inExecutableSource = false ;
@@ -2364,15 +2390,17 @@ int exec_callback_dwarf(CPUState *cpu, target_ulong pc) {
2364
2390
cur_line = it2->line_number ;
2365
2391
2366
2392
// printf("[%s] [0x%llx]-%s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(),cur_function, funct_name.c_str(),cur_line,pc);
2367
- if (funcaddrs.find (cur_function) == funcaddrs.end ())
2393
+ if (funcaddrs.find (cur_function) == funcaddrs.end ()) {
2368
2394
return 0 ;
2369
- if (cur_function == 0 )
2395
+ }
2396
+ if (cur_function == 0 ) {
2370
2397
return 0 ;
2398
+ }
2371
2399
// printf("[%s] [0x%llx]-%s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(),cur_function, funct_name.c_str(),cur_line,pc);
2372
2400
// __livevar_iter(env, pc, funcvars[cur_function], push_var_if_live);
2373
2401
// __livevar_iter(env, pc, global_var_list, push_var_if_live);
2374
2402
// __livevar_iter(env, pc, global_var_list, print_var_if_live);
2375
- if (cur_line != prev_line){
2403
+ if (cur_line != prev_line) {
2376
2404
// printf("[%s] %s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(), funct_name.c_str(),cur_line,pc);
2377
2405
pri_runcb_on_after_line_change (cpu, pc, prev_file_name.c_str (), prev_funct_name.c_str (), prev_line);
2378
2406
pri_runcb_on_before_line_change (cpu, pc, file_name.c_str (), funct_name.c_str (), cur_line);
@@ -2385,7 +2413,7 @@ int exec_callback_dwarf(CPUState *cpu, target_ulong pc) {
2385
2413
prev_function = cur_function;
2386
2414
prev_line = cur_line;
2387
2415
}
2388
- // if (funcaddrs.find(pc) != funcaddrs.end()){
2416
+ // if (funcaddrs.find(pc) != funcaddrs.end()) {
2389
2417
// on_call(env, pc);
2390
2418
// }
2391
2419
return 0 ;
@@ -2412,24 +2440,27 @@ uint32_t guest_strncpy(CPUState *cpu, char *buf, size_t maxlen, target_ulong gue
2412
2440
typedef void (* on_proc_change_t )(CPUState *env, target_ulong asid, OsiProc *proc);
2413
2441
2414
2442
void handle_asid_change (CPUState *cpu, target_ulong asid, OsiProc *p) {
2415
- // printf ("handle_asid_change\n");
2416
2443
if (!p) { return ; }
2417
2444
if (!p->name ) { return ; }
2418
2445
if (debug) {
2419
2446
printf (" p-name: %s proc-to-monitor: %s\n " , p->name , proc_to_monitor);
2420
2447
}
2421
- // printf ("...really\n");
2422
- // if (strcmp(p->name, proc_to_monitor) != 0) {
2423
2448
if (strncmp (p->name , proc_to_monitor, strlen (p->name )) == 0 ) {
2424
2449
target_ulong current_asid = panda_current_asid (cpu);
2425
2450
monitored_asid.insert (current_asid);
2426
2451
printf (" monitoring asid " TARGET_FMT_lx " \n " , current_asid);
2427
2452
}
2428
2453
if (correct_asid (cpu) && !main_exec_initialized){
2454
+ if (debug) {
2455
+ printf (" correct_asid, executing main_exec_initialized\n " );
2456
+ }
2429
2457
main_exec_initialized = ensure_main_exec_initialized (cpu);
2430
2458
}
2459
+ if (!main_exec_initialized) {
2460
+ printf (" The ensure_main_exec_intialized function failed on handle_asid_change\n " );
2461
+ // exit(1);
2462
+ }
2431
2463
// free_osiproc(p);
2432
-
2433
2464
}
2434
2465
// XXX: osi_foo is largetly commented out and basically does nothing
2435
2466
// I am keeping it here as a reminder of maybe tracking of a data structure
@@ -2502,9 +2533,8 @@ void osi_foo(CPUState *cpu, TranslationBlock *tb) {
2502
2533
2503
2534
return ;
2504
2535
}
2505
-
2506
-
2507
2536
#endif
2537
+
2508
2538
bool init_plugin (void *self) {
2509
2539
#if defined(TARGET_I386)
2510
2540
panda_arg_list *args_gen = panda_get_args (" general" );
@@ -2517,6 +2547,7 @@ bool init_plugin(void *self) {
2517
2547
// monitored_asid = 0;
2518
2548
}
2519
2549
panda_arg_list *args = panda_get_args (" dwarf2" );
2550
+ debug = panda_parse_bool_opt (args, " debug" , " enable debug output" );
2520
2551
guest_debug_path = panda_parse_string_req (args, " g_debugpath" , " path to binary/build dir on guest machine" );
2521
2552
host_debug_path = panda_parse_string_req (args, " h_debugpath" , " path to binary/build dir on host machine" );
2522
2553
host_mount_path = panda_parse_string_opt (args, " host_mount_path" , " dbg" , " path to mounted guest file system" );
@@ -2540,7 +2571,7 @@ bool init_plugin(void *self) {
2540
2571
panda_require (" pri" );
2541
2572
panda_require (" asidstory" );
2542
2573
2543
- // panda_require("osi_linux");
2574
+ // panda_require("osi_linux");
2544
2575
// make available the api for
2545
2576
assert (init_callstack_instr_api ());
2546
2577
assert (init_osi_linux_api ());
@@ -2577,7 +2608,7 @@ bool init_plugin(void *self) {
2577
2608
// if debug path actually points to a file, then make host_debug_path the
2578
2609
// directory that contains the executable
2579
2610
bin_path = std::string (host_debug_path);
2580
- // host_debug_path = dirname(strdup(host_debug_path));
2611
+ // host_debug_path = dirname(strdup(host_debug_path));
2581
2612
host_debug_path = dirname (strdup (host_debug_path));
2582
2613
} else {
2583
2614
printf (" Don\' t know what host_debug_path: %s is, but it is not a file or directory\n " , host_debug_path);
0 commit comments