|
| 1 | +/* |
| 2 | + * This file and its contents are supplied under the terms of the |
| 3 | + * Common Development and Distribution License ("CDDL"), version 1.0. |
| 4 | + * You may only use this file in accordance with the terms of version |
| 5 | + * 1.0 of the CDDL. |
| 6 | + * |
| 7 | + * A full copy of the text of the CDDL should have accompanied this |
| 8 | + * source. A copy of the CDDL is also available via the Internet at |
| 9 | + * http://www.illumos.org/license/CDDL. |
| 10 | + */ |
| 11 | + |
| 12 | +/* |
| 13 | + * Copyright 2016 Gordon Ross <[email protected]> |
| 14 | + */ |
| 15 | + |
| 16 | +/* |
| 17 | + * Example mdb module: foo |
| 18 | + */ |
| 19 | +#include <sys/mdb_modapi.h> |
| 20 | +#include <sys/foo.h> |
| 21 | + |
| 22 | +/* |
| 23 | + * One would be correct to observe that a dcmd is unnecessary for |
| 24 | + * just printing a variable with mdb. This is just an example, |
| 25 | + * where a more real-world case would print something larger and |
| 26 | + * more complex than a foocnt_t. |
| 27 | + */ |
| 28 | + |
| 29 | +void |
| 30 | +foo_cnt_help(void) |
| 31 | +{ |
| 32 | + mdb_printf("Print device ID information of Intel graphics card.\n"); |
| 33 | +} |
| 34 | + |
| 35 | +/* ARGSUSED */ |
| 36 | +static int |
| 37 | +foo_cnt_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) |
| 38 | +{ |
| 39 | + GElf_Sym sym; |
| 40 | + foocnt_t count; |
| 41 | + |
| 42 | + /* Does not take an address. */ |
| 43 | + if (flags & DCMD_ADDRSPEC) |
| 44 | + return (DCMD_USAGE); |
| 45 | + |
| 46 | + /* Locate the foo_count variable */ |
| 47 | + if (mdb_lookup_by_obj("foo", "foo_count", &sym)) { |
| 48 | + mdb_warn("failed to lookup `foo_count'\n"); |
| 49 | + return (DCMD_ERR); |
| 50 | + } |
| 51 | + addr = (uintptr_t)sym.st_value; |
| 52 | + |
| 53 | + if (mdb_vread(&count, sizeof (count), addr) == -1) { |
| 54 | + mdb_warn("failed to read foo_count at %p", addr); |
| 55 | + return (DCMD_ERR); |
| 56 | + } |
| 57 | + |
| 58 | + mdb_printf("foo_count = %d\n", count); |
| 59 | + |
| 60 | + return (DCMD_OK); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +/* |
| 65 | + * MDB module linkage information: |
| 66 | + * |
| 67 | + * We declare a list of structures describing our dcmds, a list of structures |
| 68 | + * describing our walkers, and a function named _mdb_init to return a pointer |
| 69 | + * to our module information. |
| 70 | + */ |
| 71 | + |
| 72 | +static const mdb_dcmd_t dcmds[] = { |
| 73 | + { |
| 74 | + "foo_cnt", |
| 75 | + "?", |
| 76 | + "get foo_count", |
| 77 | + foo_cnt_dcmd, |
| 78 | + foo_cnt_help |
| 79 | + }, |
| 80 | + { NULL } |
| 81 | +}; |
| 82 | + |
| 83 | +static const mdb_walker_t walkers[] = { |
| 84 | + /* none */ |
| 85 | + { NULL } |
| 86 | +}; |
| 87 | + |
| 88 | +static const mdb_modinfo_t modinfo = { |
| 89 | + MDB_API_VERSION, dcmds, walkers |
| 90 | +}; |
| 91 | + |
| 92 | +const mdb_modinfo_t * |
| 93 | +_mdb_init(void) |
| 94 | +{ |
| 95 | + return (&modinfo); |
| 96 | +} |
0 commit comments