20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 /*
  27  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
  28  */
  29 
  30 #include <fcntl.h>
  31 #include <libproc.h>
  32 #include <limits.h>
  33 #include <stdio.h>
  34 #include <strings.h>
  35 #include <sys/mkdev.h>
  36 #include <sys/stat.h>
  37 #include <sys/types.h>
  38 
  39 #include "pmap_common.h"
  40 
  41 /*
  42  * We compare the high memory addresses since stacks are faulted in from
  43  * high memory addresses to low memory addresses, and our prmap_t
  44  * structures identify only the range of addresses that have been faulted
  45  * in so far.
  46  */
  47 int
  48 cmpstacks(const void *ap, const void *bp)
  49 {
  50         const lwpstack_t *as = ap;
  51         const lwpstack_t *bs = bp;
  52         uintptr_t a = (uintptr_t)as->lwps_stack.ss_sp + as->lwps_stack.ss_size;
  53         uintptr_t b = (uintptr_t)bs->lwps_stack.ss_sp + bs->lwps_stack.ss_size;
  54 
  55         if (a < b)
  56                 return (1);
  57         if (a > b)
  58                 return (-1);
  59         return (0);
 
  71         char                    path[PATH_MAX];
  72         int                     len;
  73 
  74         if (lflag || Pstate(Pr) == PS_DEAD) {
  75                 if (Pobjname(Pr, addr, buf, bufsz) != NULL)
  76                         return (buf);
  77         } else {
  78                 if (Pobjname_resolved(Pr, addr, buf, bufsz) != NULL) {
  79                         /* Verify that the path exists */
  80                         if ((len = resolvepath(buf, buf, bufsz)) > 0) {
  81                                 buf[len] = '\0';
  82                                 return (buf);
  83                         }
  84                 }
  85         }
  86 
  87         if (Pstate(Pr) == PS_DEAD || *mapname == '\0')
  88                 return (NULL);
  89 
  90         /* first see if we can find a path via /proc */
  91         (void) snprintf(path, sizeof (path), "/proc/%d/path/%s",
  92             (int)Psp->pr_pid, mapname);
  93         len = readlink(path, buf, bufsz - 1);
  94         if (len >= 0) {
  95                 buf[len] = '\0';
  96                 return (buf);
  97         }
  98 
  99         /* fall back to object information reported by /proc */
 100         (void) snprintf(path, sizeof (path),
 101             "/proc/%d/object/%s", (int)Psp->pr_pid, mapname);
 102         if (stat(path, &statb) == 0) {
 103                 dev_t dev = statb.st_dev;
 104                 ino_t ino = statb.st_ino;
 105                 (void) snprintf(buf, bufsz, "dev:%lu,%lu ino:%lu",
 106                     (ulong_t)major(dev), (ulong_t)minor(dev), ino);
 107                 return (buf);
 108         }
 109 
 110         return (NULL);
 111 }
 112 
 113 /*
 114  * Create label for anon mappings
 115  */
 116 char *
 117 anon_name(char *name, const pstatus_t *Psp, lwpstack_t *stacks, uint_t nstacks,
 118     uintptr_t vaddr, size_t size, int mflags, int shmid, int *mtypesp)
 119 {
 120         int mtypes = 0;
 
 | 
 
 
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 /*
  27  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
  28  */
  29 
  30 #include <fcntl.h>
  31 #include <libproc.h>
  32 #include <limits.h>
  33 #include <stdio.h>
  34 #include <strings.h>
  35 #include <sys/mkdev.h>
  36 #include <sys/stat.h>
  37 #include <sys/types.h>
  38 
  39 #include "pmap_common.h"
  40 #include "ptools_common.h"
  41 
  42 /*
  43  * We compare the high memory addresses since stacks are faulted in from
  44  * high memory addresses to low memory addresses, and our prmap_t
  45  * structures identify only the range of addresses that have been faulted
  46  * in so far.
  47  */
  48 int
  49 cmpstacks(const void *ap, const void *bp)
  50 {
  51         const lwpstack_t *as = ap;
  52         const lwpstack_t *bs = bp;
  53         uintptr_t a = (uintptr_t)as->lwps_stack.ss_sp + as->lwps_stack.ss_size;
  54         uintptr_t b = (uintptr_t)bs->lwps_stack.ss_sp + bs->lwps_stack.ss_size;
  55 
  56         if (a < b)
  57                 return (1);
  58         if (a > b)
  59                 return (-1);
  60         return (0);
 
  72         char                    path[PATH_MAX];
  73         int                     len;
  74 
  75         if (lflag || Pstate(Pr) == PS_DEAD) {
  76                 if (Pobjname(Pr, addr, buf, bufsz) != NULL)
  77                         return (buf);
  78         } else {
  79                 if (Pobjname_resolved(Pr, addr, buf, bufsz) != NULL) {
  80                         /* Verify that the path exists */
  81                         if ((len = resolvepath(buf, buf, bufsz)) > 0) {
  82                                 buf[len] = '\0';
  83                                 return (buf);
  84                         }
  85                 }
  86         }
  87 
  88         if (Pstate(Pr) == PS_DEAD || *mapname == '\0')
  89                 return (NULL);
  90 
  91         /* first see if we can find a path via /proc */
  92         (void) proc_snprintf(path, sizeof (path), "/proc/%d/path/%s",
  93             (int)Psp->pr_pid, mapname);
  94         len = readlink(path, buf, bufsz - 1);
  95         if (len >= 0) {
  96                 buf[len] = '\0';
  97                 return (buf);
  98         }
  99 
 100         /* fall back to object information reported by /proc */
 101         (void) proc_snprintf(path, sizeof (path),
 102             "/proc/%d/object/%s", (int)Psp->pr_pid, mapname);
 103         if (stat(path, &statb) == 0) {
 104                 dev_t dev = statb.st_dev;
 105                 ino_t ino = statb.st_ino;
 106                 (void) snprintf(buf, bufsz, "dev:%lu,%lu ino:%lu",
 107                     (ulong_t)major(dev), (ulong_t)minor(dev), ino);
 108                 return (buf);
 109         }
 110 
 111         return (NULL);
 112 }
 113 
 114 /*
 115  * Create label for anon mappings
 116  */
 117 char *
 118 anon_name(char *name, const pstatus_t *Psp, lwpstack_t *stacks, uint_t nstacks,
 119     uintptr_t vaddr, size_t size, int mflags, int shmid, int *mtypesp)
 120 {
 121         int mtypes = 0;
 
 |