Skip to content

Commit 0fe1ef2

Browse files
committed
vsprintf: add support for '%pS' and '%pF' pointer formats
They print out a pointer in symbolic format, if possible (ie using symbolic KALLSYMS information). The '%pS' format is for regular direct pointers (which can point to data or code and that you find on the stack during backtraces etc), while '%pF' is for C function pointer types. On most architectures, the two mean exactly the same thing, but some architectures use an indirect pointer for C function pointers, where the function pointer points to a function descriptor (which in turn contains the actual pointer to the code). The '%pF' code automatically does the appropriate function descriptor dereference on such architectures. Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4d8a743 commit 0fe1ef2

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

lib/vsprintf.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/string.h>
2323
#include <linux/ctype.h>
2424
#include <linux/kernel.h>
25+
#include <linux/kallsyms.h>
26+
#include <linux/uaccess.h>
2527

2628
#include <asm/page.h> /* for PAGE_SIZE */
2729
#include <asm/div64.h>
@@ -511,15 +513,52 @@ static char *string(char *buf, char *end, char *s, int field_width, int precisio
511513
return buf;
512514
}
513515

516+
static inline void *dereference_function_descriptor(void *ptr)
517+
{
518+
#if defined(CONFIG_IA64) || defined(CONFIG_PPC64)
519+
void *p;
520+
if (!probe_kernel_address(ptr, p))
521+
ptr = p;
522+
#endif
523+
return ptr;
524+
}
525+
526+
static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
527+
{
528+
unsigned long value = (unsigned long) ptr;
529+
#ifdef CONFIG_KALLSYMS
530+
char sym[KSYM_SYMBOL_LEN];
531+
sprint_symbol(sym, value);
532+
return string(buf, end, sym, field_width, precision, flags);
533+
#else
534+
field_width = 2*sizeof(void *);
535+
flags |= SPECIAL | SMALL | ZEROPAD;
536+
return number(buf, end, value, 16, field_width, precision, flags);
537+
#endif
538+
}
539+
514540
/*
515541
* Show a '%p' thing. A kernel extension is that the '%p' is followed
516542
* by an extra set of alphanumeric characters that are extended format
517543
* specifiers.
518544
*
519-
* Right now don't actually handle any such, but we will..
545+
* Right now we just handle 'F' (for symbolic Function descriptor pointers)
546+
* and 'S' (for Symbolic direct pointers), but this can easily be
547+
* extended in the future (network address types etc).
548+
*
549+
* The difference between 'S' and 'F' is that on ia64 and ppc64 function
550+
* pointers are really function descriptors, which contain a pointer the
551+
* real address.
520552
*/
521553
static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
522554
{
555+
switch (*fmt) {
556+
case 'F':
557+
ptr = dereference_function_descriptor(ptr);
558+
/* Fallthrough */
559+
case 'S':
560+
return symbol_string(buf, end, ptr, field_width, precision, flags);
561+
}
523562
flags |= SMALL;
524563
if (field_width == -1) {
525564
field_width = 2*sizeof(void *);

0 commit comments

Comments
 (0)