在内核中使用 dump_stack 的时候可以看到如下用法:
- static inline void print_ip_sym(unsigned long ip)
- {
- printk("[<%px>] %pS\n", (void *) ip, (void *) ip);
- }
然后我们就可以看到类似如下的内核 log:
可以看到, 上面不光输出了运行地址, 还把对应的函数名以及偏移地址都列了出来, 内核是如何做到的呢?
在调用 printk 的时候:
printk --> vprintk_default --> vprintk_emit --> vprintk_store --> vscnprintf --> vsnprintf --> pointer
上面的转换就是在 pointer 中完成的:
- /*
- * Show a '%p' thing. A kernel extension is that the '%p' is followed
- * by an extra set of alphanumeric characters that are extended format
- * specifiers.
- *
- * Please update scripts/checkpatch.pl when adding/removing conversion
- * characters. (Search for "check for vsprintf extension").
- *
- * Right now we handle:
- *
- * - 'F' For symbolic function descriptor pointers with offset
- * - 'f' For simple symbolic function names without offset
- * - 'S' For symbolic direct pointers with offset
- * - 's' For symbolic direct pointers without offset
- * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
- * - 'B' For backtraced symbolic direct pointers with offset
- * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
- * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
- * - 'b[l]' For a bitmap, the number of bits is determined by the field
- * width which must be explicitly specified either as part of the
- * format string '2b[l]' or through '%*b[l]', [l] selects
- * range-list format instead of hex format
- * - 'M' For a 6-byte Mac address, it prints the address in the
- * usual colon-separated hex notation
- * - 'm' For a 6-byte Mac address, it prints the hex address without colons
- * - 'MF' For a 6-byte Mac FDDI address, it prints the address
- * with a Dash-separated hex notation
- * - '[mM]R' For a 6-byte Mac address, Reverse order (Bluetooth)
- * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
- * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
- * IPv6 uses colon separated network-order 16 bit hex with leading 0's
- * [S][pfs]
- * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
- * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
- * - 'i' [46] for 'raw' IPv4/IPv6 addresses
- * IPv6 omits the colons (01020304...0f)
- * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
- * [S][pfs]
- * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
- * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
- * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
- * - 'I[6S]c' for IPv6 addresses printed as specified by
- * http://tools.ietf.org/html/rfc5952
- * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
- * of the following flags (see string_escape_mem() for the
- * details):
- * a - ESCAPE_ANY
- * c - ESCAPE_SPECIAL
- * h - ESCAPE_HEX
- * n - ESCAPE_NULL
- * o - ESCAPE_OCTAL
- * p - ESCAPE_NP
- * s - ESCAPE_SPACE
- * By default ESCAPE_ANY_NP is used.
- * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
- * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
- * Options for %pU are:
- * b big endian lower case hex (default)
- * B big endian UPPER case hex
- * l little endian lower case hex
- * L little endian UPPER case hex
- * big endian output byte order is:
- * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
- * little endian output byte order is:
- * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
- * - 'V' For a struct va_format which contains a format string * and va_list *,
- * call vsnprintf(->format, *->va_list).
- * Implements a "recursive vsnprintf".
- * Do not use this feature without some mechanism to verify the
- * correctness of the format string and va_list arguments.
- * - 'K' For a kernel pointer that should be hidden from unprivileged users
- * - 'NF' For a netdev_features_t
- * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
- * a certain separator (' ' by default):
- * C colon
- * D Dash
- * N no separator
- * The maximum supported length is 64 bytes of the input. Consider
- * to use print_hex_dump() for the larger input.
- * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
- * (default assumed to be phys_addr_t, passed by reference)
- * - 'd[234]' For a dentry name (optionally 2-4 last components)
- * - 'D[234]' Same as 'd' but for a struct file
- * - 'g' For block_device name (gendisk + partition number)
- * - 'C' For a clock, it prints the name (Common Clock Framework) or address
- * (legacy clock framework) of the clock
- * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
- * (legacy clock framework) of the clock
- * - 'Cr' For a clock, it prints the current rate of the clock
- * - 'G' For flags to be printed as a collection of symbolic strings that would
- * construct the specific value. Supported flags given by option:
- * p page flags (see struct page) given as pointer to unsigned long
- * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
- * v vma flags (VM_*) given as pointer to unsigned long
- * - 'O' For a kobject based struct. Must be one of the following:
- * - 'OF[fnpPcCF]' For a device tree object
- * Without any optional arguments prints the full_name
- * f device node full_name
- * n device node name
- * p device node phandle
- * P device node path spec (name + @unit)
- * F device node flags
- * c major compatible string
- * C full compatible string
- *
- * ** Please update also Documentation/printk-formats.txt when making changes **
- *
- * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
- * function pointers are really function descriptors, which contain a
- * pointer to the real address.
- */
- static noinline_for_stack
- char *pointer(const char *fmt, char *buf, char *end, void *ptr,
- struct printf_spec spec)
- {
- const int default_width = 2 * sizeof(void *);
- if (!ptr && *fmt != 'K') {
- /*
- * Print (null) with the same width as a pointer so it makes
- * tabular output look nice.
- */
- if (spec.field_width == -1)
- spec.field_width = default_width;
- return string(buf, end, "(null)", spec);
- }
- switch (*fmt) {
- case 'F':
- case 'f':
- ptr = dereference_function_descriptor(ptr);
- /* Fallthrough */
- case 'S':
- case 's':
- case 'B':
- return symbol_string(buf, end, ptr, spec, fmt);
- case 'R':
- case 'r':
- return resource_string(buf, end, ptr, spec, fmt);
- case 'h':
- return hex_string(buf, end, ptr, spec, fmt);
- case 'b':
- switch (fmt[1]) {
- case 'l':
- return bitmap_list_string(buf, end, ptr, spec, fmt);
- default:
- return bitmap_string(buf, end, ptr, spec, fmt);
- }
- case 'M': /* Colon separated: 00:01:02:03:04:05 */
- case 'm': /* Contiguous: 000102030405 */
- /* [mM]F (FDDI) */
- /* [mM]R (Reverse order; Bluetooth) */
- return mac_address_string(buf, end, ptr, spec, fmt);
- case 'I': /* Formatted IP supported
- * 4: 1.2.3.4
- * 6: 0001:0203:...:0708
- * 6c: 1::708 or 1::1.2.3.4
- */
- case 'i': /* Contiguous:
- * 4: 001.002.003.004
- * 6: 000102...0f
- */
- switch (fmt[1]) {
- case '6':
- return ip6_addr_string(buf, end, ptr, spec, fmt);
- case '4':
- return ip4_addr_string(buf, end, ptr, spec, fmt);
- case 'S': {
- const union {
- struct sockaddr raw;
- struct sockaddr_in v4;
- struct sockaddr_in6 v6;
- } *sa = ptr;
- switch (sa->raw.sa_family) {
- case AF_INET:
- return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
- case AF_INET6:
- return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
- default:
- return string(buf, end, "(invalid address)", spec);
- }}
- }
- break;
- case 'E':
- return escaped_string(buf, end, ptr, spec, fmt);
- case 'U':
- return uuid_string(buf, end, ptr, spec, fmt);
- case 'V':
- {
- va_list va;
- va_copy(va, *((struct va_format *)ptr)->va);
- buf += vsnprintf(buf, end> buf ? end - buf : 0,
- ((struct va_format *)ptr)->fmt, va);
- va_end(va);
- return buf;
- }
- case 'K':
- switch (kptr_restrict) {
- case 0:
- /* Always print %pK values */
- break;
- case 1: {
- const struct cred *cred;
- /*
- * kptr_restrict==1 cannot be used in IRQ context
- * because its test for CAP_SYSLOG would be meaningless.
- */
- if (in_irq() || in_serving_softirq() || in_nmi()) {
- if (spec.field_width == -1)
- spec.field_width = default_width;
- return string(buf, end, "pK-error", spec);
- }
- /*
- * Only print the real pointer value if the current
- * process has CAP_SYSLOG and is running with the
- * same credentials it started with. This is because
- * access to files is checked at open() time, but %pK
- * checks permission at read() time. We don't want to
- * leak pointer values if a binary opens a file using
- * %pK and then elevates privileges before reading it.
- */
- cred = current_cred();
- if (!has_capability_noaudit(current, CAP_SYSLOG) ||
- !uid_eq(cred->euid, cred->uid) ||
- !gid_eq(cred->egid, cred->gid))
- ptr = NULL;
- break;
- }
- case 2:
- default:
- /* Always print 0's for %pK */
- ptr = NULL;
- break;
- }
- break;
- case 'N':
- return netdev_bits(buf, end, ptr, fmt);
- case 'a':
- return address_val(buf, end, ptr, fmt);
- case 'd':
- return dentry_name(buf, end, ptr, spec, fmt);
- case 'C':
- return clock(buf, end, ptr, spec, fmt);
- case 'D':
- return dentry_name(buf, end,
- ((const struct file *)ptr)->f_path.dentry,
- spec, fmt);
- #ifdef CONFIG_BLOCK
- case 'g':
- return bdev_name(buf, end, ptr, spec, fmt);
- #endif
- case 'G':
- return flags_string(buf, end, ptr, fmt);
- case 'O':
- switch (fmt[1]) {
- case 'F':
- return device_node_string(buf, end, ptr, spec, fmt + 1);
- }
- }
- spec.flags |= SMALL;
- if (spec.field_width == -1) {
- spec.field_width = default_width;
- spec.flags |= ZEROPAD;
- }
- spec.base = 16;
- return number(buf, end, (unsigned long) ptr, spec);
- }
第 136 行, symbol_string(buf, end, ptr, spec, fmt);
- static noinline_for_stack
- char *symbol_string(char *buf, char *end, void *ptr,
- struct printf_spec spec, const char *fmt)
- {
- unsigned long value;
- #ifdef CONFIG_KALLSYMS
- char sym[KSYM_SYMBOL_LEN];
- #endif
- if (fmt[1] == 'R')
- ptr = __builtin_extract_return_addr(ptr);
- value = (unsigned long)ptr;
- #ifdef CONFIG_KALLSYMS
- if (*fmt == 'B')
- sprint_backtrace(sym, value);
- else if (*fmt != 'f' && *fmt != 's')
- sprint_symbol(sym, value);
- else
- sprint_symbol_no_offset(sym, value);
- return string(buf, end, sym, spec);
- #else
- return special_hex_number(buf, end, value, sizeof(void *));
- #endif
- }
第 20 行: sprint_symbol_no_offset
- /**
- * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
- * @buffer: buffer to be stored
- * @address: address to lookup
- *
- * This function looks up a kernel symbol with @address and stores its name
- * and module name to @buffer if possible. If no symbol was found, just saves
- * its @address as is.
- *
- * This function returns the number of bytes stored in @buffer.
- */
- int sprint_symbol_no_offset(char *buffer, unsigned long address)
- {
- return __sprint_symbol(buffer, address, 0, 0);
- }
完.
来源: http://www.bubuko.com/infodetail-3109021.html