diff --git a/src/dwarf/dwarf_function.c b/src/dwarf/dwarf_function.c index 8126e26..e3cf4f0 100644 --- a/src/dwarf/dwarf_function.c +++ b/src/dwarf/dwarf_function.c @@ -115,19 +115,7 @@ static void clear(pst_function* fn) pst_call_site_storage_fini(&fn->call_sites); } -pst_parameter* pst_function_next_parameter(pst_function* fn, pst_parameter* p) -{ - struct list_node* n = (p == NULL) ? list_first(&fn->params) : list_next(&p->node); - - pst_parameter* ret = NULL; - if(n) { - ret = list_entry(n, pst_parameter, node); - } - - return ret; -} - -bool handle_lexical_block(pst_function* fn, Dwarf_Die* result) +static bool handle_lexical_block(pst_function* fn, Dwarf_Die* result) { uint64_t lowpc = 0, highpc = 0; const char* origin_name = ""; dwarf_lowpc(result, &lowpc); @@ -188,7 +176,30 @@ bool handle_lexical_block(pst_function* fn, Dwarf_Die* result) return true; } -bool pst_function_print_dwarf(pst_function* fn) +pst_parameter* function_next_parameter(pst_function* fn, pst_parameter* p) +{ + struct list_node* n = (p == NULL) ? list_first(&fn->params) : list_next(&p->node); + + pst_parameter* ret = NULL; + if(n) { + ret = list_entry(n, pst_parameter, node); + } + + return ret; +} + +void function_print_simple(pst_function* fn) +{ + fn->ctx->print(fn->ctx, "%s() ", fn->info.name); + if(fn->info.file) { + fn->ctx->print(fn->ctx, "at %s:%d, %p", fn->info.file, fn->info.line, (void*)fn->info.pc); + } else { + fn->ctx->print(fn->ctx, "at %p", (void*)fn->info.pc); + } + fn->ctx->print(fn->ctx, "\n"); +} + +bool function_print_pretty(pst_function* fn) { char* at = NULL; if(fn->info.file) { @@ -199,18 +210,18 @@ bool pst_function_print_dwarf(pst_function* fn) } // handle return parameter and be safe if function haven't parameters (for example, dwar info for function is absent) - pst_parameter* param = pst_function_next_parameter(fn, NULL); + pst_parameter* param = function_next_parameter(fn, NULL); if(param && (param->info.flags & PARAM_RETURN)) { // print return value type, function name and start list of parameters parameter_print(param); fn->ctx->print(fn->ctx, " %s(", fn->info.name); - param = pst_function_next_parameter(fn, param); + param = function_next_parameter(fn, param); } else { fn->ctx->print(fn->ctx, "%s(", fn->info.name); } bool first = true; bool start_variable = false; - for(; param; param = pst_function_next_parameter(fn, param)) { + for(; param; param = function_next_parameter(fn, param)) { if(param->info.flags & PARAM_RETURN) { // print return value type, function name and start list of parameters parameter_print(param); @@ -251,7 +262,7 @@ bool pst_function_print_dwarf(pst_function* fn) return true; } -bool pst_function_handle_dwarf(pst_function * fn, Dwarf_Die* d) +bool function_handle_dwarf(pst_function * fn, Dwarf_Die* d) { fn->die = d; get_frame(fn); @@ -378,7 +389,7 @@ bool pst_function_handle_dwarf(pst_function * fn, Dwarf_Die* d) return true; } -bool pst_function_unwind(pst_function* fn) +bool function_unwind(pst_function* fn) { Dwfl_Line *dwline = dwfl_getsrc(fn->ctx->dwfl, fn->info.pc); if(dwline != NULL) { diff --git a/src/dwarf/dwarf_function.h b/src/dwarf/dwarf_function.h index c5f5983..a9d504c 100644 --- a/src/dwarf/dwarf_function.h +++ b/src/dwarf/dwarf_function.h @@ -36,9 +36,11 @@ void pst_function_init(pst_function* fn, pst_context* _ctx, pst_function* _paren pst_function* pst_function_new(pst_context* _ctx, pst_function* _parent); void pst_function_fini(pst_function* fn); -bool pst_function_unwind(pst_function* fn); -bool pst_function_handle_dwarf(pst_function * fn, Dwarf_Die* d); -bool pst_function_print_dwarf(pst_function* fn); -pst_parameter* pst_function_next_parameter(pst_function* fn, pst_parameter* p); +bool function_unwind(pst_function* fn); +bool function_handle_dwarf(pst_function * fn, Dwarf_Die* d); +bool function_print_pretty(pst_function* fn); +void function_print_simple(pst_function* fn); + +pst_parameter* function_next_parameter(pst_function* fn, pst_parameter* p); #endif /* __PST_DWARF_FUNCTION_H__ */ diff --git a/src/dwarf/dwarf_handler.c b/src/dwarf/dwarf_handler.c index 2b134fc..f5b5169 100644 --- a/src/dwarf/dwarf_handler.c +++ b/src/dwarf/dwarf_handler.c @@ -68,7 +68,7 @@ bool get_dwarf_function(pst_handler* h, pst_function* fun) if(tag == DW_TAG_subprogram || tag == DW_TAG_entry_point || tag == DW_TAG_inlined_subroutine) { //ctx.log(SEVERITY_DEBUG, "function die name %s", dwarf_diename(&result)); if(!strcmp(fun->info.name, dwarf_diename(&result))) { - return pst_function_handle_dwarf(fun, &result); + return function_handle_dwarf(fun, &result); } } } while(dwarf_siblingof(&result, &result) == 0); @@ -169,7 +169,7 @@ const char* pst_print_pretty(pst_handler* h) uint32_t idx = 0; for(pst_function* fn = pst_handler_next_function(h, NULL); fn; fn = pst_handler_next_function(h, fn)) { h->ctx.print(&h->ctx, "[%-2u] ", idx); idx++; - pst_function_print_dwarf(fn); + function_print_pretty(fn); h->ctx.print(&h->ctx, "\n"); } @@ -191,14 +191,8 @@ const char* pst_print_simple(pst_handler* h) h->ctx.print(&h->ctx, "Simple unwind-based stack trace:\n"); uint32_t idx = 0; for(pst_function* fn = pst_handler_next_function(h, NULL); fn; fn = pst_handler_next_function(h, fn)) { - h->ctx.print(&h->ctx, "[%-2d] ", idx); - h->ctx.print(fn->ctx, "%s() ", fn->info.name); - if(fn->info.file) { - h->ctx.print(fn->ctx, "at %s:%d, %p", fn->info.file, fn->info.line, (void*)fn->info.pc); - } else { - h->ctx.print(fn->ctx, "at %p", (void*)fn->info.pc); - } - h->ctx.print(&h->ctx, "\n"); + h->ctx.print(&h->ctx, "[%-2u] ", idx); + function_print_simple(fn); idx++; } @@ -283,7 +277,7 @@ bool pst_handler_unwind(pst_handler* h) pst_function* last = last_function(h); pst_function* fn = add_function(h, NULL); fn->info.pc = pc; fn->info.sp = sp; - if(!pst_function_unwind(fn)) { + if(!function_unwind(fn)) { del_function(fn); } else if(last) { last->parent = fn; diff --git a/src/libpst.c b/src/libpst.c index 0794187..94b51af 100644 --- a/src/libpst.c +++ b/src/libpst.c @@ -67,7 +67,7 @@ pst_function* pst_function_next(pst_handler* handler, pst_function* current) pst_parameter* pst_parameter_next(pst_function* function, pst_parameter* current) { - return pst_function_next_parameter(function, current); + return function_next_parameter(function, current); } pst_parameter* pst_parameter_next_child(pst_parameter* parent, pst_parameter* current)