2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
55 TAILQ_ENTRY(symdata) link;
61 static TAILQ_HEAD(symlist, symdata) symlist;
62 static struct symdata *symcache;
63 static char *symbegin;
67 read_symbols(const char *file)
71 size_t buflen = sizeof(buf);
81 if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
82 file = "/boot/kernel";
86 snprintf(cmd, sizeof(cmd), "nm -n %s", file);
87 if ((fp = popen(cmd, "r")) != NULL) {
88 while (fgets(buf, sizeof(buf), fp) != NULL) {
89 s1 = strtok(buf, " \t\n");
90 s2 = strtok(NULL, " \t\n");
91 s3 = strtok(NULL, " \t\n");
93 sym = malloc(sizeof(struct symdata));
94 sym->symaddr = (char *)strtoul(s1, NULL, 16);
96 sym->symname = strdup(s3);
97 if (strcmp(s3, "kernbase") == 0)
98 symbegin = sym->symaddr;
99 if (strcmp(s3, "end") == 0)
100 symend = sym->symaddr;
101 TAILQ_INSERT_TAIL(&symlist, sym, link);
106 symcache = TAILQ_FIRST(&symlist);
110 address_to_symbol(void *kptr, struct save_ctx *ctx)
112 char *buf = ctx->save_buf;
113 int size = sizeof(ctx->save_buf);
115 if (symcache == NULL ||
116 (char *)kptr < symbegin || (char *)kptr >= symend
118 snprintf(buf, size, "%p", kptr);
121 while ((char *)symcache->symaddr < (char *)kptr) {
122 if (TAILQ_NEXT(symcache, link) == NULL)
124 symcache = TAILQ_NEXT(symcache, link);
126 while ((char *)symcache->symaddr > (char *)kptr) {
127 if (symcache != TAILQ_FIRST(&symlist))
128 symcache = TAILQ_PREV(symcache, symlist, link);
130 snprintf(buf, size, "%s+%d", symcache->symname,
131 (int)((char *)kptr - symcache->symaddr));