From cdbd3e9148ca9ac760dc7f80046e20e60289bc70 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Tue, 24 May 2022 12:03:43 +0200 Subject: lib/igt_core: use print_backtrace() on internal_assert() When internal_assert() hits, it is desired to know what part of the code actually hit the issue. Reviewed-by: Petri Latvala Signed-off-by: Mauro Carvalho Chehab --- lib/igt_core.c | 124 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 61 deletions(-) (limited to 'lib') diff --git a/lib/igt_core.c b/lib/igt_core.c index b7116f4e..e7425326 100644 --- a/lib/igt_core.c +++ b/lib/igt_core.c @@ -351,6 +351,67 @@ static bool stderr_needs_sentinel = false; static int _igt_dynamic_tests_executed = -1; +static void print_backtrace(void) +{ + unw_cursor_t cursor; + unw_context_t uc; + int stack_num = 0; + + Dwfl_Callbacks cbs = { + .find_elf = dwfl_linux_proc_find_elf, + .find_debuginfo = dwfl_standard_find_debuginfo, + }; + + Dwfl *dwfl = dwfl_begin(&cbs); + + if (dwfl_linux_proc_report(dwfl, getpid())) { + dwfl_end(dwfl); + dwfl = NULL; + } else + dwfl_report_end(dwfl, NULL, NULL); + + igt_info("Stack trace:\n"); + + unw_getcontext(&uc); + unw_init_local(&cursor, &uc); + while (unw_step(&cursor) > 0) { + char name[255]; + unw_word_t off, ip; + Dwfl_Module *mod = NULL; + + unw_get_reg(&cursor, UNW_REG_IP, &ip); + + if (dwfl) + mod = dwfl_addrmodule(dwfl, ip); + + if (mod) { + const char *src, *dwfl_name; + Dwfl_Line *line; + int lineno; + GElf_Sym sym; + + line = dwfl_module_getsrc(mod, ip); + dwfl_name = dwfl_module_addrsym(mod, ip, &sym, NULL); + + if (line && dwfl_name) { + src = dwfl_lineinfo(line, NULL, &lineno, NULL, NULL, NULL); + igt_info(" #%d %s:%d %s()\n", stack_num++, src, lineno, dwfl_name); + continue; + } + } + + if (unw_get_proc_name(&cursor, name, 255, &off) < 0) + igt_info(" #%d [+0x%x]\n", stack_num++, + (unsigned int) ip); + else + igt_info(" #%d [%s+0x%x]\n", stack_num++, name, + (unsigned int) off); + } + + if (dwfl) + dwfl_end(dwfl); +} + __attribute__((format(printf, 2, 3))) static void internal_assert(bool cond, const char *format, ...) { @@ -362,6 +423,8 @@ static void internal_assert(bool cond, const char *format, ...) va_end(ap); fprintf(stderr, "please refer to lib/igt_core documentation\n"); + print_backtrace(); + assert(0); } } @@ -1766,67 +1829,6 @@ static void write_stderr(const char *str) __write_stderr(str, strlen(str)); } -static void print_backtrace(void) -{ - unw_cursor_t cursor; - unw_context_t uc; - int stack_num = 0; - - Dwfl_Callbacks cbs = { - .find_elf = dwfl_linux_proc_find_elf, - .find_debuginfo = dwfl_standard_find_debuginfo, - }; - - Dwfl *dwfl = dwfl_begin(&cbs); - - if (dwfl_linux_proc_report(dwfl, getpid())) { - dwfl_end(dwfl); - dwfl = NULL; - } else - dwfl_report_end(dwfl, NULL, NULL); - - igt_info("Stack trace:\n"); - - unw_getcontext(&uc); - unw_init_local(&cursor, &uc); - while (unw_step(&cursor) > 0) { - char name[255]; - unw_word_t off, ip; - Dwfl_Module *mod = NULL; - - unw_get_reg(&cursor, UNW_REG_IP, &ip); - - if (dwfl) - mod = dwfl_addrmodule(dwfl, ip); - - if (mod) { - const char *src, *dwfl_name; - Dwfl_Line *line; - int lineno; - GElf_Sym sym; - - line = dwfl_module_getsrc(mod, ip); - dwfl_name = dwfl_module_addrsym(mod, ip, &sym, NULL); - - if (line && dwfl_name) { - src = dwfl_lineinfo(line, NULL, &lineno, NULL, NULL, NULL); - igt_info(" #%d %s:%d %s()\n", stack_num++, src, lineno, dwfl_name); - continue; - } - } - - if (unw_get_proc_name(&cursor, name, 255, &off) < 0) - igt_info(" #%d [+0x%x]\n", stack_num++, - (unsigned int) ip); - else - igt_info(" #%d [%s+0x%x]\n", stack_num++, name, - (unsigned int) off); - } - - if (dwfl) - dwfl_end(dwfl); -} - static const char hex[] = "0123456789abcdef"; static void -- cgit v1.2.3