diff options
author | 2016-09-16 19:13:16 +0000 | |
---|---|---|
committer | 2016-09-16 19:13:16 +0000 | |
commit | fd68ecf39ef31c9ba23bfb67f49b6ae021178a0f (patch) | |
tree | ffe292e0c486a4141d32585bf8a7d8b566b863bd /sys | |
parent | drop unneeded casting noise (diff) | |
download | wireguard-openbsd-fd68ecf39ef31c9ba23bfb67f49b6ae021178a0f.tar.xz wireguard-openbsd-fd68ecf39ef31c9ba23bfb67f49b6ae021178a0f.zip |
teach ddb(4) about CTF. currently it only loads the CTF and uses it on amd64
to lookup the number of function parameters. however having this basic
facility allows us to expand it's usage.
currently hidden behind the (disabled) DDBCTF kernel option as some of the
required tools are not available in base yet. in addition to that one
also needs recent bootblocks that load the .SUNW_ctf kernel section.
discussed with mpi@ over many a cider and ale in cambridge
feedback and ok guenther@ mpi@
Diffstat (limited to 'sys')
-rw-r--r-- | sys/arch/amd64/amd64/db_trace.c | 17 | ||||
-rw-r--r-- | sys/conf/GENERIC | 3 | ||||
-rw-r--r-- | sys/conf/files | 3 | ||||
-rw-r--r-- | sys/ddb/db_ctf.c | 385 | ||||
-rw-r--r-- | sys/ddb/db_ctf.h | 179 | ||||
-rw-r--r-- | sys/ddb/db_elf.c | 19 | ||||
-rw-r--r-- | sys/ddb/db_elf.h | 47 | ||||
-rw-r--r-- | sys/ddb/db_extern.h | 6 |
8 files changed, 636 insertions, 23 deletions
diff --git a/sys/arch/amd64/amd64/db_trace.c b/sys/arch/amd64/amd64/db_trace.c index 54acc230c75..d8d297c7f78 100644 --- a/sys/arch/amd64/amd64/db_trace.c +++ b/sys/arch/amd64/amd64/db_trace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: db_trace.c,v 1.22 2016/09/10 06:36:26 jasper Exp $ */ +/* $OpenBSD: db_trace.c,v 1.23 2016/09/16 19:13:16 jasper Exp $ */ /* $NetBSD: db_trace.c,v 1.1 2003/04/26 18:39:27 fvdl Exp $ */ /* @@ -36,6 +36,9 @@ #include <machine/frame.h> #include <machine/trap.h> +#ifdef DDBCTF +#include <ddb/db_extern.h> +#endif #include <ddb/db_sym.h> #include <ddb/db_access.h> #include <ddb/db_variables.h> @@ -79,20 +82,26 @@ struct db_variable * db_eregs = db_regs + nitems(db_regs); #define INTERRUPT 3 #define AST 4 -int db_numargs(struct callframe *); +int db_numargs(struct callframe *, const char *); void db_nextframe(struct callframe **, db_addr_t *, long *, int, int (*) (const char *, ...)); /* + * Lookup the function signature in the CTF section, or just + * return 0 like before when unable to do so. In case of no CTF: * Figure out how many arguments were passed into the frame at "fp". * We can probably figure out how many arguments where passed above * the first 6 (which are in registers), but since we can't * reliably determine the values currently, just return 0. */ int -db_numargs(struct callframe *fp) +db_numargs(struct callframe *fp, const char *sym) { +#ifdef DDBCTF + return db_ctf_func_numargs(sym); +#else return 0; +#endif /* DDBCTF */ } /* @@ -230,7 +239,7 @@ db_stack_trace_print(db_expr_t addr, boolean_t have_addr, db_expr_t count, } else { normal: is_trap = NONE; - narg = db_numargs(frame); + narg = db_numargs(frame, name); } (*pr)("%s(", name); diff --git a/sys/conf/GENERIC b/sys/conf/GENERIC index 9e7e2b750a3..d3b9178e9ef 100644 --- a/sys/conf/GENERIC +++ b/sys/conf/GENERIC @@ -1,4 +1,4 @@ -# $OpenBSD: GENERIC,v 1.233 2016/09/04 09:22:28 mpi Exp $ +# $OpenBSD: GENERIC,v 1.234 2016/09/16 19:13:17 jasper Exp $ # # Machine-independent option; used by all architectures for their # GENERIC kernel @@ -7,6 +7,7 @@ option DDB # in-kernel debugger #option DDBPROF # ddb(4) based profiling +#option DDBCTF # DDB support for CTF #option DDB_SAFE_CONSOLE # allow break into ddb during boot #makeoptions DEBUG="-g" # compile full symbol table #makeoptions PROF="-pg" # build profiled kernel diff --git a/sys/conf/files b/sys/conf/files index 70fd647709d..3aff3e45aa5 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,4 +1,4 @@ -# $OpenBSD: files,v 1.631 2016/09/15 01:05:15 dlg Exp $ +# $OpenBSD: files,v 1.632 2016/09/16 19:13:17 jasper Exp $ # $NetBSD: files,v 1.87 1996/05/19 17:17:50 jonathan Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 @@ -597,6 +597,7 @@ file net/if_pppoe.c pppoe needs-flag file ddb/db_access.c ddb | kgdb file ddb/db_break.c ddb file ddb/db_command.c ddb +file ddb/db_ctf.c ddb & ddbctf file ddb/db_dwarf.c ddb file ddb/db_elf.c ddb file ddb/db_examine.c ddb diff --git a/sys/ddb/db_ctf.c b/sys/ddb/db_ctf.c new file mode 100644 index 00000000000..f20f4f98556 --- /dev/null +++ b/sys/ddb/db_ctf.c @@ -0,0 +1,385 @@ +/* $OpenBSD: db_ctf.c,v 1.1 2016/09/16 19:13:17 jasper Exp $ */ + +/* + * Copyright (c) 2016 Jasper Lievisse Adriaanse <jasper@openbsd.org> + * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/stdint.h> +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/exec.h> + +#include <machine/db_machdep.h> + +#include <ddb/db_extern.h> +#include <ddb/db_sym.h> +#include <ddb/db_elf.h> +#include <ddb/db_output.h> +#include <ddb/db_ctf.h> + +#include <sys/exec_elf.h> +#include <sys/malloc.h> +#include <lib/libz/zlib.h> + +extern db_symtab_t db_symtab; + +struct ddb_ctf { + struct ctf_header *cth; + const char *data; + off_t dlen; + unsigned int ctf_found; + unsigned int nsyms; + size_t ctftab_size; + const char *ctftab; +}; + +struct ddb_ctf db_ctf; + +static const char *db_ctf_lookup_name(unsigned int); +static const char *db_ctf_idx2sym(size_t *, unsigned char); +static const char *db_elf_find_ctftab(db_symtab_t *, size_t *); +static char *db_ctf_decompress(const char *, size_t, off_t); +static int db_ctf_print_functions(); +static int db_ctf_nsyms(void); + +#define ELF_CTF ".SUNW_ctf" + +/* + * Entrypoint to verify CTF presence, initialize the header, decompress + * the data, etc. + */ +void +db_ctf_init(void) +{ + db_symtab_t *stab = &db_symtab; + const char *ctftab; + size_t ctftab_size; + int nsyms; + + /* Assume nothing was correct found until proven otherwise. */ + db_ctf.ctf_found = 0; + + ctftab = db_elf_find_ctftab(stab, &ctftab_size); + if (ctftab == NULL) { + return; + } + + db_ctf.ctftab = ctftab; + db_ctf.ctftab_size = ctftab_size; + db_ctf.cth = (struct ctf_header *)ctftab; + db_ctf.dlen = db_ctf.cth->cth_stroff + db_ctf.cth->cth_strlen; + + /* Now decompress the section, take into account to skip the header */ + if (db_ctf.cth->cth_flags & CTF_F_COMPRESS) { + if ((db_ctf.data = + db_ctf_decompress(db_ctf.ctftab + sizeof(*db_ctf.cth), + db_ctf.ctftab_size - sizeof(*db_ctf.cth), + db_ctf.dlen)) == NULL) + return; + } else { + db_printf("Unsupported non-compressed CTF section encountered\n"); + return; + } + + /* Lookup the total number of kernel symbols. */ + if ((nsyms = db_ctf_nsyms()) == 0) + return; + else + db_ctf.nsyms = nsyms; + + /* We made it this far, everything seems fine. */ + db_ctf.ctf_found = 1; +} + +/* + * Internal helper function - return a pointer to the CTF table + * for the current symbol table (and update the size). + */ +static const char * +db_elf_find_ctftab(db_symtab_t *stab, size_t *size) +{ + Elf_Ehdr *elf = STAB_TO_EHDR(stab); + Elf_Shdr *shp = STAB_TO_SHDR(stab, elf); + char *shstrtab; + int i; + + shstrtab = (char *)elf + shp[elf->e_shstrndx].sh_offset; + + for (i = 0; i < elf->e_shnum; i++) { + if ((shp[i].sh_flags & SHF_ALLOC) != 0 && + strcmp(ELF_CTF, shstrtab+shp[i].sh_name) == 0) { + *size = shp[i].sh_size; + return ((char *)elf + shp[i].sh_offset); + } + } + + return (NULL); +} + +void +db_dump_ctf_header(void) +{ + if (!db_ctf.ctf_found) + return; + + db_printf("CTF header found at %p (%ld)\n", db_ctf.ctftab, + db_ctf.ctftab_size); + db_printf("cth_magic: 0x%04x\n", db_ctf.cth->cth_magic); + db_printf("cth_verion: %d\n", db_ctf.cth->cth_version); + db_printf("cth_flags: 0x%02x", db_ctf.cth->cth_flags); + if (db_ctf.cth->cth_flags & CTF_F_COMPRESS) { + db_printf(" (compressed)"); + } + db_printf("\n"); + db_printf("cth_parlabel: %s\n", + db_ctf_lookup_name(db_ctf.cth->cth_parlabel)); + db_printf("cth_parname: %s\n", + db_ctf_lookup_name(db_ctf.cth->cth_parname)); + db_printf("cth_lbloff: %d\n", db_ctf.cth->cth_lbloff); + db_printf("cth_objtoff: %d\n", db_ctf.cth->cth_objtoff); + db_printf("cth_funcoff: %d\n", db_ctf.cth->cth_funcoff); + db_printf("cth_typeoff: %d\n", db_ctf.cth->cth_typeoff); + db_printf("cth_stroff: %d\n", db_ctf.cth->cth_stroff); + db_printf("cth_strlen: %d\n", db_ctf.cth->cth_strlen); + +#if 1 + db_ctf_print_functions(); +#endif + return; +} + +/* + * We need a way to get the number of symbols, so (ab)use db_elf_sym_forall() + * to give us the count. + */ +struct db_ctf_forall_arg { + int cnt; + db_sym_t sym; +}; + +static void db_ctf_forall(db_sym_t, char *, char *, int, void *); + +static void +db_ctf_forall(db_sym_t sym, char *name, char *suff, int pre, void *varg) +{ + struct db_ctf_forall_arg *arg = varg; + + if (arg->cnt-- == 0) + arg->sym = sym; +} + +static int +db_ctf_nsyms(void) +{ + int nsyms; + struct db_ctf_forall_arg dfa; + + dfa.cnt = 0; + db_elf_sym_forall(db_ctf_forall, &dfa); + nsyms = -dfa.cnt; + + /* The caller must make sure to handle zero symbols. */ + return nsyms; +} + +/* + * Convert an index to a symbol name while ensuring the type is matched. + * It must be noted this only works if the CTF table has the same order + * as the symbol table. + */ +static const char * +db_ctf_idx2sym(size_t *idx, unsigned char type) +{ + db_symtab_t *stab = &db_symtab; + Elf_Sym *symp, *symtab_start; + const Elf_Sym *st; + char *strtab; + size_t i; + + if (stab->private == NULL) + return (NULL); + + strtab = db_elf_find_strtab(stab); + if (strtab == NULL) + return (NULL); + + symtab_start = STAB_TO_SYMSTART(stab); + symp = symtab_start; + + for (i = *idx + 1; i < db_ctf.nsyms; i++) { + st = &symp[i]; + + if (ELF_ST_TYPE(st->st_info) != type) + continue; + + *idx = i; + return strtab + st->st_name; + } + + return (NULL); +} + +/* + * For a given function name, return the number of arguments. + */ +int +db_ctf_func_numargs(const char *funcname) +{ + const char *s; + unsigned short *fsp, kind, vlen; + size_t idx = 0; + int nargs; + + if (!db_ctf.ctf_found) + return (0); + + fsp = (unsigned short *)(db_ctf.data + db_ctf.cth->cth_funcoff); + while (fsp < (unsigned short *)(db_ctf.data + db_ctf.cth->cth_typeoff)) { + kind = CTF_INFO_KIND(*fsp); + vlen = CTF_INFO_VLEN(*fsp); + s = db_ctf_idx2sym(&idx, STT_FUNC); + fsp++; + + if (kind == CTF_K_UNKNOWN && vlen == 0) + continue; + + nargs = 0; + if (s != NULL) { + /* + * We have to keep increasing fsp++ while walking the + * table even if we discard the value at that location. + * This is required to keep a moving index. + * + * First increment for the return type, then for each + * parameter type. + */ + fsp++; + + while (vlen-- > 0) { + nargs++; + fsp++; + } + + if (strncmp(funcname, s, strlen(funcname)) == 0) { + return (nargs); + } + } + } + + return (0); +} + +static int +db_ctf_print_functions(void) +{ + unsigned short *fsp, kind, vlen; + size_t idx = 0, i = 0; + const char *s; + int l; + + if (!db_ctf.ctf_found) + return 1; + + fsp = (unsigned short *)(db_ctf.data + db_ctf.cth->cth_funcoff); + while (fsp < (unsigned short *)(db_ctf.data + db_ctf.cth->cth_typeoff)) { + kind = CTF_INFO_KIND(*fsp); + vlen = CTF_INFO_VLEN(*fsp); + fsp++; + + if (kind == CTF_K_UNKNOWN && vlen == 0) + continue; + + l = db_printf(" [%zu] FUNC ", i++); + if ((s = db_ctf_idx2sym(&idx, STT_FUNC)) != NULL) + db_printf("(%s)", s); + db_printf(" returns: %u args: (", *fsp++); + while (vlen-- > 0) + db_printf("%u%s", *fsp++, (vlen > 0) ? ", " : ""); + db_printf(") idx: %zu\n", idx); + } + db_printf("\n"); + return 0; +} + +static const char * +db_ctf_lookup_name(unsigned int offset) +{ + const char *name; + + if (CTF_NAME_STID(offset) != CTF_STRTAB_0) + return "external"; + + if (CTF_NAME_OFFSET(offset) >= db_ctf.cth->cth_strlen) + return "exceeds strlab"; + + if (db_ctf.cth->cth_stroff + CTF_NAME_OFFSET(offset) >= db_ctf.dlen) + return "invalid"; + + name = db_ctf.data + db_ctf.cth->cth_stroff + CTF_NAME_OFFSET(offset); + if (*name == '\0') + return "(anon)"; + + return name; +} + +static char * +db_ctf_decompress(const char *buf, size_t size, off_t len) +{ + z_stream stream; + char *data; + int error; + + /* XXX: drop malloc(9) usage */ + data = malloc(len, M_TEMP, M_WAITOK|M_ZERO|M_CANFAIL); + if (data == NULL) { + return (NULL); + } + + memset(&stream, 0, sizeof(stream)); + stream.next_in = (void *)buf; + stream.avail_in = size; + stream.next_out = data; + stream.avail_out = len; + + if ((error = inflateInit(&stream)) != Z_OK) { + db_printf("zlib inflateInit failed: %s", zError(error)); + goto exit; + } + + if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) { + db_printf("zlib inflate failed: %s", zError(error)); + inflateEnd(&stream); + goto exit; + } + + if ((error = inflateEnd(&stream)) != Z_OK) { + db_printf("zlib inflateEnd failed: %s", zError(error)); + goto exit; + } + + if (stream.total_out != len) { + db_printf("decompression failed: %llu != %llu", + stream.total_out, len); + goto exit; + } + + return (data); + +exit: + free(data, M_DEVBUF, sizeof(*data)); + return (NULL); +} diff --git a/sys/ddb/db_ctf.h b/sys/ddb/db_ctf.h new file mode 100644 index 00000000000..163e42e664a --- /dev/null +++ b/sys/ddb/db_ctf.h @@ -0,0 +1,179 @@ +/* $OpenBSD: db_ctf.h,v 1.1 2016/09/16 19:13:17 jasper Exp $ */ + +/* + * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _DDB_DB_CTF_H_ +#define _DDB_DB_CTF_H_ + +/* + * CTF ``Compact ANSI-C Type Format'' ABI header file. + */ + +struct ctf_header { + unsigned short cth_magic; + unsigned char cth_version; + unsigned char cth_flags; + unsigned int cth_parlabel; + unsigned int cth_parname; + unsigned int cth_lbloff; + unsigned int cth_objtoff; + unsigned int cth_funcoff; + unsigned int cth_typeoff; + unsigned int cth_stroff; + unsigned int cth_strlen; +}; + +#define CTF_F_COMPRESS (1 << 0) /* zlib compression */ + +struct ctf_lblent { + unsigned int ctl_label; + unsigned int ctl_typeidx; +}; + +struct ctf_stype { + unsigned int cts_name; + unsigned short cts_info; + union { + unsigned short _size; + unsigned short _type; + } _ST; +#define cts_size _ST._size +#define cts_type _ST._type +}; + +struct ctf_type { + struct ctf_stype _ctt_stype; +#define ctt_name _ctt_stype.cts_name +#define ctt_info _ctt_stype.cts_info +#define ctt_size _ctt_stype.cts_size +#define ctt_type _ctt_stype.cts_type + unsigned int ctt_lsizehi; + unsigned int ctt_lsizelo; +}; + +struct ctf_array { + unsigned short cta_contents; + unsigned short cta_index; + unsigned int cta_nelems; +}; + +struct ctf_member { + unsigned int ctm_name; + unsigned short ctm_type; + unsigned short ctm_offset; +}; + +struct ctf_lmember { + struct ctf_member _ctlm_member; +#define ctlm_name _ctlm_member.ctm_name +#define ctlm_type _ctlm_member.ctm_type +#define ctlm_pad0 _ctlm_member.ctm_offset + unsigned int ctlm_offsethi; + unsigned int ctlm_offsetlo; +}; + +#define CTF_LSTRUCT_THRESH 8192 + +struct ctf_enum { + unsigned int cte_name; + int cte_value; +}; + +#define CTF_MAGIC 0xcff1 +#define CTF_VERSION 2 + +#define CTF_MAX_NAME 0x7fffffff +#define CTF_MAX_VLEN 0x03ff +#define CTF_MAX_SIZE 0xfffe + +#define CTF_STRTAB_0 0 +#define CTF_STRTAB_1 1 + +/* + * Info macro. + */ +#define CTF_INFO_VLEN(i) (((i) & CTF_MAX_VLEN)) +#define CTF_INFO_ISROOT(i) (((i) & 0x0400) >> 10) +#define CTF_INFO_KIND(i) (((i) & 0xf800) >> 11) +#define CTF_K_UNKNOWN 0 +#define CTF_K_INTEGER 1 +#define CTF_K_FLOAT 2 +#define CTF_K_POINTER 3 +#define CTF_K_ARRAY 4 +#define CTF_K_FUNCTION 5 +#define CTF_K_STRUCT 6 +#define CTF_K_UNION 7 +#define CTF_K_ENUM 8 +#define CTF_K_FORWARD 9 +#define CTF_K_TYPEDEF 10 +#define CTF_K_VOLATILE 11 +#define CTF_K_CONST 12 +#define CTF_K_RESTRICT 13 +#define CTF_K_MAX 31 + +/* + * Integer/Float Encoding macro. + */ +#define _CTF_ENCODING(e) (((e) & 0xff000000) >> 24) +#define _CTF_OFFSET(e) (((e) & 0x00ff0000) >> 16) +#define _CTF_BITS(e) (((e) & 0x0000ffff)) + +#define CTF_INT_ENCODING(e) _CTF_ENCODING(e) +#define CTF_INT_SIGNED (1 << 0) +#define CTF_INT_CHAR (1 << 1) +#define CTF_INT_BOOL (1 << 2) +#define CTF_INT_VARARGS (1 << 3) +#define CTF_INT_OFFSET(e) _CTF_OFFSET(e) +#define CTF_INT_BITS(e) _CTF_BITS(e) + +#define CTF_FP_ENCODING(e) _CTF_ENCODING(e) +#define CTF_FP_SINGLE 1 +#define CTF_FP_DOUBLE 2 +#define CTF_FP_CPLX 3 +#define CTF_FP_DCPLX 4 +#define CTF_FP_LDCPLX 5 +#define CTF_FP_LDOUBLE 6 +#define CTF_FP_INTRVL 7 +#define CTF_FP_DINTRVL 8 +#define CTF_FP_LDINTRVL 9 +#define CTF_FP_IMAGRY 10 +#define CTF_FP_DIMAGRY 11 +#define CTF_FP_LDIMAGRY 12 +#define CTF_FP_OFFSET(e) _CTF_OFFSET(e) +#define CTF_FP_BITS(e) _CTF_BITS(e) + +/* + * Name reference macro. + */ +#define CTF_NAME_STID(n) ((n) >> 31) +#define CTF_NAME_OFFSET(n) ((n) & CTF_MAX_NAME) + +/* + * Type macro. + */ +#define CTF_SIZE_TO_LSIZE_HI(s) ((uint32_t)((uint64_t)(s) >> 32)) +#define CTF_SIZE_TO_LSIZE_LO(s) ((uint32_t)(s)) +#define CTF_TYPE_LSIZE(t) \ + (((uint64_t)(t)->ctt_lsizehi) << 32 | (t)->ctt_lsizelo) + +/* + * Member macro. + */ +#define CTF_LMEM_OFFSET(m) \ + (((uint64_t)(m)->ctlm_offsethi) << 32 | (m)->ctlm_offsetlo) + +#endif /* _DDB_DB_CTF_H_ */ diff --git a/sys/ddb/db_elf.c b/sys/ddb/db_elf.c index e7ad6afa312..d701929e662 100644 --- a/sys/ddb/db_elf.c +++ b/sys/ddb/db_elf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: db_elf.c,v 1.22 2016/04/20 08:02:59 mpi Exp $ */ +/* $OpenBSD: db_elf.c,v 1.23 2016/09/16 19:13:17 jasper Exp $ */ /* $NetBSD: db_elf.c,v 1.13 2000/07/07 21:55:18 jhawk Exp $ */ /*- @@ -39,30 +39,17 @@ #include <machine/db_machdep.h> +#include <ddb/db_elf.h> #include <ddb/db_sym.h> #include <ddb/db_output.h> #include <sys/exec_elf.h> - -typedef struct { - const char *name; /* symtab name */ - char *start; /* symtab location */ - char *end; - char *private; /* optional machdep pointer */ -} db_symtab_t; - db_symtab_t db_symtab; Elf_Sym *db_elf_sym_lookup(char *); -static char *db_elf_find_strtab(db_symtab_t *); static char *db_elf_find_linetab(db_symtab_t *, size_t *); -#define STAB_TO_SYMSTART(stab) ((Elf_Sym *)((stab)->start)) -#define STAB_TO_SYMEND(stab) ((Elf_Sym *)((stab)->end)) -#define STAB_TO_EHDR(stab) ((Elf_Ehdr *)((stab)->private)) -#define STAB_TO_SHDR(stab, e) ((Elf_Shdr *)((stab)->private + (e)->e_shoff)) - /* * Find the symbol table and strings; tell ddb about them. * @@ -201,7 +188,7 @@ db_elf_sym_init(int symsize, void *symtab, void *esymtab, const char *name) * Internal helper function - return a pointer to the string table * for the current symbol table. */ -static char * +char * db_elf_find_strtab(db_symtab_t *stab) { Elf_Ehdr *elf = STAB_TO_EHDR(stab); diff --git a/sys/ddb/db_elf.h b/sys/ddb/db_elf.h new file mode 100644 index 00000000000..474939593fa --- /dev/null +++ b/sys/ddb/db_elf.h @@ -0,0 +1,47 @@ +/* $OpenBSD: db_elf.h,v 1.1 2016/09/16 19:13:17 jasper Exp $ */ +/* $NetBSD: db_elf.c,v 1.13 2000/07/07 21:55:18 jhawk Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +typedef struct { + const char *name; /* symtab name */ + char *start; /* symtab location */ + char *end; + char *private; /* optional machdep pointer */ +} db_symtab_t; + + +#define STAB_TO_SYMSTART(stab) ((Elf_Sym *)((stab)->start)) +#define STAB_TO_SYMEND(stab) ((Elf_Sym *)((stab)->end)) +#define STAB_TO_EHDR(stab) ((Elf_Ehdr *)((stab)->private)) +#define STAB_TO_SHDR(stab, e) ((Elf_Shdr *)((stab)->private + (e)->e_shoff)) + +char *db_elf_find_strtab(db_symtab_t *); diff --git a/sys/ddb/db_extern.h b/sys/ddb/db_extern.h index cf4b8e852bc..313542d30ba 100644 --- a/sys/ddb/db_extern.h +++ b/sys/ddb/db_extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: db_extern.h,v 1.17 2016/09/04 09:22:29 mpi Exp $ */ +/* $OpenBSD: db_extern.h,v 1.18 2016/09/16 19:13:17 jasper Exp $ */ /* $NetBSD: db_extern.h,v 1.1 1996/02/05 01:57:00 christos Exp $ */ /* @@ -59,4 +59,8 @@ void db_prof_init(void); int db_prof_enable(void); void db_prof_disable(void); +/* db_ctf.c */ +int db_ctf_func_numargs(const char *); +void db_ctf_init(void); + #endif /* _DDB_DB_EXTERN_H_ */ |