Print this page
5231 ::printf doesn't handle enums
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Richard Lowe <richlowe@richlowe.net>
Reviewed by: Alex Reece <alex@delphix.com>


   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
  28  * Copyright (c) 2012 Joyent, Inc. All rights reserved.

  29  */
  30 
  31 #include <mdb/mdb_modapi.h>
  32 #include <mdb/mdb_target.h>
  33 #include <mdb/mdb_argvec.h>
  34 #include <mdb/mdb_string.h>
  35 #include <mdb/mdb_stdlib.h>
  36 #include <mdb/mdb_err.h>
  37 #include <mdb/mdb_debug.h>
  38 #include <mdb/mdb_fmt.h>
  39 #include <mdb/mdb_ctf.h>
  40 #include <mdb/mdb_ctf_impl.h>
  41 #include <mdb/mdb.h>
  42 #include <mdb/mdb_tab.h>
  43 
  44 #include <sys/isa_defs.h>
  45 #include <sys/param.h>
  46 #include <sys/sysmacros.h>
  47 #include <netinet/in.h>
  48 #include <strings.h>


2543         ssize_t size;
2544         mdb_ctf_id_t base;
2545         ctf_encoding_t e;
2546 
2547         union {
2548                 uint64_t ui8;
2549                 uint32_t ui4;
2550                 uint16_t ui2;
2551                 uint8_t ui1;
2552                 int64_t i8;
2553                 int32_t i4;
2554                 int16_t i2;
2555                 int8_t i1;
2556         } u;
2557 
2558         if (mdb_ctf_type_resolve(id, &base) == -1) {
2559                 mdb_warn("could not resolve type");
2560                 return (DCMD_ABORT);
2561         }
2562 
2563         if (mdb_ctf_type_kind(base) != CTF_K_INTEGER) {
2564                 mdb_warn("expected integer type\n");
2565                 return (DCMD_ABORT);
2566         }
2567 


2568         if (mdb_ctf_type_encoding(base, &e) != 0) {
2569                 mdb_warn("could not get type encoding");
2570                 return (DCMD_ABORT);
2571         }





2572 
2573         if (sign)
2574                 sign = e.cte_format & CTF_INT_SIGNED;
2575 
2576         size = e.cte_bits / NBBY;
2577 
2578         /*
2579          * Check to see if our life has been complicated by the presence of
2580          * a bitfield.  If it has, we will print it using logic that is only
2581          * slightly different than that found in print_bitfield(), above.  (In
2582          * particular, see the comments there for an explanation of the
2583          * endianness differences in this code.)
2584          */
2585         if (size > 8 || (e.cte_bits % NBBY) != 0 ||
2586             (size & (size - 1)) != 0) {
2587                 uint64_t mask = (1ULL << e.cte_bits) - 1;
2588                 uint64_t value = 0;
2589                 uint8_t *buf = (uint8_t *)&value;
2590                 uint8_t shift;
2591 


2733 
2734         if (mdb_ctf_type_resolve(id, &base) == -1) {
2735                 mdb_warn("could not resolve type");
2736                 return (DCMD_ABORT);
2737         }
2738 
2739         if (mdb_ctf_type_kind(base) == CTF_K_POINTER) {
2740                 uintptr_t value;
2741 
2742                 if (mdb_vread(&value, sizeof (value), addr) == -1) {
2743                         mdb_warn("failed to read pointer at %llx", addr);
2744                         return (DCMD_ERR);
2745                 }
2746 
2747                 if (mdb_readstr(buf, sizeof (buf) - 1, value) < 0) {
2748                         mdb_warn("failed to read string at %llx", value);
2749                         return (DCMD_ERR);
2750                 }
2751 
2752                 mdb_printf(fmt, buf);



















2753                 return (0);
2754         }
2755 
2756         if (mdb_ctf_type_kind(base) != CTF_K_ARRAY) {
2757                 mdb_warn("exepected pointer or array type\n");
2758                 return (DCMD_ABORT);
2759         }
2760 
2761         if (mdb_ctf_array_info(base, &r) == -1 ||
2762             mdb_ctf_type_resolve(r.mta_contents, &base) == -1 ||
2763             (size = mdb_ctf_type_size(base)) == -1) {
2764                 mdb_warn("can't determine array type");
2765                 return (DCMD_ABORT);
2766         }
2767 
2768         if (size != 1) {
2769                 mdb_warn("string format specifier requires "
2770                     "an array of characters\n");
2771                 return (DCMD_ABORT);
2772         }




   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
  28  * Copyright (c) 2012 Joyent, Inc. All rights reserved.
  29  * Copyright (c) 2014 Nexenta Systems, Inc. All rights reserved.
  30  */
  31 
  32 #include <mdb/mdb_modapi.h>
  33 #include <mdb/mdb_target.h>
  34 #include <mdb/mdb_argvec.h>
  35 #include <mdb/mdb_string.h>
  36 #include <mdb/mdb_stdlib.h>
  37 #include <mdb/mdb_err.h>
  38 #include <mdb/mdb_debug.h>
  39 #include <mdb/mdb_fmt.h>
  40 #include <mdb/mdb_ctf.h>
  41 #include <mdb/mdb_ctf_impl.h>
  42 #include <mdb/mdb.h>
  43 #include <mdb/mdb_tab.h>
  44 
  45 #include <sys/isa_defs.h>
  46 #include <sys/param.h>
  47 #include <sys/sysmacros.h>
  48 #include <netinet/in.h>
  49 #include <strings.h>


2544         ssize_t size;
2545         mdb_ctf_id_t base;
2546         ctf_encoding_t e;
2547 
2548         union {
2549                 uint64_t ui8;
2550                 uint32_t ui4;
2551                 uint16_t ui2;
2552                 uint8_t ui1;
2553                 int64_t i8;
2554                 int32_t i4;
2555                 int16_t i2;
2556                 int8_t i1;
2557         } u;
2558 
2559         if (mdb_ctf_type_resolve(id, &base) == -1) {
2560                 mdb_warn("could not resolve type");
2561                 return (DCMD_ABORT);
2562         }
2563 
2564         switch (mdb_ctf_type_kind(base)) {
2565                 case CTF_K_ENUM:
2566                         e.cte_format = CTF_INT_SIGNED;
2567                         e.cte_offset = 0;
2568                         e.cte_bits = mdb_ctf_type_size(id) * NBBY;
2569                         break;
2570                 case CTF_K_INTEGER:
2571                         if (mdb_ctf_type_encoding(base, &e) != 0) {
2572                                 mdb_warn("could not get type encoding");
2573                                 return (DCMD_ABORT);
2574                         }
2575                         break;
2576                 default:
2577                         mdb_warn("expected integer type\n");
2578                         return (DCMD_ABORT);
2579         }
2580 
2581         if (sign)
2582                 sign = e.cte_format & CTF_INT_SIGNED;
2583 
2584         size = e.cte_bits / NBBY;
2585 
2586         /*
2587          * Check to see if our life has been complicated by the presence of
2588          * a bitfield.  If it has, we will print it using logic that is only
2589          * slightly different than that found in print_bitfield(), above.  (In
2590          * particular, see the comments there for an explanation of the
2591          * endianness differences in this code.)
2592          */
2593         if (size > 8 || (e.cte_bits % NBBY) != 0 ||
2594             (size & (size - 1)) != 0) {
2595                 uint64_t mask = (1ULL << e.cte_bits) - 1;
2596                 uint64_t value = 0;
2597                 uint8_t *buf = (uint8_t *)&value;
2598                 uint8_t shift;
2599 


2741 
2742         if (mdb_ctf_type_resolve(id, &base) == -1) {
2743                 mdb_warn("could not resolve type");
2744                 return (DCMD_ABORT);
2745         }
2746 
2747         if (mdb_ctf_type_kind(base) == CTF_K_POINTER) {
2748                 uintptr_t value;
2749 
2750                 if (mdb_vread(&value, sizeof (value), addr) == -1) {
2751                         mdb_warn("failed to read pointer at %llx", addr);
2752                         return (DCMD_ERR);
2753                 }
2754 
2755                 if (mdb_readstr(buf, sizeof (buf) - 1, value) < 0) {
2756                         mdb_warn("failed to read string at %llx", value);
2757                         return (DCMD_ERR);
2758                 }
2759 
2760                 mdb_printf(fmt, buf);
2761                 return (0);
2762         }
2763 
2764         if (mdb_ctf_type_kind(base) == CTF_K_ENUM) {
2765                 const char *strval;
2766                 int value;
2767 
2768                 if (mdb_vread(&value, sizeof (value), addr) == -1) {
2769                         mdb_warn("failed to read pointer at %llx", addr);
2770                         return (DCMD_ERR);
2771                 }
2772 
2773                 if ((strval = mdb_ctf_enum_name(id, value))) {
2774                         mdb_printf(fmt, strval);
2775                 } else {
2776                         (void) mdb_snprintf(buf, sizeof (buf), "<%d>", value);
2777                         mdb_printf(fmt, buf);
2778                 }
2779 
2780                 return (0);
2781         }
2782 
2783         if (mdb_ctf_type_kind(base) != CTF_K_ARRAY) {
2784                 mdb_warn("exepected pointer or array type\n");
2785                 return (DCMD_ABORT);
2786         }
2787 
2788         if (mdb_ctf_array_info(base, &r) == -1 ||
2789             mdb_ctf_type_resolve(r.mta_contents, &base) == -1 ||
2790             (size = mdb_ctf_type_size(base)) == -1) {
2791                 mdb_warn("can't determine array type");
2792                 return (DCMD_ABORT);
2793         }
2794 
2795         if (size != 1) {
2796                 mdb_warn("string format specifier requires "
2797                     "an array of characters\n");
2798                 return (DCMD_ABORT);
2799         }