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

@@ -19,10 +19,12 @@
  * CDDL HEADER END
  */
 /*
  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright 2018 Joyent, Inc.
  */
 
 #include <sys/isa_defs.h>
 
 #include <stdio.h>

@@ -135,11 +137,11 @@
 
 static  int     thr_stack(const td_thrhandle_t *, void *);
 static  void    free_threadinfo(void);
 static  struct threadinfo *find_thread(id_t);
 static  int     all_call_stacks(pstack_handle_t *, int);
-static  void    tlhead(id_t, id_t);
+static  void    tlhead(id_t, id_t, const char *);
 static  int     print_frame(void *, prgregset_t, uint_t, const long *);
 static  void    print_zombie(struct ps_prochandle *, struct threadinfo *);
 static  void    print_syscall(const lwpstatus_t *, prgregset_t);
 static  void    call_stack(pstack_handle_t *, const lwpstatus_t *);
 

@@ -378,10 +380,11 @@
 
 static int
 thread_call_stack(void *data, const lwpstatus_t *psp,
     const lwpsinfo_t *pip)
 {
+        char lwpname[THREAD_NAME_MAX] = "";
         pstack_handle_t *h = data;
         lwpstatus_t lwpstatus;
         struct threadinfo *tip;
 
         if (!proc_lwp_in_set(h->lwps, pip->pr_lwpid))

@@ -389,11 +392,14 @@
         h->count++;
 
         if ((tip = find_thread(pip->pr_lwpid)) == NULL)
                 return (0);
 
-        tlhead(tip->threadid, pip->pr_lwpid);
+        (void) Plwp_getname(h->proc, pip->pr_lwpid,
+            lwpname, sizeof (lwpname));
+
+        tlhead(tip->threadid, pip->pr_lwpid, lwpname);
         tip->threadid = 0;      /* finish eliminating tid */
         if (psp)
                 call_stack(h, psp);
         else {
                 if (tip->state == TD_THR_ZOMBIE)

@@ -410,17 +416,21 @@
 
 static int
 lwp_call_stack(void *data,
         const lwpstatus_t *psp, const lwpsinfo_t *pip)
 {
+        char lwpname[THREAD_NAME_MAX] = "";
         pstack_handle_t *h = data;
 
         if (!proc_lwp_in_set(h->lwps, pip->pr_lwpid))
                 return (0);
         h->count++;
 
-        tlhead(0, pip->pr_lwpid);
+        (void) Plwp_getname(h->proc, pip->pr_lwpid,
+            lwpname, sizeof (lwpname));
+
+        tlhead(0, pip->pr_lwpid, lwpname);
         if (psp)
                 call_stack(h, psp);
         else
                 (void) printf("\t** zombie "
                     "(exited, not detached, not yet joined) **\n");

@@ -460,11 +470,11 @@
                                 tip->threadid = 0;
 
                         if ((tid = tip->threadid) != 0) {
                                 (void) memcpy(lwpstatus.pr_reg, tip->regs,
                                     sizeof (prgregset_t));
-                                tlhead(tid, tip->lwpid);
+                                tlhead(tid, tip->lwpid, NULL);
                                 if (tip->state == TD_THR_ZOMBIE)
                                         print_zombie(Pr, tip);
                                 else
                                         call_stack(h, &lwpstatus);
                         }

@@ -473,27 +483,53 @@
                 }
         }
         return (0);
 }
 
+/* The width of the header */
+#define HEAD_WIDTH      (62)
 static void
-tlhead(id_t threadid, id_t lwpid)
+tlhead(id_t threadid, id_t lwpid, const char *name)
 {
+        char buf[128] = { 0 };
+        char num[16];
+        ssize_t amt = 0;
+        int i;
+
         if (threadid == 0 && lwpid == 0)
                 return;
 
-        (void) printf("-----------------");
+        if (lwpid > 0) {
+                (void) snprintf(num, sizeof (num), "%d", (int)lwpid);
+                (void) strlcat(buf, "thread# ", sizeof (buf));
+                (void) strlcat(buf, num, sizeof (buf));
+        }
 
-        if (threadid && lwpid)
-                (void) printf("  lwp# %d / thread# %d  ",
-                    (int)lwpid, (int)threadid);
-        else if (threadid)
-                (void) printf("---------  thread# %d  ", (int)threadid);
-        else if (lwpid)
-                (void) printf("  lwp# %d  ------------", (int)lwpid);
+        if (threadid > 0) {
+                (void) snprintf(num, sizeof (num), "%d", (int)threadid);
+                if (lwpid > 0)
+                        (void) strlcat(buf, " / ", sizeof (buf));
+                (void) strlcat(buf, "lwp# ", sizeof (buf));
+                (void) strlcat(buf, num, sizeof (buf));
+        }
 
-        (void) printf("--------------------\n");
+        if (name != NULL && strlen(name) > 0) {
+                (void) strlcat(buf, " [", sizeof (buf));
+                (void) strlcat(buf, name, sizeof (buf));
+                (void) strlcat(buf, "]", sizeof (buf));
+        }
+
+        amt = (HEAD_WIDTH - strlen(buf) - 2);
+        if (amt < 4)
+                amt = 4;
+
+        for (i = 0; i < amt / 2; i++)
+                (void) putc('-', stdout);
+        (void) printf(" %s ", buf);
+        for (i = 0; i < (amt / 2) + (amt % 2); i++)
+                (void) putc('-', stdout);
+        (void) putc('\n', stdout);
 }
 
 /*ARGSUSED*/
 static int
 print_java_frame(void *cld, prgregset_t gregs, const char *name, int bci,