2 * Copyright (c) 2002, 2003 Greg Lehey
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * This software is provided by the author ``as is'' and any express
15 * or implied warranties, including, but not limited to, the implied
16 * warranties of merchantability and fitness for a particular purpose
17 * are disclaimed. In no event shall the author be liable for any
18 * direct, indirect, incidental, special, exemplary, or consequential
19 * damages (including, but not limited to, procurement of substitute
20 * goods or services; loss of use, data, or profits; or business
21 * interruption) however caused and on any theory of liability,
22 * whether in contract, strict liability, or tort (including
23 * negligence or otherwise) arising in any way out of the use of this
24 * software, even if advised of the possibility of such damage.
26 /* $Id: asf.c,v 1.6 2003/11/04 06:38:37 green Exp $ */
27 /* $FreeBSD: src/usr.sbin/asf/asf.c,v 1.6 2003/11/04 06:38:37 green Exp $ */
36 #include <sys/param.h>
39 #include <sys/types.h>
44 const char *modules_path; /* path relative to kernel
46 const char *outfile; /* and where to write the output */
49 * Take a blank separated list of tokens and turn it into a list of
50 * individual nul-delimited strings. Build a list of pointers at
51 * token, which must have enough space for the tokens. Return the
52 * number of tokens, or -1 on error (typically a missing string
56 tokenize(char *cptr, char *token[], int maxtoken)
58 char delim; /* delimiter to search for */
59 int tokennr; /* index of this token */
61 for (tokennr = 0; tokennr < maxtoken;) {
62 while (isspace(*cptr))
63 cptr++; /* skip initial white space */
64 if ((*cptr == '\0') || (*cptr == '\n')
65 || (*cptr == '#')) /* end of line */
66 return tokennr; /* return number of tokens found */
68 token[tokennr] = cptr; /* point to it */
69 tokennr++; /* one more */
70 if (tokennr == maxtoken) /* run off the end? */
72 if ((delim == '\'') || (delim == '"')) { /* delimitered */
76 && (cptr[-1] != '\\')) { /* found the partner */
77 cptr++; /* move on past */
78 if (!isspace(*cptr)) /* no space after closing quote */
80 *cptr++ = '\0'; /* delimit */
81 } else if ((*cptr == '\0')
82 || (*cptr == '\n')) /* end of line */
85 } else { /* not quoted */
86 while ((*cptr != '\0') && (!isspace(*cptr)) && (*cptr != '\n'))
88 if (*cptr != '\0') /* not end of the line, */
89 *cptr++ = '\0'; /* delimit and move to the next */
92 return maxtoken; /* can't get here */
96 findmodule(char *mod_path, const char *module_name)
98 char *const path_argv[2] = { mod_path, NULL };
99 char *module_path = NULL;
100 size_t module_name_len = strlen(module_name);
104 if (mod_path == NULL) {
106 "Can't allocate memory to traverse a path: %s (%d)\n",
111 fts = fts_open(path_argv, FTS_PHYSICAL | FTS_NOCHDIR, NULL);
114 "Can't begin traversing path %s: %s (%d)\n",
120 while ((ftsent = fts_read(fts)) != NULL) {
121 if (ftsent->fts_info == FTS_DNR ||
122 ftsent->fts_info == FTS_ERR ||
123 ftsent->fts_info == FTS_NS) {
125 "Error while traversing path %s: %s (%d)\n",
131 if (ftsent->fts_info != FTS_F ||
132 ftsent->fts_namelen != module_name_len ||
133 memcmp(module_name, ftsent->fts_name, module_name_len) != 0)
135 if (asprintf(&module_path,
137 (int)ftsent->fts_pathlen,
138 ftsent->fts_path) == -1) {
140 "Can't allocate memory traversing path %s: %s (%d)\n",
148 if (ftsent == NULL && errno != 0) {
150 "Couldn't complete traversing path %s: %s (%d)\n",
158 return (module_path);
162 usage(const char *myname)
166 "%s [-a] [-f] [-k] [modules-path [outfile]]\n\n"
167 "\t-a\tappend to outfile)\n"
168 "\t-f\tfind the module in any subdirectory of module-path\n"
169 "\t-k\ttake input from kldstat(8)\n",
174 main(int argc, char *argv[])
179 FILE *out; /* output file */
181 int tokens; /* number of tokens on line */
183 const char *filemode = "w"; /* mode for outfile */
184 char cwd[MAXPATHLEN]; /* current directory */
185 char *token[MAXTOKEN];
188 getcwd(cwd, MAXPATHLEN); /* find where we are */
190 while ((ch = getopt(argc, argv, "afk")) != -1) {
192 case 'k': /* get input from kldstat(8) */
193 if (!(kldstat = popen("kldstat", "r"))) {
194 perror("Can't start kldstat");
198 case 'a': /* append to outfile */
201 case 'f': /* find .ko (recursively) */
214 modules_path = argv[0];
227 "Extraneous startup information: \"%s\", aborting\n",
229 usage(getprogname());
232 if (modules_path == NULL)
233 modules_path = "/boot/kernel";
236 if ((out = fopen(outfile, filemode)) == NULL) {
238 "Can't open output file %s: %s (%d)\n",
244 while (fgets(buf, MAXLINE, kldstat)) {
245 if ((!(strstr(buf, "kernel")))
248 long long textaddr = 0;
249 long long dataaddr = 0;
250 long long bssaddr = 0;
252 tokens = tokenize(buf, token, MAXTOKEN);
255 base = strtoll(token[2], NULL, 16);
259 "/usr/bin/objdump --section-headers %s/%s",
265 modpath = findmodule(strdup(modules_path), token[4]);
270 "/usr/bin/objdump --section-headers %s",
274 if (!(objcopy = popen(ocbuf, "r"))) {
276 "Can't start %s: %s (%d)\n",
282 while (fgets(ocbuf, MAXLINE, objcopy)) {
284 char *octoken[MAXTOKEN];
286 octokens = tokenize(ocbuf, octoken, MAXTOKEN);
288 if (!strcmp(octoken[1], ".text"))
289 textaddr = strtoll(octoken[3], NULL, 16) + base;
290 else if (!strcmp(octoken[1], ".data"))
291 dataaddr = strtoll(octoken[3], NULL, 16) + base;
292 else if (!strcmp(octoken[1], ".bss"))
293 bssaddr = strtoll(octoken[3], NULL, 16) + base;
296 if (textaddr) { /* we must have a text address */
299 "add-symbol-file %s%s%s/%s 0x%llx",
300 modules_path[0] != '/' ? cwd : "",
301 modules_path[0] != '/' ? "/" : "",
308 modpath = findmodule(strdup(modules_path), token[4]);
312 "add-symbol-file %s 0x%llx",
318 fprintf(out, " -s .data 0x%llx", dataaddr);
320 fprintf(out, " -s .bss 0x%llx", bssaddr);