/* * Get the linker file list from kldstat(8) output. * The "run" flag tells if kldstat(8) should run now. * Of course, kldstat(1) can run in a live system only, but its output * can be saved beforehand and fed to this function later via stdin. */ void asf_prog(int run) { char buf[LINE_MAX]; char *token[MAXTOKEN]; char *endp; FILE *kldstat; caddr_t base; int tokens; if (run) { if ((kldstat = popen("kldstat", "r")) == NULL) err(2, "can't start kldstat"); } else kldstat = stdin; while (fgets(buf, sizeof(buf), kldstat)) { /* Skip header line and main kernel file */ if (buf[0] == 'I' || strstr(buf, KERNFILE)) continue; tokens = tokenize(buf, token, MAXTOKEN); if (tokens < 4) continue; base = (caddr_t)(uintptr_t)strtoumax(token[2], &endp, 16); if (endp == NULL || *endp != '\0') continue; kfile_add(token[4], base); } }
/* * Get the linker file list using the kld interface. * Works with a live kernel only. */ void asf_kld() { struct kld_file_stat kfs; int fid = 0; /* indicates the beginning of the linker file list */ while ((fid = kldnext(fid)) != 0) { if (fid == -1) err(2, "kldnext"); kfs.version = sizeof(kfs); /* must be set for kldstat(2) */ /* Get info on this linker file */ if (kldstat(fid, &kfs) == -1) err(2, "kldstat"); if (strcmp(kfs.name, KERNFILE) == 0) continue; /* Add to our list of linker files */ kfile_add(kfs.name, kfs.address); } }
/* * Get the list of linker files using kvm(3). * Can work with a live kernel as well as with a crash dump. */ void asf_kvm(const char *kernfile, const char *corefile) { char errbuf[LINE_MAX]; char name[PATH_MAX]; kvm_t *kd; struct nlist nl[2]; struct linker_file lf; linker_file_list_t linker_files; ssize_t n; void *kp; kd = kvm_openfiles(kernfile, corefile, NULL, O_RDONLY, errbuf); if (kd == NULL) errx(2, "open kernel memory: %s", errbuf); /* * Locate the head of the linker file list using kernel symbols. */ strcpy(name, LINKER_HEAD); nl[0].n_name = name; /* can't use LINKER_HEAD here because it's const */ nl[1].n_name = NULL; /* terminate the array for kvm_nlist() */ switch (kvm_nlist(kd, nl)) { case 0: break; case -1: warnx("%s: %s", LINKER_HEAD, kvm_geterr(kd)); kvm_close(kd); exit(2); default: kvm_close(kd); errx(2, "%s: symbol not found", LINKER_HEAD); } /* * Read the head of the linker file list from kernel memory. */ n = kvm_read(kd, nl[0].n_value, &linker_files, sizeof(linker_files)); if (n == -1) goto read_err; if (n != sizeof(linker_files)) { kvm_close(kd); errx(2, "%s: short read", LINKER_HEAD); } /* * Traverse the linker file list starting at its head. */ for (kp = linker_files.tqh_first; kp; kp = lf.link.tqe_next) { /* Read a linker file structure */ n = kvm_read(kd, (u_long)kp, &lf, sizeof(lf)); if (n == -1) goto read_err; if (n != sizeof(lf)) { kvm_close(kd); errx(2, "kvm: short read"); } /* Read the name of the file stored separately */ bzero(name, sizeof(name)); n = kvm_read(kd, (u_long)lf.filename, name, sizeof(name) - 1); if (n == -1) goto read_err; if (strcmp(name, KERNFILE) == 0) continue; /* Add this file to our list of linker files */ kfile_add(name, lf.address); } kvm_close(kd); return; read_err: /* A common error case */ warnx("read kernel memory: %s", kvm_geterr(kd)); kvm_close(kd); exit(2); }