Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/prstat/prutil.c
          +++ new/usr/src/cmd/prstat/prutil.c
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright (c) 2013 Gary Mills
  23   23   *
  24   24   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  25   25   * Use is subject to license terms.
  26   26   *
  27   27   * Portions Copyright 2009 Chad Mynhier
       28 + * Copyright 2018 Joyent, Inc.  All rights reserved.
  28   29   */
  29   30  
  30   31  #include <sys/types.h>
  31   32  #include <sys/param.h>
  32   33  #include <sys/resource.h>
  33   34  #include <sys/priocntl.h>
  34   35  #include <sys/rtpriocntl.h>
  35   36  #include <sys/tspriocntl.h>
  36   37  #include <zone.h>
  37   38  
↓ open down ↓ 279 lines elided ↑ open up ↑
 317  318                          (void) snprintf(str, len, "%.*s%c", width - 1,
 318  319                              zone_name, '*');
 319  320                  else
 320  321                          (void) snprintf(str, len, "%-28s", zone_name);
 321  322          }
 322  323  }
 323  324  
 324  325  /*
 325  326   * Remove all unprintable characters from process name
 326  327   */
 327      -void
 328      -stripfname(char *buf)
      328 +static void
      329 +stripfname(char *buf, size_t bufsize, const char *pname)
 329  330  {
 330  331          int bytesleft = PRFNSZ;
 331  332          wchar_t wchar;
 332  333          int length;
 333  334          char *cp;
 334  335  
      336 +        (void) strlcpy(buf, pname, bufsize);
      337 +
 335  338          buf[bytesleft - 1] = '\0';
 336  339  
 337  340          for (cp = buf; *cp != '\0'; cp += length) {
 338  341                  length = mbtowc(&wchar, cp, MB_LEN_MAX);
 339  342                  if (length <= 0) {
 340  343                          *cp = '\0';
 341  344                          break;
 342  345                  }
 343  346                  if (!iswprint(wchar)) {
 344  347                          if (bytesleft <= length) {
 345  348                                  *cp = '\0';
 346  349                                  break;
 347  350                          }
 348  351                          (void) memmove(cp, cp + length, bytesleft - length);
 349  352                          length = 0;
 350  353                  }
 351  354                  bytesleft -= length;
 352  355          }
      356 +}
      357 +
      358 +
      359 +/*
      360 + * prstat has always implicitly wanted a terminal width of at least 80 columns
      361 + * (when a TTY is present).  If run in a terminal narrower than 80 columns,
      362 + * prstat output may wrap.  For wider terminals, we allow the last column to use
      363 + * the additional space.
      364 + *
      365 + * We never truncate if using -c, or not outputting to a TTY.
      366 + */
      367 +static int
      368 +format_namewidth(void)
      369 +{
      370 +        int prefixlen = 0;
      371 +
      372 +        if (opts.o_cols == 0 || !(opts.o_outpmode & (OPT_TERMCAP | OPT_TRUNC)))
      373 +                return (0);
      374 +
      375 +        if (opts.o_outpmode & OPT_PSINFO) {
      376 +                if (opts.o_outpmode & OPT_LGRP)
      377 +                        prefixlen = 64;
      378 +                else
      379 +                        prefixlen = 59;
      380 +        } else if (opts.o_outpmode & OPT_MSACCT) {
      381 +                prefixlen = 64;
      382 +        }
      383 +
      384 +        return (opts.o_cols - prefixlen);
      385 +}
      386 +
      387 +void
      388 +format_name(lwp_info_t *lwp, char *buf, size_t buflen)
      389 +{
      390 +        int pname_width = PRFNSZ;
      391 +        char nr_suffix[20];
      392 +        char pname[PRFNSZ];
      393 +        int width;
      394 +        int n;
      395 +
      396 +        stripfname(pname, sizeof (pname), lwp->li_info.pr_fname);
      397 +
      398 +        if (opts.o_outpmode & OPT_LWPS) {
      399 +                n = snprintf(nr_suffix, sizeof (nr_suffix), "%d",
      400 +                    lwp->li_info.pr_lwp.pr_lwpid);
      401 +        } else {
      402 +                n = snprintf(nr_suffix, sizeof (nr_suffix), "%d",
      403 +                    lwp->li_info.pr_nlwp + lwp->li_info.pr_nzomb);
      404 +        }
      405 +
      406 +        width = format_namewidth();
      407 +
      408 +        /* If we're over budget, truncate the process name not the LWP part. */
      409 +        if (strlen(pname) > (width - n - 1)) {
      410 +                pname_width = width - n - 1;
      411 +                pname[pname_width - 1] = '*';
      412 +        }
      413 +
      414 +        if ((opts.o_outpmode & OPT_LWPS) && lwp->li_lwpname[0] != '\0') {
      415 +                n = snprintf(buf, buflen, "%.*s/%s [%s]", pname_width,
      416 +                    pname, nr_suffix, lwp->li_lwpname);
      417 +        } else {
      418 +                n = snprintf(buf, buflen, "%.*s/%s", pname_width,
      419 +                    pname, nr_suffix);
      420 +        }
      421 +
      422 +        if (width > 0 && strlen(buf) > width)
      423 +                buf[width] = '\0';
 353  424  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX