mdb_print.c revision e0ad97e30ea0a9af63c42d71690b5f387c763420
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <mdb/mdb_modapi.h>
#include <mdb/mdb_target.h>
#include <mdb/mdb_argvec.h>
#include <mdb/mdb_string.h>
#include <mdb/mdb_stdlib.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_ctf_impl.h>
#include <sys/isa_defs.h>
#include <sys/sysmacros.h>
#include <strings.h>
#include <libctf.h>
#include <ctype.h>
typedef struct holeinfo {
} holeinfo_t;
typedef struct printarg {
const char *pa_delim; /* element delimiter string */
const char *pa_prefix; /* element prefix string */
const char *pa_suffix; /* element suffix string */
int pa_nholes; /* size of holes array */
int pa_flags; /* formatting flags (see below) */
int pa_depth; /* previous depth */
int pa_nest; /* array nesting depth */
int pa_tab; /* tabstop width */
} printarg_t;
#define IS_CHAR(e) \
#define MEMBER_DELIM_ERR -1
#define MEMBER_DELIM_DONE 0
#define MEMBER_DELIM_PTR 1
#define MEMBER_DELIM_DOT 2
#define MEMBER_DELIM_LBR 3
typedef int printarg_f(const char *, const char *,
void *);
static void print_close_sou(printarg_t *, int);
/*
* Given an address, look up the symbol ID of the specified symbol in its
* containing module. We only support lookups for exact matches.
*/
static const char *
{
const char *p;
return (NULL); /* address does not exactly match a symbol */
return (NULL);
return (p);
}
return (NULL); /* address does not fall within a mapping */
return (NULL);
return (name);
}
/*
* This lets dcmds be a little fancy with their processing of type arguments
* while still treating them more or less as a single argument.
* For example, if a command is invokes like this:
*
* ::<dcmd> proc_t ...
*
* this function will just copy "proc_t" into the provided buffer. If the
* command is instead invoked like this:
*
* ::<dcmd> struct proc ...
*
* this function will place the string "struct proc" into the provided buffer
* and increment the caller's argv and argc. This allows the caller to still
* treat the type argument logically as it would an other atomic argument.
*/
int
{
return (DCMD_USAGE);
if (argc <= 1) {
return (DCMD_ABORT);
}
return (DCMD_USAGE);
} else {
}
return (0);
}
/*ARGSUSED*/
int
{
char tn[MDB_SYM_NAMLEN];
int ret;
if (flags & DCMD_ADDRSPEC)
return (DCMD_USAGE);
return (ret);
if (argc != 1)
return (DCMD_USAGE);
return (DCMD_ERR);
}
if (flags & DCMD_PIPE_OUT)
else
return (DCMD_OK);
}
/*ARGSUSED*/
int
{
const char *member;
char tn[MDB_SYM_NAMLEN];
int ret;
if (flags & DCMD_ADDRSPEC)
return (DCMD_USAGE);
return (ret);
return (DCMD_USAGE);
return (DCMD_ERR);
}
return (DCMD_ERR);
}
if (flags & DCMD_PIPE_OUT) {
mdb_warn("member %s of type %s is not byte-aligned\n",
return (DCMD_ERR);
}
return (DCMD_OK);
}
mdb_printf("offsetof (%s, %s) = %#lr",
mdb_printf("\n");
return (DCMD_OK);
}
/*ARGSUSED*/
static int
{
/*
* This function is called with every name in the enum. We make
* "arg" be the common prefix, if any.
*/
if (str[0] == 0) {
return (1);
return (0);
}
if (*str == 0) {
str--; /* don't smother a name completely */
}
break;
}
name++;
str++;
}
*str = 0;
}
struct enum_p2_info {
char *e_buf; /* buffer for holding names */
};
static int
{
return (1); /* non-power-of-2; abort processing */
return (0); /* already seen this value */
}
if (bit == 0)
else
} else {
}
}
/* skip the common prefix as necessary */
}
return (0);
}
static int
{
struct enum_p2_info eii;
}
static int
{
struct enum_p2_info eii;
prefix[0] = 0;
return (-1);
/* push out any final value, with a | if we missed anything */
if (missed != 0)
}
}
return (0);
}
struct enum_cbinfo {
const char *e_string; /* NULL for value searches */
};
#define E_PRETTY 0x01
#define E_HEX 0x02
#define E_SEARCH_STRING 0x04
#define E_SEARCH_VALUE 0x08
#define E_ELIDE_PREFIX 0x10
static void
{
(void) mdb_inc_indent(indent);
} else {
}
(void) mdb_dec_indent(indent);
mdb_printf("\n");
} else {
}
}
static int
{
if (flags & E_SEARCH_STRING) {
return (0);
} else if (flags & E_SEARCH_VALUE) {
return (0);
}
return (0);
}
void
enum_help(void)
{
mdb_printf("%s",
"Without an address and name, print all values for the enumeration \"enum\".\n"
"With an address, look up a particular value in \"enum\". With a name, look\n"
"up a particular name in \"enum\".\n");
(void) mdb_dec_indent(2);
mdb_printf("\n%<b>OPTIONS%</b>\n");
(void) mdb_inc_indent(2);
mdb_printf("%s",
" -e remove common prefixes from enum names\n"
" -x report enum values in hexadecimal\n");
}
/*ARGSUSED*/
int
{
struct enum_cbinfo info;
char prefix[MDB_SYM_NAMLEN];
int i;
NULL);
argc -= i;
argv += i;
return (i);
/*
* Check as an enumeration tag first, and fall back
* to checking for a typedef. Yes, this means that
* anonymous enumerations whose typedefs conflict with
* an enum tag can't be accessed. Don't do that.
*/
return (DCMD_ERR);
}
} else {
return (DCMD_ERR);
}
}
/* resolve it, and make sure we're looking at an enumeration */
return (DCMD_ERR);
}
return (DCMD_ERR);
}
if (argc > 2)
return (DCMD_USAGE);
if (argc == 2) {
if (flags & DCMD_ADDRSPEC) {
mdb_warn("may only specify one of: name, address\n");
return (DCMD_USAGE);
}
} else {
return (DCMD_USAGE);
}
}
if (flags & DCMD_ADDRSPEC) {
search = mdb_get_dot();
}
mdb_warn("value '%lld' out of enumeration range\n",
search);
}
}
if (isp2)
else
}
/* if the enum is a power-of-two one, process it that way */
return (DCMD_OK);
}
prefix[0] = 0;
return (DCMD_ERR);
}
type);
else
type);
return (DCMD_ERR);
}
return (DCMD_OK);
}
static int
{
const char *p;
mdb_var_t *v;
mdb_warn("'%c' may not be used in a variable "
"name\n", *p);
return (DCMD_ABORT);
}
return (DCMD_ERR);
} else {
if (v->v_flags & MDB_NV_RDONLY) {
return (DCMD_ABORT);
}
}
/*
* If there already exists a vcb for this variable, we may be
* calling the dcmd in a loop. We only create a vcb for this
* variable on the first invocation.
*/
return (0);
}
/*ARGSUSED*/
int
{
int ret;
return (DCMD_USAGE);
/*
* We are being given a raw offset in lieu of a type and
* member; confirm the arguments.
*/
return (DCMD_USAGE);
argv++;
argc--;
mdb_warn("offset must fall on a word boundary\n");
return (DCMD_ABORT);
}
} else {
const char *member;
char buf[MDB_SYM_NAMLEN];
int ret;
if (ret != 0)
return (ret);
return (DCMD_ABORT);
}
argv++;
argc--;
return (DCMD_USAGE);
argv++;
argc--;
mdb_warn("failed to find member %s of type %s",
return (DCMD_ABORT);
}
return (DCMD_ABORT);
}
}
/*
* If we have any unchewed arguments, a variable name must be present.
*/
if (argc == 1) {
return (DCMD_USAGE);
return (ret);
} else if (argc != 0) {
return (DCMD_USAGE);
}
a = addr;
do {
mdb_printf("%lr\n", a);
mdb_warn("failed to read next pointer from object %p",
a);
return (DCMD_ERR);
}
a = tmp;
return (DCMD_OK);
}
int
{
char tn[MDB_SYM_NAMLEN];
if (!(flags & DCMD_ADDRSPEC))
return (DCMD_USAGE);
if (argc >= 2) {
if (ret != 0)
return (ret);
return (DCMD_USAGE);
return (DCMD_ABORT);
}
else
} else {
return (DCMD_ERR);
}
return (DCMD_USAGE);
return (ret);
} else if (argc > 3) {
return (DCMD_USAGE);
}
}
return (DCMD_OK);
}
/*
* Print an integer bitfield in hexadecimal by reading the enclosing byte(s)
* and then shifting and masking the data in the lower bits of a uint64_t.
*/
static int
{
const char *format;
return (0);
return (0);
}
/*
* On big-endian machines, we need to adjust the buf pointer to refer
* to the lowest 'size' bytes in 'value', and we need shift based on
* the offset from the end of the data, not the offset of the start.
*/
#ifdef _BIG_ENDIAN
#endif
mdb_warn("failed to read %lu bytes at %llx",
return (1);
}
/*
* Offsets are counted from opposite ends on little- and
* big-endian machines.
*/
#ifdef _BIG_ENDIAN
#endif
/*
* If the bits we want do not begin on a byte boundary, shift the data
* right so that the value is in the lowest 'cte_bits' of 'value'.
*/
/*
* We default to printing signed bitfields as decimals,
* and unsigned bitfields in hexadecimal. If they specify
* hexadecimal, we treat the field as unsigned.
*/
} else {
/* sign-extend value, and print as a signed decimal */
format = "%#lld";
}
return (0);
}
/*
* Print out a character or integer value. We use some simple heuristics,
* described below, to determine the appropriate radix to use for output.
*/
static int
{
const char *const *fsp;
union {
time_t t;
} u;
return (0);
mdb_printf("...\n");
return (0);
}
/*
* If the size is not a power-of-two number of bytes in the range 1-8
* then we assume it is a bitfield and print it as such.
*/
mdb_printf("'");
return (1);
mdb_printf("'");
return (0);
}
mdb_warn("failed to read %lu bytes at %llx",
return (1);
}
/*
* We pretty-print time_t values as a calendar date and time.
*/
mdb_printf("%Y", u.t);
return (0);
}
/*
* The default format is hexadecimal.
*/
else
switch (size) {
case sizeof (uint8_t):
break;
case sizeof (uint16_t):
break;
case sizeof (uint32_t):
break;
case sizeof (uint64_t):
break;
}
return (0);
}
/*ARGSUSED*/
static int
{
return (0);
if (mdb_ctf_type_encoding(base, &e) != 0) {
return (0);
}
}
/*
* Print out a floating point value. We only provide support for floats in
* the ANSI-C float, double, and long double formats.
*/
/*ARGSUSED*/
static int
{
#ifndef _KMDB
union {
float f;
double d;
long double ld;
} u;
return (0);
if (mdb_ctf_type_encoding(base, &e) == 0) {
if (e.cte_format == CTF_FP_SINGLE &&
sizeof (u.f), addr) != sizeof (u.f)) {
return (1);
}
} else if (e.cte_format == CTF_FP_DOUBLE &&
sizeof (u.d), addr) != sizeof (u.d)) {
return (1);
}
} else if (e.cte_format == CTF_FP_LDOUBLE &&
return (1);
}
} else {
mdb_printf("??? (unsupported FP format %u / %u bits\n",
e.cte_format, e.cte_bits);
}
} else
#else
mdb_printf("<FLOAT>");
#endif
return (0);
}
/*
* Print out a pointer value as a symbol name + offset or a hexadecimal value.
* If the pointer itself is a char *, we attempt to read a bit of the data
* referenced by the pointer and display it if it is a printable ASCII string.
*/
/*ARGSUSED*/
static int
{
char buf[256];
return (0);
return (1);
}
return (0);
}
return (0);
}
}
return (0);
}
/*
* Print out a fixed-size array. We special-case arrays of characters
* and attempt to print them out as ASCII strings if possible. For other
* arrays, we iterate over a maximum of pa_armemlim members and call
* mdb_ctf_type_visit() again on each element to print its value.
*/
/*ARGSUSED*/
static int
{
int d, sou;
char buf[8];
char *str;
return (0);
mdb_printf("[ ... ]");
return (0);
}
/*
* Determine the base type and size of the array's content. If this
* fails, we cannot print anything and just give up.
*/
return (0);
}
/*
* Read a few bytes and determine if the content appears to be
* printable ASCII characters. If so, read the entire array and
* attempt to display it as a string if it is printable.
*/
return (1);
}
if (strisprint(str)) {
return (0);
}
}
else
limit = r.mta_nelems;
if (limit == 0) {
mdb_printf("[ ... ]");
return (0);
}
if (sou) {
mdb_printf("[\n");
} else {
mdb_printf("[ ");
}
if (limit < r.mta_nelems)
else
}
mdb_warn("failed to print array data");
return (1);
}
}
if (sou) {
print_close_sou(&pa, d);
if (limit < r.mta_nelems) {
mdb_printf("%*s... ]",
} else {
mdb_printf("%*s]",
}
}
/* copy the hole array info, since it may have been grown */
return (0);
}
/*
* Print out a struct or union header. We need only print the open brace
* because mdb_ctf_type_visit() itself will automatically recurse through
* all members of the given struct or union.
*/
/*ARGSUSED*/
static int
{
mdb_printf("{ ... }");
else
mdb_printf("{");
return (0);
}
/*
* Print an enum value. We attempt to convert the value to the corresponding
* enum name and print that if possible.
*/
/*ARGSUSED*/
static int
{
const char *ename;
int value;
if (!(flags & PA_SHOWVAL))
return (0);
return (1);
}
else
(void) mdb_inc_indent(8);
mdb_printf(" (");
ename = "???";
}
}
mdb_printf(")");
(void) mdb_dec_indent(8);
return (0);
}
/*
* This will only get called if the structure isn't found in any available CTF
* data.
*/
/*ARGSUSED*/
static int
{
char basename[MDB_SYM_NAMLEN];
mdb_printf("; ");
else
mdb_printf("<forward declaration of unknown type>");
return (0);
}
static void
{
static const char *const name = "<<HOLE>>";
char type[MDB_SYM_NAMLEN];
int bitfield =
size > 8 ||
/*
* The hole is larger than the largest integer type. To
* handle this, we split up the hole at 8-byte-aligned
* boundaries, recursing to print each subsection. For
* normal C structures, we'll loop at most twice.
*/
}
return;
}
if (bitfield)
else
else
mdb_printf("%llx.%lx ",
}
/*
* We fake up a ctf_encoding_t, and use print_int_val() to print
* the value. Holes are always processed as unsigned integers.
*/
bzero(&e, sizeof (e));
e.cte_format = 0;
e.cte_offset = 0;
else
}
/*
* The print_close_sou() function is called for each structure or union
* which has been completed. For structures, we detect and print any holes
* before printing the closing brace.
*/
static void
{
}
/* if the struct is an array element, print a comma after the } */
}
static printarg_f *const printfuncs[] = {
print_int, /* CTF_K_INTEGER */
print_float, /* CTF_K_FLOAT */
print_ptr, /* CTF_K_POINTER */
print_array, /* CTF_K_ARRAY */
print_ptr, /* CTF_K_FUNCTION */
print_sou, /* CTF_K_STRUCT */
print_sou, /* CTF_K_UNION */
print_enum, /* CTF_K_ENUM */
print_tag /* CTF_K_FORWARD */
};
/*
* The elt_print function is used as the mdb_ctf_type_visit callback. For
* each element, we print an appropriate name prefix and then call the
* print subroutine for this type class in the array above.
*/
static int
{
print_close_sou(pap, d);
return (0);
if (!mdb_ctf_type_valid(base) ||
return (-1); /* errno is set for us */
/*
* If basetype is different and informative, concatenate
* <<basetype>> (or <<baset...>> if it doesn't fit)
*
* We just use the end of the buffer to store the type name, and
* only connect it up if that's necessary.
*/
char *basetype;
(void) strlcpy(
}
}
/*
* grow the hole array, if necessary
*/
}
/* compute the next expected offset */
if (kind == CTF_K_INTEGER &&
mdb_ctf_type_encoding(base, &e) == 0)
else {
/* something bad happened, disable hole checking */
}
if (IS_COMPOSITE(kind)) {
hole++;
}
}
else
mdb_printf("%llx.%lx ",
}
/*
* We want to avoid printing a trailing space when
* dealing with pointers in a structure, so we end
* up with:
*
* label_t *t_onfault = 0
*
* If depth is zero, always print the trailing space unless
* we also have a prefix.
*/
mdb_printf(" ");
}
}
if (mdb_ctf_type_encoding(base, &e) == 0) {
size > 8 ||
}
}
if (depth != 0 ||
mdb_warn("unknown ctf for %s type %s kind %d\n",
return (-1);
}
if (rc != 0)
else
return (rc);
}
/*
* Special semantics for pipelines.
*/
static int
{
union {
} u;
mdb_warn("could not resolve type\n");
return (-1);
}
/*
* If the user gives -a, then always print out the address of the
* member.
*/
return (0);
}
switch (mdb_ctf_type_kind(base)) {
case CTF_K_POINTER:
return (-1);
}
break;
case CTF_K_INTEGER:
case CTF_K_ENUM:
if (mdb_ctf_type_encoding(base, &e) != 0) {
mdb_printf("could not get type encoding\n");
return (-1);
}
/*
* For immediate values, we just print out the value.
*/
}
mdb_warn("failed to read %lu bytes at %p",
return (-1);
}
switch (size) {
case sizeof (uint8_t):
break;
case sizeof (uint16_t):
break;
case sizeof (uint32_t):
break;
case sizeof (uint64_t):
break;
}
mdb_printf("\n");
break;
case CTF_K_FUNCTION:
case CTF_K_FLOAT:
case CTF_K_ARRAY:
case CTF_K_UNKNOWN:
case CTF_K_STRUCT:
case CTF_K_UNION:
case CTF_K_FORWARD:
/*
* For these types, always print the address of the member
*/
break;
default:
break;
}
return (0);
}
static int
parse_delimiter(char **strp)
{
switch (**strp) {
case '\0':
return (MEMBER_DELIM_DONE);
case '.':
return (MEMBER_DELIM_DOT);
case '[':
return (MEMBER_DELIM_LBR);
case '-':
if (**strp == '>') {
return (MEMBER_DELIM_PTR);
}
/*FALLTHROUGH*/
default:
return (MEMBER_DELIM_ERR);
}
}
static int
{
if (size == sizeof (mdb_tgt_addr_t)) {
return (-1);
}
} else {
return (-1);
}
}
/*
* We've dereferenced at least once, we must be on the real
* target. If we were in the immediate target, reset to the real
* target; it's reset as needed when we return to the print
* routines.
*/
return (0);
}
static int
{
int delim;
char member[64];
char buf[128];
char *end;
int kind;
/*
* id always has the unresolved type for printing error messages
* that include the type; rid always has the resolved type for
* use in mdb_ctf_* calls. It is possible for this command to fail,
* however, if the resolved type is in the parent and it is currently
* unavailable. Note that we also can't print out the name of the
* type, since that would also rely on looking up the resolved name.
*/
mdb_warn("failed to resolve type");
return (-1);
}
/*
* If the user fails to specify an initial delimiter, guess -> for
* pointer types and . for non-pointer types.
*/
if (delim == MEMBER_DELIM_ERR)
*last_deref = FALSE;
while (delim != MEMBER_DELIM_DONE) {
switch (delim) {
case MEMBER_DELIM_PTR:
if (kind != CTF_K_POINTER) {
mdb_warn("%s is not a pointer type\n",
return (-1);
}
return (-1);
off = 0;
break;
case MEMBER_DELIM_DOT:
mdb_warn("%s is not a struct or union type\n",
return (-1);
}
break;
case MEMBER_DELIM_LBR:
mdb_warn("no trailing ']'\n");
return (-1);
}
switch (mdb_ctf_type_kind(rid)) {
case CTF_K_POINTER:
return (-1);
if (size <= 0) {
mdb_warn("cannot dereference void "
"type\n");
return (-1);
}
off = 0;
*last_deref = TRUE;
break;
case CTF_K_ARRAY:
mdb_warn("index %r is outside of "
"array bounds [0 .. %r]\n",
}
if (size <= 0) {
mdb_warn("cannot dereference void "
"type\n");
return (-1);
}
off = 0;
break;
default:
mdb_warn("cannot index into non-array, "
"non-pointer type\n");
return (-1);
}
continue;
case MEMBER_DELIM_ERR:
default:
return (-1);
}
*last_deref = FALSE;
/*
* Find the end of the member name; assume that a member
* name is at least one character long.
*/
continue;
return (-1);
}
}
return (0);
}
/*
* Recursively descend a print a given data structure. We create a struct of
* the relevant print arguments and then call mdb_ctf_type_visit() to do the
* traversal, using elt_print() as the callback for each element.
*/
/*ARGSUSED*/
int
{
int d, i;
char s_name[MDB_SYM_NAMLEN];
NULL);
uflags |= PA_SHOWNAME;
mdb_warn("-p and -i options are incompatible\n");
return (DCMD_ERR);
}
argc -= i;
argv += i;
int ret;
return (DCMD_USAGE);
sizeof (s_name))) != 0)
return (ret);
return (DCMD_ABORT);
}
} else {
argc--;
argv++;
}
return (DCMD_USAGE);
return (DCMD_ERR);
return (DCMD_ERR);
}
else
if (opt_i) {
const char *vargv[2];
0, 2, vargv);
}
if (opt_c != MDB_ARR_NOLIMIT)
if (opt_C)
if (opt_l != MDB_ARR_NOLIMIT)
if (opt_L)
if (argc > 0) {
for (i = 0; i < argc; i++) {
int last_deref;
int kind;
char buf[MDB_SYM_NAMLEN];
&off, &last_deref) != 0) {
err = DCMD_ABORT;
goto out;
}
/*
* If the member string ends with a "[0]"
* (last_deref * is true) and the type is a
* structure or union, * print "->" rather
* than "[0]." in elt_print.
*/
char *end;
"%s", member);
*end = '\0';
} else {
}
} else {
err = DCMD_ABORT;
goto out;
}
}
if (flags & DCMD_PIPE_OUT) {
mdb_warn("failed to print type");
goto out;
}
} else if (off != 0) {
&pa) != 0) {
mdb_warn("failed to print type");
goto out;
}
} else {
&pa) == -1) {
mdb_warn("failed to print type");
goto out;
}
print_close_sou(&pa, d);
}
}
} else if (flags & DCMD_PIPE_OUT) {
mdb_warn("failed to print type");
goto out;
}
} else {
mdb_warn("failed to print type");
goto out;
}
print_close_sou(&pa, d);
}
out:
return (err);
}
void
print_help(void)
{
"-a show address of object\n"
"-C unlimit the length of character arrays\n"
"-c limit limit the length of character arrays\n"
"-d output values in decimal\n"
"-h print holes in structures\n"
"-i interpret address as data of the given type\n"
"-L unlimit the length of standard arrays\n"
"-l limit limit the length of standard arrays\n"
"-n don't print pointers as symbol offsets\n"
"-p interpret address as a physical memory address\n"
"-s depth limit the recursion depth\n"
"-T show type and <<base type>> of object\n"
"-t show type of object\n"
"-x output values in hexadecimal\n"
"\n"
"type may be omitted if the C type of addr can be inferred.\n"
"\n"
"Members may be specified with standard C syntax using the\n"
"array indexing operator \"[index]\", structure member\n"
"operator \".\", or structure pointer operator \"->\".\n"
"\n"
"Offsets must use the $[ expression ] syntax\n");
}