Print this page
10921 mdb_nicenum() needs smatch fix


   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   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 /*
  23  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2013 by Delphix. All rights reserved.
  25  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.

  26  */
  27 
  28 #include <mdb/mdb_modapi.h>
  29 #include <mdb/mdb_module.h>
  30 #include <mdb/mdb_string.h>
  31 #include <mdb/mdb_debug.h>
  32 #include <mdb/mdb_callb.h>
  33 #include <mdb/mdb_dump.h>
  34 #include <mdb/mdb_err.h>
  35 #include <mdb/mdb_io.h>
  36 #include <mdb/mdb_lex.h>
  37 #include <mdb/mdb_frame.h>
  38 #include <mdb/mdb.h>
  39 
  40 /*
  41  * Private callback structure for implementing mdb_walk_dcmd, below.
  42  */
  43 typedef struct {
  44         mdb_idcmd_t *dw_dcmd;
  45         mdb_argvec_t dw_argv;


  75 void
  76 mdb_nicenum(uint64_t num, char *buf)
  77 {
  78         uint64_t n = num;
  79         int index = 0;
  80         char *u;
  81 
  82         while (n >= 1024) {
  83                 n = (n + (1024 / 2)) / 1024; /* Round up or down */
  84                 index++;
  85         }
  86 
  87         u = &" \0K\0M\0G\0T\0P\0E\0"[index*2];
  88 
  89         if (index == 0) {
  90                 (void) mdb_snprintf(buf, MDB_NICENUM_BUFLEN, "%llu",
  91                     (u_longlong_t)n);
  92         } else if (n < 10 && (num & (num - 1)) != 0) {
  93                 (void) mdb_snprintfrac(buf, MDB_NICENUM_BUFLEN,
  94                     num, 1ULL << 10 * index, 2);
  95                 strcat(buf, u);
  96         } else if (n < 100 && (num & (num - 1)) != 0) {
  97                 (void) mdb_snprintfrac(buf, MDB_NICENUM_BUFLEN,
  98                     num, 1ULL << 10 * index, 1);
  99                 strcat(buf, u);
 100         } else {
 101                 (void) mdb_snprintf(buf, MDB_NICENUM_BUFLEN, "%llu%s",
 102                     (u_longlong_t)n, u);
 103         }
 104 }
 105 
 106 ssize_t
 107 mdb_vread(void *buf, size_t nbytes, uintptr_t addr)
 108 {
 109         ssize_t rbytes = mdb_tgt_vread(mdb.m_target, buf, nbytes, addr);
 110 
 111         if (rbytes > 0 && rbytes < nbytes)
 112                 return (set_errbytes(rbytes, nbytes));
 113 
 114         return (rbytes);
 115 }
 116 
 117 ssize_t
 118 mdb_vwrite(const void *buf, size_t nbytes, uintptr_t addr)
 119 {




   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   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 /*
  23  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2013 by Delphix. All rights reserved.
  25  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
  26  * Copyright 2019 Joyent, Inc.
  27  */
  28 
  29 #include <mdb/mdb_modapi.h>
  30 #include <mdb/mdb_module.h>
  31 #include <mdb/mdb_string.h>
  32 #include <mdb/mdb_debug.h>
  33 #include <mdb/mdb_callb.h>
  34 #include <mdb/mdb_dump.h>
  35 #include <mdb/mdb_err.h>
  36 #include <mdb/mdb_io.h>
  37 #include <mdb/mdb_lex.h>
  38 #include <mdb/mdb_frame.h>
  39 #include <mdb/mdb.h>
  40 
  41 /*
  42  * Private callback structure for implementing mdb_walk_dcmd, below.
  43  */
  44 typedef struct {
  45         mdb_idcmd_t *dw_dcmd;
  46         mdb_argvec_t dw_argv;


  76 void
  77 mdb_nicenum(uint64_t num, char *buf)
  78 {
  79         uint64_t n = num;
  80         int index = 0;
  81         char *u;
  82 
  83         while (n >= 1024) {
  84                 n = (n + (1024 / 2)) / 1024; /* Round up or down */
  85                 index++;
  86         }
  87 
  88         u = &" \0K\0M\0G\0T\0P\0E\0"[index*2];
  89 
  90         if (index == 0) {
  91                 (void) mdb_snprintf(buf, MDB_NICENUM_BUFLEN, "%llu",
  92                     (u_longlong_t)n);
  93         } else if (n < 10 && (num & (num - 1)) != 0) {
  94                 (void) mdb_snprintfrac(buf, MDB_NICENUM_BUFLEN,
  95                     num, 1ULL << 10 * index, 2);
  96                 (void) strcat(buf, u);
  97         } else if (n < 100 && (num & (num - 1)) != 0) {
  98                 (void) mdb_snprintfrac(buf, MDB_NICENUM_BUFLEN,
  99                     num, 1ULL << 10 * index, 1);
 100                 (void) strcat(buf, u);
 101         } else {
 102                 (void) mdb_snprintf(buf, MDB_NICENUM_BUFLEN, "%llu%s",
 103                     (u_longlong_t)n, u);
 104         }
 105 }
 106 
 107 ssize_t
 108 mdb_vread(void *buf, size_t nbytes, uintptr_t addr)
 109 {
 110         ssize_t rbytes = mdb_tgt_vread(mdb.m_target, buf, nbytes, addr);
 111 
 112         if (rbytes > 0 && rbytes < nbytes)
 113                 return (set_errbytes(rbytes, nbytes));
 114 
 115         return (rbytes);
 116 }
 117 
 118 ssize_t
 119 mdb_vwrite(const void *buf, size_t nbytes, uintptr_t addr)
 120 {