Skip to content

Commit

Permalink
core/utils: add snprintf_symbol
Browse files Browse the repository at this point in the history
get_symbol is difficult to use. Add snprintf_symbol helper which
prints a symbol into a buffer with length, and returns the number
of bytes used, similarly to snprintf. Use this in the stack dumping
code rather than open-coding it.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Stewart Smith <stewart@linux.vnet.ibm.com>
  • Loading branch information
npiggin authored and stewartsmith committed Feb 9, 2018
1 parent 85a1de3 commit 3a74228
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
17 changes: 4 additions & 13 deletions core/stack.c
Expand Up @@ -59,8 +59,7 @@ void __print_backtrace(unsigned int pir,
static char bt_text_buf[4096];
int i, l = 0, max;
char *buf = out_buf;
unsigned long bottom, top, tbot, ttop, saddr = 0;
char *sym = NULL, *sym_end = NULL;
unsigned long bottom, top, tbot, ttop;
char mark;

if (!out_buf) {
Expand All @@ -82,20 +81,12 @@ void __print_backtrace(unsigned int pir,
mark = '*';
else
mark = ' ';
if (symbols)
saddr = get_symbol(entries->pc, &sym, &sym_end);
else
saddr = 0;
l += snprintf(buf + l, max - l,
" S: %016lx R: %016lx %c ",
entries->sp, entries->pc, mark);
while(saddr && sym < sym_end && l < max)
buf[l++] = *(sym++);
if (sym && l < max)
l += snprintf(buf + l, max - l, "+0x%lx\n",
entries->pc - saddr);
else
l += snprintf(buf + l, max - l, "\n");
if (symbols)
l += snprintf_symbol(buf + l, max - l, entries->pc);
l += snprintf(buf + l, max - l, "\n");
entries++;
}
if (!out_buf)
Expand Down
26 changes: 25 additions & 1 deletion core/utils.c
Expand Up @@ -61,7 +61,7 @@ char __attrconst tohex(uint8_t nibble)
return __tohex[nibble];
}

unsigned long get_symbol(unsigned long addr, char **sym, char **sym_end)
static unsigned long get_symbol(unsigned long addr, char **sym, char **sym_end)
{
unsigned long prev = 0, next;
char *psym = NULL, *p = __sym_map_start;
Expand All @@ -88,3 +88,27 @@ unsigned long get_symbol(unsigned long addr, char **sym, char **sym_end)
return 0;
}

size_t snprintf_symbol(char *buf, size_t len, uint64_t addr)
{
unsigned long saddr;
char *sym, *sym_end;
size_t l;

saddr = get_symbol(addr, &sym, &sym_end);
if (!saddr)
return 0;

if (len > sym_end - sym)
l = sym_end - sym;
else
l = len - 1;
memcpy(buf, sym, l);

/*
* This snprintf will insert the terminating NUL even if the
* symbol has used up the entire buffer less 1.
*/
l += snprintf(buf + l, len - l, "+0x%llx", addr - saddr);

return l;
}
3 changes: 1 addition & 2 deletions include/skiboot.h
Expand Up @@ -204,8 +204,7 @@ extern const char version[];
/* Debug support */
extern char __sym_map_start[];
extern char __sym_map_end[];
extern unsigned long get_symbol(unsigned long addr,
char **sym, char **sym_end);
extern size_t snprintf_symbol(char *buf, size_t len, uint64_t addr);

/* Direct controls */
extern void direct_controls_init(void);
Expand Down

0 comments on commit 3a74228

Please sign in to comment.