Skip to content

Commit cc00253

Browse files
committed
Adding dwarf2 and pri_taint debug prints to help with LAVA debugging
1 parent 580f7bb commit cc00253

File tree

2 files changed

+215
-101
lines changed

2 files changed

+215
-101
lines changed

panda/plugins/dwarf2/dwarf2.cpp

Lines changed: 84 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ void pri_dwarf_plog(const char *file_callee, const char *fn_callee, uint64_t lno
10391039

10401040
Panda__LogEntry ple = PANDA__LOG_ENTRY__INIT;
10411041
// create a call or ret message
1042-
if (isCall){
1042+
if (isCall) {
10431043
ple.dwarf2_call = dwarf;
10441044
}
10451045
else{
@@ -1850,6 +1850,7 @@ bool load_debug_info(const char *dbg_prefix, const char *basename, uint64_t base
18501850
return true;
18511851
}
18521852

1853+
// You need this code to run to fill out TaintQueryPri
18531854
bool read_debug_info(const char* dbg_prefix, const char *basename, uint64_t base_address, bool needs_reloc) {
18541855

18551856
printf ("read_debug_info %s\n", dbg_prefix);
@@ -1879,6 +1880,7 @@ bool looking_for_libc=false;
18791880
const char *libc_host_path=NULL;
18801881
std::string libc_name;
18811882

1883+
// Call back to the loaded plugin
18821884
void on_library_load(CPUState *cpu, target_ulong pc, char *guest_lib_name, target_ulong base_addr, target_ulong size) {
18831885
printf ("on_library_load guest_lib_name=%s\n", guest_lib_name);
18841886
if (!correct_asid(cpu)) {
@@ -1890,7 +1892,7 @@ void on_library_load(CPUState *cpu, target_ulong pc, char *guest_lib_name, targe
18901892
//printf("Trying to load symbols for %s at %#x.\n", lib_name, base_addr);
18911893
std::string lib = std::string(guest_lib_name);
18921894
std::size_t found = lib.find(guest_debug_path);
1893-
if (found == std::string::npos){
1895+
if (found == std::string::npos) {
18941896
char *lib_name = strdup((host_mount_path + lib).c_str());
18951897
printf("access(%s, F_OK): %x\n", lib_name, access(lib_name, F_OK));
18961898
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
18991901
}
19001902
if (looking_for_libc &&
19011903
lib.find(libc_name) != std::string::npos) {
1902-
// if (lib.find("libc-2.13") != std::string::npos) {
19031904
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");
19051905
printf ("actually loading lib_name = %s\n", lib_name);
19061906
bool needs_reloc = true; // elf_base != base_addr;
19071907
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
19311931
}
19321932

19331933
// 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.
19381936
bool main_exec_initialized = false;
1939-
#define MOD_CHECK_FREQ 1000
19401937
bool ensure_main_exec_initialized(CPUState *cpu) {
19411938
//if (!correct_asid(cpu)) return;
19421939
OsiProc *p = get_current_process(cpu);
19431940
GArray *libs = NULL;
19441941
libs = get_mappings(cpu, p);
19451942
free_osiproc(p);
1946-
if (!libs)
1943+
if (!libs) {
1944+
printf("get_mappings failed\n");
19471945
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);
19501948

19511949
for (unsigned i = 0; i < libs->len; i++) {
19521950
char fname[260] = {};
19531951
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);
19571952
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;
19591975
}
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+
19621978
//std::size_t found = lib.find(guest_debug_path);
19631979
//if (found == std::string::npos) continue;
19641980
//std::string host_name = lib.substr(0, found) +
@@ -2062,12 +2078,15 @@ void on_call(CPUState *cpu, target_ulong pc) {
20622078
if (it == line_range_list.end() || pc < it->lowpc ){
20632079
auto it_dyn = addr_to_dynl_function.find(pc);
20642080
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+
}
20662084
pri_runcb_on_fn_start(cpu, pc, NULL, it_dyn->second.c_str());
20672085
}
20682086
else {
2069-
if (debug)
2087+
if (debug) {
20702088
printf("CALL: Could not find line info for 0x" TARGET_FMT_lx "\n", pc);
2089+
}
20712090
}
20722091
return;
20732092
}
@@ -2129,7 +2148,7 @@ void on_ret(CPUState *cpu, target_ulong pc_func) {
21292148
std::string file_name = it->filename;
21302149
std::string funct_name = funcaddrs[cur_function];
21312150
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)
21332152
if (logCallSites) {
21342153
dwarf_log_callsite(cpu, file_name.c_str(), funct_name.c_str(), cur_line, false);
21352154
}
@@ -2141,10 +2160,10 @@ void __livevar_iter(CPUState *cpu,
21412160
std::vector<VarInfo> vars,
21422161
liveVarCB f,
21432162
void *args,
2144-
target_ulong fp){
2163+
target_ulong fp) {
21452164
//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;
21482167
DwarfVarType var_type {type_map[it.fname][it.cu][it.var_type], it.dec_line, var_name};
21492168
//enum LocType { LocReg, LocMem, LocConst, LocErr };
21502169
target_ulong var_loc;
@@ -2179,14 +2198,14 @@ int livevar_find(CPUState *cpu,
21792198
std::vector<VarInfo> vars,
21802199
liveVarPred pred,
21812200
void *args,
2182-
VarInfo &ret_var){
2201+
VarInfo &ret_var) {
21832202

21842203
target_ulong fp = dwarf2_get_cur_fp(cpu, pc);
2185-
if (fp == (target_ulong) -1){
2204+
if (fp == (target_ulong) -1) {
21862205
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);
21872206
return 0;
21882207
}
2189-
for (auto it : vars){
2208+
for (auto it : vars) {
21902209
target_ulong var_loc;
21912210
//process_dwarf_locs(locdesc[i]->ld_s, locdesc[i]->ld_cents);
21922211
//printf("\n");
@@ -2206,7 +2225,7 @@ int livevar_find(CPUState *cpu,
22062225
* end PPPs
22072226
******************************************************************** */
22082227
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) {
22102229
case LocReg:
22112230
break;
22122231
case LocMem:
@@ -2238,7 +2257,7 @@ void dwarf_get_vma_symbol (CPUState *cpu, target_ulong pc, target_ulong vma, cha
22382257

22392258
//VarInfo ret_var = VarInfo(NULL, NULL, NULL, 0);
22402259
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)) {
22422261
*symbol_name = (char *)ret_var.var_name.c_str();
22432262
return;
22442263
}
@@ -2257,7 +2276,7 @@ void dwarf_get_pc_source_info(CPUState *cpu, target_ulong pc, SrcInfo *info, int
22572276
return;
22582277
}
22592278
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) {
22612280
auto it_dyn = addr_to_dynl_function.find(pc);
22622281
if (it_dyn != addr_to_dynl_function.end()){
22632282
//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
22722291
return;
22732292
}
22742293

2275-
if (it->lowpc == it->highpc){
2294+
if (it->lowpc == it->highpc) {
22762295
//printf("In a a plt function\n");
22772296
*rc = 1;
22782297
return;
@@ -2289,11 +2308,11 @@ void dwarf_get_pc_source_info(CPUState *cpu, target_ulong pc, SrcInfo *info, int
22892308
void dwarf_all_livevar_iter(CPUState *cpu,
22902309
target_ulong pc,
22912310
liveVarCB f,
2292-
void *args){
2311+
void *args) {
22932312
//void (*f)(const char *var_ty, const char *var_nm, LocType loc_t, target_ulong loc)){
2294-
if (inExecutableSource){
2313+
if (inExecutableSource) {
22952314
target_ulong fp = dwarf2_get_cur_fp(cpu, pc);
2296-
if (fp == (target_ulong) -1){
2315+
if (fp == (target_ulong) -1) {
22972316
printf("Error: was not able to get the Frame Pointer for the function %s at @ 0x" TARGET_FMT_lx "\n",
22982317
funcaddrs[cur_function].c_str(), pc);
22992318
return;
@@ -2307,8 +2326,10 @@ void dwarf_all_livevar_iter(CPUState *cpu,
23072326
void dwarf_funct_livevar_iter(CPUState *cpu,
23082327
target_ulong pc,
23092328
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+
}
23122333
if (inExecutableSource) {
23132334
target_ulong fp = dwarf2_get_cur_fp(cpu, pc);
23142335
if (fp == (target_ulong) -1){
@@ -2328,13 +2349,15 @@ void dwarf_global_livevar_iter(CPUState *cpu,
23282349
}
23292350

23302351
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+
}
23332355
auto it2 = std::lower_bound(line_range_list.begin(), line_range_list.end(), pc, CompareRangeAndPC());
23342356
// after the call to lower_bound the `pc` should be between it2->lowpc and it2->highpc
23352357
// 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) {
23372359
return false;
2360+
}
23382361
return true;
23392362
/*
23402363
// 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) {
23502373

23512374
int exec_callback_dwarf(CPUState *cpu, target_ulong pc) {
23522375
inExecutableSource = false;
2353-
if (!correct_asid(cpu)) return 0;
2376+
if (!correct_asid(cpu)) {
2377+
return 0;
2378+
}
23542379
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) {
23562381
return 0;
2382+
}
23572383
inExecutableSource = true;
23582384
if (it2->lowpc == it2->highpc) {
23592385
inExecutableSource = false;
@@ -2364,15 +2390,17 @@ int exec_callback_dwarf(CPUState *cpu, target_ulong pc) {
23642390
cur_line = it2->line_number;
23652391

23662392
//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()) {
23682394
return 0;
2369-
if (cur_function == 0)
2395+
}
2396+
if (cur_function == 0) {
23702397
return 0;
2398+
}
23712399
//printf("[%s] [0x%llx]-%s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(),cur_function, funct_name.c_str(),cur_line,pc);
23722400
//__livevar_iter(env, pc, funcvars[cur_function], push_var_if_live);
23732401
//__livevar_iter(env, pc, global_var_list, push_var_if_live);
23742402
//__livevar_iter(env, pc, global_var_list, print_var_if_live);
2375-
if (cur_line != prev_line){
2403+
if (cur_line != prev_line) {
23762404
//printf("[%s] %s(), ln: %4lld, pc @ 0x%x\n",file_name.c_str(), funct_name.c_str(),cur_line,pc);
23772405
pri_runcb_on_after_line_change (cpu, pc, prev_file_name.c_str(), prev_funct_name.c_str(), prev_line);
23782406
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) {
23852413
prev_function = cur_function;
23862414
prev_line = cur_line;
23872415
}
2388-
//if (funcaddrs.find(pc) != funcaddrs.end()){
2416+
//if (funcaddrs.find(pc) != funcaddrs.end()) {
23892417
// on_call(env, pc);
23902418
//}
23912419
return 0;
@@ -2412,24 +2440,27 @@ uint32_t guest_strncpy(CPUState *cpu, char *buf, size_t maxlen, target_ulong gue
24122440
typedef void (* on_proc_change_t)(CPUState *env, target_ulong asid, OsiProc *proc);
24132441

24142442
void handle_asid_change(CPUState *cpu, target_ulong asid, OsiProc *p) {
2415-
// printf ("handle_asid_change\n");
24162443
if (!p) { return; }
24172444
if (!p->name) { return; }
24182445
if (debug) {
24192446
printf("p-name: %s proc-to-monitor: %s\n", p->name, proc_to_monitor);
24202447
}
2421-
// printf ("...really\n");
2422-
//if (strcmp(p->name, proc_to_monitor) != 0) {
24232448
if (strncmp(p->name, proc_to_monitor, strlen(p->name)) == 0) {
24242449
target_ulong current_asid = panda_current_asid(cpu);
24252450
monitored_asid.insert(current_asid);
24262451
printf ("monitoring asid " TARGET_FMT_lx "\n", current_asid);
24272452
}
24282453
if (correct_asid(cpu) && !main_exec_initialized){
2454+
if (debug) {
2455+
printf ("correct_asid, executing main_exec_initialized\n");
2456+
}
24292457
main_exec_initialized = ensure_main_exec_initialized(cpu);
24302458
}
2459+
if (!main_exec_initialized) {
2460+
printf("The ensure_main_exec_intialized function failed on handle_asid_change\n");
2461+
// exit(1);
2462+
}
24312463
//free_osiproc(p);
2432-
24332464
}
24342465
// XXX: osi_foo is largetly commented out and basically does nothing
24352466
// 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) {
25022533

25032534
return;
25042535
}
2505-
2506-
25072536
#endif
2537+
25082538
bool init_plugin(void *self) {
25092539
#if defined(TARGET_I386)
25102540
panda_arg_list *args_gen = panda_get_args("general");
@@ -2517,6 +2547,7 @@ bool init_plugin(void *self) {
25172547
// monitored_asid = 0;
25182548
}
25192549
panda_arg_list *args = panda_get_args("dwarf2");
2550+
debug = panda_parse_bool_opt(args, "debug", "enable debug output");
25202551
guest_debug_path = panda_parse_string_req(args, "g_debugpath", "path to binary/build dir on guest machine");
25212552
host_debug_path = panda_parse_string_req(args, "h_debugpath", "path to binary/build dir on host machine");
25222553
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) {
25402571
panda_require("pri");
25412572
panda_require("asidstory");
25422573

2543-
//panda_require("osi_linux");
2574+
// panda_require("osi_linux");
25442575
// make available the api for
25452576
assert(init_callstack_instr_api());
25462577
assert(init_osi_linux_api());
@@ -2577,7 +2608,7 @@ bool init_plugin(void *self) {
25772608
// if debug path actually points to a file, then make host_debug_path the
25782609
// directory that contains the executable
25792610
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));
25812612
host_debug_path = dirname(strdup(host_debug_path));
25822613
} else {
25832614
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

Comments
 (0)