2002-10-27 16:14:05 +05:30
|
|
|
/*
|
2012-03-02 17:59:36 +05:30
|
|
|
* pmap.c - print process memory mapping
|
|
|
|
* Copyright 2002 Albert Cahalan
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2002-10-27 16:14:05 +05:30
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-02 17:59:36 +05:30
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-10-27 16:14:05 +05:30
|
|
|
*/
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <ctype.h>
|
2011-06-05 21:35:26 +05:30
|
|
|
#include <errno.h>
|
2002-10-27 16:05:13 +05:30
|
|
|
#include <fcntl.h>
|
2011-06-05 21:35:26 +05:30
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-10-27 16:05:13 +05:30
|
|
|
#include <string.h>
|
2004-01-30 03:58:13 +05:30
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
2011-06-05 21:35:26 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-09-27 17:38:04 +05:30
|
|
|
#include <assert.h>
|
2004-01-30 03:58:13 +05:30
|
|
|
|
2011-10-09 05:35:14 +05:30
|
|
|
#include "c.h"
|
2012-03-23 18:02:24 +05:30
|
|
|
#include "fileutils.h"
|
2011-10-09 05:35:14 +05:30
|
|
|
#include "nls.h"
|
2011-06-05 21:35:26 +05:30
|
|
|
#include "proc/escape.h"
|
2011-10-10 00:59:26 +05:30
|
|
|
#include "xalloc.h"
|
2004-01-27 01:31:56 +05:30
|
|
|
#include "proc/readproc.h"
|
|
|
|
#include "proc/version.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
static void __attribute__ ((__noreturn__))
|
|
|
|
usage(FILE * out)
|
|
|
|
{
|
2011-10-09 05:35:14 +05:30
|
|
|
fputs(USAGE_HEADER, out);
|
2011-06-05 21:35:26 +05:30
|
|
|
fprintf(out,
|
2011-10-09 05:35:14 +05:30
|
|
|
_(" %s [options] pid [pid ...]\n"), program_invocation_short_name);
|
|
|
|
fputs(USAGE_OPTIONS, out);
|
2011-12-28 02:16:16 +05:30
|
|
|
fputs(_(" -x, --extended show details\n"
|
2012-09-27 17:38:04 +05:30
|
|
|
" -X show even more details\n"
|
|
|
|
" WARNING: format changes according to /proc/PID/smaps\n"
|
|
|
|
" -XX show everything the kernel provides\n"
|
2011-12-28 02:16:16 +05:30
|
|
|
" -d, --device show the device format\n"
|
|
|
|
" -q, --quiet do not display header and footer\n"
|
|
|
|
" -A, --range=<low>[,<high>] limit results to the given range\n"), out);
|
2011-10-09 05:35:14 +05:30
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("pmap(1)"));
|
2011-06-05 21:35:26 +05:30
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
static char mapbuf[1024];
|
|
|
|
static char cmdbuf[512];
|
|
|
|
|
2009-05-03 12:09:33 +05:30
|
|
|
static unsigned KLONG range_low;
|
|
|
|
static unsigned KLONG range_high = ~0ull;
|
|
|
|
|
2004-01-29 09:03:44 +05:30
|
|
|
static int d_option;
|
|
|
|
static int q_option;
|
2011-06-05 21:35:26 +05:30
|
|
|
static int x_option;
|
2012-09-27 17:38:04 +05:30
|
|
|
static int X_option;
|
2002-10-27 16:05:13 +05:30
|
|
|
|
2004-01-30 03:58:13 +05:30
|
|
|
static unsigned shm_minor = ~0u;
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
static void discover_shm_minor(void)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
int shmid;
|
2012-11-02 23:20:56 +05:30
|
|
|
char mapbuf_b[256];
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
if (!freopen("/proc/self/maps", "r", stdin))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* create */
|
|
|
|
shmid = shmget(IPC_PRIVATE, 42, IPC_CREAT | 0666);
|
|
|
|
if (shmid == -1)
|
|
|
|
/* failed; oh well */
|
|
|
|
return;
|
|
|
|
/* attach */
|
|
|
|
addr = shmat(shmid, NULL, SHM_RDONLY);
|
|
|
|
if (addr == (void *)-1)
|
|
|
|
goto out_destroy;
|
|
|
|
|
2012-11-02 23:20:56 +05:30
|
|
|
while (fgets(mapbuf_b, sizeof mapbuf_b, stdin)) {
|
2011-06-05 21:35:26 +05:30
|
|
|
char flags[32];
|
|
|
|
/* to clean up unprintables */
|
|
|
|
char *tmp;
|
|
|
|
unsigned KLONG start, end;
|
|
|
|
unsigned long long file_offset, inode;
|
|
|
|
unsigned dev_major, dev_minor;
|
2012-11-02 23:20:56 +05:30
|
|
|
sscanf(mapbuf_b, "%" KLF "x-%" KLF "x %31s %llx %x:%x %llu", &start,
|
2011-06-05 21:35:26 +05:30
|
|
|
&end, flags, &file_offset, &dev_major, &dev_minor,
|
|
|
|
&inode);
|
2012-11-02 23:20:56 +05:30
|
|
|
tmp = strchr(mapbuf_b, '\n');
|
2011-06-05 21:35:26 +05:30
|
|
|
if (tmp)
|
|
|
|
*tmp = '\0';
|
2012-11-02 23:20:56 +05:30
|
|
|
tmp = mapbuf_b;
|
2011-06-05 21:35:26 +05:30
|
|
|
while (*tmp) {
|
|
|
|
if (!isprint(*tmp))
|
|
|
|
*tmp = '?';
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
if (start > (unsigned long)addr)
|
|
|
|
continue;
|
|
|
|
if (dev_major)
|
|
|
|
continue;
|
|
|
|
if (flags[3] != 's')
|
|
|
|
continue;
|
2012-11-02 23:20:56 +05:30
|
|
|
if (strstr(mapbuf_b, "/SYSV")) {
|
2011-06-05 21:35:26 +05:30
|
|
|
shm_minor = dev_minor;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shmdt(addr))
|
2011-12-17 23:02:47 +05:30
|
|
|
perror(_("shared memory detach"));
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
out_destroy:
|
|
|
|
if (shmctl(shmid, IPC_RMID, NULL))
|
2011-12-17 23:02:47 +05:30
|
|
|
perror(_("shared memory remove"));
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
return;
|
2002-10-27 16:05:13 +05:30
|
|
|
}
|
|
|
|
|
2012-02-06 02:13:36 +05:30
|
|
|
static char *mapping_name(proc_t * p, unsigned KLONG addr,
|
2012-11-02 23:20:56 +05:30
|
|
|
unsigned KLONG len, const char *mapbuf_b,
|
2011-06-05 21:35:26 +05:30
|
|
|
unsigned showpath, unsigned dev_major,
|
|
|
|
unsigned dev_minor, unsigned long long inode)
|
|
|
|
{
|
2012-02-06 02:13:36 +05:30
|
|
|
char *cp;
|
2011-06-05 21:35:26 +05:30
|
|
|
|
2012-11-02 23:20:56 +05:30
|
|
|
if (!dev_major && dev_minor == shm_minor && strstr(mapbuf_b, "/SYSV")) {
|
2011-06-05 21:35:26 +05:30
|
|
|
static char shmbuf[64];
|
2012-02-26 04:30:11 +05:30
|
|
|
snprintf(shmbuf, sizeof shmbuf, " [ shmid=0x%llx ]", inode);
|
2011-06-05 21:35:26 +05:30
|
|
|
return shmbuf;
|
|
|
|
}
|
|
|
|
|
2012-11-02 23:20:56 +05:30
|
|
|
cp = strrchr(mapbuf_b, '/');
|
2011-06-05 21:35:26 +05:30
|
|
|
if (cp) {
|
|
|
|
if (showpath)
|
2012-11-02 23:20:56 +05:30
|
|
|
return strchr(mapbuf_b, '/');
|
2011-06-05 21:35:26 +05:30
|
|
|
return cp[1] ? cp + 1 : cp;
|
|
|
|
}
|
|
|
|
|
2012-11-02 23:20:56 +05:30
|
|
|
cp = strchr(mapbuf_b, '/');
|
2011-06-05 21:35:26 +05:30
|
|
|
if (cp) {
|
|
|
|
if (showpath)
|
|
|
|
return cp;
|
|
|
|
/* it WILL succeed */
|
|
|
|
return strrchr(cp, '/') + 1;
|
|
|
|
}
|
|
|
|
|
2011-10-09 05:35:14 +05:30
|
|
|
cp = _(" [ anon ]");
|
2011-06-05 21:35:26 +05:30
|
|
|
if ((p->start_stack >= addr) && (p->start_stack <= addr + len))
|
2011-10-09 05:35:14 +05:30
|
|
|
cp = _(" [ stack ]");
|
2011-06-05 21:35:26 +05:30
|
|
|
return cp;
|
2004-01-27 01:31:56 +05:30
|
|
|
}
|
2002-10-27 16:05:13 +05:30
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
|
|
|
|
#define DETAIL_LENGTH 32
|
|
|
|
#define DETL "31" /* for format strings */
|
|
|
|
#define NUM_LENGTH 21 /* python says: len(str(2**64)) == 20 */
|
|
|
|
#define NUML "20" /* for format strings */
|
|
|
|
|
|
|
|
struct listnode {
|
|
|
|
char description[DETAIL_LENGTH];
|
|
|
|
char value_str[NUM_LENGTH];
|
|
|
|
unsigned KLONG value;
|
|
|
|
unsigned KLONG total;
|
|
|
|
int max_width;
|
|
|
|
struct listnode *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct listnode *listhead=NULL, *listtail=NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static int is_unimportant (char *s)
|
|
|
|
{
|
|
|
|
if (strcmp(s, "AnonHugePages") == 0) return 1;
|
|
|
|
if (strcmp(s, "KernelPageSize") == 0) return 1;
|
|
|
|
if (strcmp(s, "MMUPageSize") == 0) return 1;
|
|
|
|
if (strcmp(s, "Shared_Dirty") == 0) return 1;
|
|
|
|
if (strcmp(s, "Private_Dirty") == 0) return 1;
|
|
|
|
if (strcmp(s, "Shared_Clean") == 0) return 1;
|
|
|
|
if (strcmp(s, "Private_Clean") == 0) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_extended_maps (FILE *f)
|
|
|
|
{
|
|
|
|
struct listnode *listnode;
|
|
|
|
char flags[DETAIL_LENGTH], map_desc[128],
|
|
|
|
detail_desc[DETAIL_LENGTH], value_str[NUM_LENGTH],
|
|
|
|
start[NUM_LENGTH], end[NUM_LENGTH],
|
|
|
|
offset[NUM_LENGTH], inode[NUM_LENGTH],
|
|
|
|
dev[64], fmt_str[64];
|
|
|
|
int maxw1=0, maxw2=0, maxw3=0, maxw4=0, maxw5=0;
|
|
|
|
int nfields, firstmapping, footer_gap, i;
|
|
|
|
unsigned KLONG value;
|
|
|
|
char *ret;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
|
|
|
firstmapping = 1;
|
|
|
|
while (ret != NULL) {
|
|
|
|
/* === READ MAPPING === */
|
|
|
|
map_desc[0] = '\0';
|
|
|
|
nfields = sscanf(mapbuf,
|
|
|
|
"%"NUML"[0-9a-f]-%"NUML"[0-9a-f] "
|
|
|
|
"%"DETL"s %"NUML"[0-9a-f] "
|
|
|
|
"%63[0-9a-f:] %"NUML"s %127[^\n]%c",
|
|
|
|
start, end, flags, offset,
|
|
|
|
dev, inode, map_desc, &c);
|
|
|
|
/* Must read at least up to inode, else something has changed! */
|
|
|
|
if (nfields < 6)
|
|
|
|
xerrx(EXIT_FAILURE, _("Unknown format in smaps file!"));
|
|
|
|
/* If line too long we dump everything else. */
|
|
|
|
while (c != '\n') {
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
|
|
|
c = mapbuf[strlen(mapbuf) - 1];
|
|
|
|
}
|
|
|
|
/* Store maximum widths for printing nice later */
|
|
|
|
if (strlen(start) > maxw1) maxw1 = strlen(start);
|
|
|
|
if (strlen(flags) > maxw2) maxw2 = strlen(flags);
|
|
|
|
if (strlen(offset) > maxw3) maxw3 = strlen(offset);
|
|
|
|
if (strlen(dev) > maxw4) maxw4 = strlen(dev);
|
|
|
|
if (strlen(inode) > maxw5) maxw5 = strlen(inode);
|
|
|
|
|
|
|
|
if (7 > maxw5) maxw5 = 7;
|
|
|
|
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
|
|
|
nfields = sscanf(mapbuf, "%"DETL"[^:]: %"NUML"[0-9] kB %c",
|
|
|
|
detail_desc, value_str, &c);
|
|
|
|
listnode = listhead;
|
|
|
|
/* === READ MAPPING DETAILS === */
|
|
|
|
while (ret != NULL && nfields == 2) {
|
|
|
|
|
|
|
|
if (X_option < 2 && is_unimportant(detail_desc))
|
|
|
|
goto loop_end;
|
|
|
|
/* === CREATE LIST AND FILL description FIELD === */
|
|
|
|
if (listnode == NULL) {
|
|
|
|
assert(firstmapping == 1);
|
|
|
|
listnode = calloc(1, sizeof *listnode);
|
|
|
|
if (listhead == NULL) {
|
|
|
|
assert(listtail == NULL);
|
|
|
|
listhead = listnode;
|
|
|
|
} else {
|
|
|
|
listtail->next = listnode;
|
|
|
|
}
|
|
|
|
listtail = listnode;
|
|
|
|
/* listnode was calloc()ed so all fields are already NULL! */
|
|
|
|
strcpy(listnode->description, detail_desc);
|
|
|
|
if (strlen(detail_desc) > 7)
|
|
|
|
listnode->max_width = strlen(detail_desc);
|
|
|
|
else
|
|
|
|
listnode->max_width = 7;
|
|
|
|
} else {
|
|
|
|
/* === LIST EXISTS === */
|
|
|
|
if ((listnode == NULL) ||
|
|
|
|
(strcmp(listnode->description, detail_desc) != 0))
|
|
|
|
xerrx(EXIT_FAILURE, "ERROR: %s %s",
|
|
|
|
_("inconsistent detail field in smaps file, line:\n"),
|
|
|
|
mapbuf);
|
|
|
|
}
|
|
|
|
strcpy(listnode->value_str, value_str);
|
|
|
|
sscanf(value_str, "%"KLF"u", &listnode->value);
|
|
|
|
listnode->total += listnode->value;
|
|
|
|
if (strlen(value_str) > listnode->max_width)
|
|
|
|
listnode->max_width = strlen(value_str);
|
|
|
|
listnode = listnode->next;
|
|
|
|
loop_end:
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
|
|
|
nfields = sscanf(mapbuf, "%"DETL"[^:]: %"NUML"[0-9] kB %c",
|
|
|
|
detail_desc, value_str, &c);
|
|
|
|
}
|
|
|
|
/* === PRINT THIS MAPPING === */
|
|
|
|
/* Print header */
|
|
|
|
if (firstmapping && !q_option) {
|
|
|
|
if (strlen("Address") > maxw1) maxw1 = strlen("Address");
|
|
|
|
if (strlen("Flags") > maxw2) maxw2 = strlen("Flags");
|
|
|
|
if (strlen("Offset") > maxw3) maxw3 = strlen("Offset");
|
|
|
|
if (strlen("Device") > maxw4) maxw4 = strlen("Device");
|
|
|
|
if (strlen("Inode") > maxw5) maxw5 = strlen("Inode");
|
|
|
|
sprintf(fmt_str, "%%%ds %%%ds %%%ds %%%ds %%%ds",
|
|
|
|
maxw1, maxw2, maxw3, maxw4, maxw5);
|
|
|
|
printf(fmt_str, "Address", "Flags", "Offset", "Device", "Inode");
|
|
|
|
|
|
|
|
for (listnode=listhead; listnode=listnode->next;
|
|
|
|
listnode!=NULL) {
|
|
|
|
sprintf(fmt_str, " %%%ds", listnode->max_width);
|
|
|
|
printf(fmt_str, listnode->description);
|
|
|
|
}
|
|
|
|
printf(" %s\n", "Description");
|
|
|
|
}
|
|
|
|
/* Print data */
|
|
|
|
sprintf(fmt_str, "%%%ds %%%ds %%%ds %%%ds %%%ds",
|
|
|
|
maxw1, maxw2, maxw3, maxw4, maxw5);
|
|
|
|
printf(fmt_str, start, flags, offset, dev, inode);
|
|
|
|
|
|
|
|
for (listnode=listhead; listnode=listnode->next;
|
|
|
|
listnode!=NULL) {
|
|
|
|
sprintf(fmt_str, " %%%ds", listnode->max_width);
|
|
|
|
printf(fmt_str, listnode->value_str);
|
|
|
|
}
|
|
|
|
printf(" %s\n", map_desc);
|
|
|
|
|
|
|
|
firstmapping = 0;
|
|
|
|
}
|
|
|
|
/* === PRINT TOTALS === */
|
2012-12-22 11:30:00 +05:30
|
|
|
if (!q_option && listhead!=NULL) {
|
2012-09-27 17:38:04 +05:30
|
|
|
footer_gap = maxw1+maxw2+maxw3+maxw4+maxw5+5;
|
|
|
|
for (i=0; i<footer_gap; i++)
|
|
|
|
putc(' ', stdout);
|
|
|
|
for (listnode=listhead; listnode=listnode->next;
|
|
|
|
listnode!=NULL) {
|
|
|
|
for (i=0; i<listnode->max_width; i++)
|
|
|
|
putc('=', stdout);
|
|
|
|
putc(' ', stdout);
|
|
|
|
}
|
|
|
|
putc('\n', stdout);
|
|
|
|
for (i=0; i<footer_gap; i++)
|
|
|
|
putc(' ', stdout);
|
|
|
|
for (listnode=listhead; listnode=listnode->next;
|
|
|
|
listnode!=NULL) {
|
|
|
|
sprintf(fmt_str, "%%%dd ", listnode->max_width);
|
|
|
|
printf(fmt_str, listnode->total);
|
|
|
|
}
|
|
|
|
fputs("KB \n", stdout);
|
|
|
|
}
|
|
|
|
/* We don't free() the list, it's used for all PIDs passed as arguments */
|
|
|
|
}
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
static int one_proc(proc_t * p)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
FILE *fp;
|
|
|
|
unsigned long total_shared = 0ul;
|
|
|
|
unsigned long total_private_readonly = 0ul;
|
|
|
|
unsigned long total_private_writeable = 0ul;
|
2011-12-26 03:41:27 +05:30
|
|
|
KLONG diff = 0;
|
2011-06-05 21:35:26 +05:30
|
|
|
|
2012-01-24 23:26:33 +05:30
|
|
|
const char *cp2 = NULL;
|
2011-06-05 21:35:26 +05:30
|
|
|
unsigned long long rss = 0ull;
|
|
|
|
unsigned long long private_dirty = 0ull;
|
|
|
|
unsigned long long shared_dirty = 0ull;
|
|
|
|
unsigned long long total_rss = 0ull;
|
|
|
|
unsigned long long total_private_dirty = 0ull;
|
|
|
|
unsigned long long total_shared_dirty = 0ull;
|
|
|
|
|
|
|
|
/* Overkill, but who knows what is proper? The "w" prog uses
|
|
|
|
* the tty width to determine this.
|
|
|
|
*/
|
|
|
|
int maxcmd = 0xfffff;
|
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
if (x_option || X_option) {
|
2011-06-05 21:35:26 +05:30
|
|
|
sprintf(buf, "/proc/%u/smaps", p->tgid);
|
2012-09-27 17:38:04 +05:30
|
|
|
if ((fp = fopen(buf, "r")) == NULL)
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
sprintf(buf, "/proc/%u/maps", p->tgid);
|
|
|
|
if ((fp = fopen(buf, "r")) == NULL)
|
2011-06-05 21:35:26 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
escape_command(cmdbuf, p, sizeof cmdbuf, &maxcmd,
|
|
|
|
ESC_ARGS | ESC_BRACKETS);
|
|
|
|
printf("%u: %s\n", p->tgid, cmdbuf);
|
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
if (X_option) {
|
|
|
|
print_extended_maps(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
if (!q_option && (x_option | d_option)) {
|
|
|
|
if (x_option) {
|
|
|
|
if (sizeof(KLONG) == 4)
|
2011-12-17 23:02:47 +05:30
|
|
|
/* Translation Hint: Please keep
|
|
|
|
* alignment of the following four
|
|
|
|
* headers intact. */
|
2011-06-05 21:35:26 +05:30
|
|
|
printf
|
2011-10-09 05:35:14 +05:30
|
|
|
(_("Address Kbytes RSS Dirty Mode Mapping\n"));
|
2011-06-05 21:35:26 +05:30
|
|
|
else
|
|
|
|
printf
|
2011-10-09 05:35:14 +05:30
|
|
|
(_("Address Kbytes RSS Dirty Mode Mapping\n"));
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
|
|
|
if (d_option) {
|
|
|
|
if (sizeof(KLONG) == 4)
|
|
|
|
printf
|
2011-10-09 05:35:14 +05:30
|
|
|
(_("Address Kbytes Mode Offset Device Mapping\n"));
|
2011-06-05 21:35:26 +05:30
|
|
|
else
|
|
|
|
printf
|
2011-10-09 05:35:14 +05:30
|
|
|
(_("Address Kbytes Mode Offset Device Mapping\n"));
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(mapbuf, sizeof mapbuf, fp)) {
|
|
|
|
char flags[32];
|
|
|
|
/* to clean up unprintables */
|
|
|
|
char *tmp;
|
2011-12-26 03:41:27 +05:30
|
|
|
unsigned KLONG start, end;
|
2011-06-05 21:35:26 +05:30
|
|
|
unsigned long long file_offset, inode;
|
|
|
|
unsigned dev_major, dev_minor;
|
|
|
|
unsigned long long smap_value;
|
|
|
|
char smap_key[20];
|
|
|
|
|
|
|
|
/* hex values are lower case or numeric, keys are upper */
|
|
|
|
if (mapbuf[0] >= 'A' && mapbuf[0] <= 'Z') {
|
|
|
|
/* Its a key */
|
|
|
|
if (sscanf
|
|
|
|
(mapbuf, "%20[^:]: %llu", smap_key,
|
|
|
|
&smap_value) == 2) {
|
|
|
|
if (strncmp("Rss", smap_key, 3) == 0) {
|
|
|
|
rss = smap_value;
|
|
|
|
total_rss += smap_value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp("Shared_Dirty", smap_key, 12) == 0) {
|
|
|
|
shared_dirty = smap_value;
|
|
|
|
total_shared_dirty += smap_value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp("Private_Dirty", smap_key, 13) == 0) {
|
|
|
|
private_dirty = smap_value;
|
|
|
|
total_private_dirty += smap_value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp("Swap", smap_key, 4) == 0) {
|
|
|
|
/*doesnt matter as long as last */
|
|
|
|
printf((sizeof(KLONG) == 8)
|
|
|
|
? "%016" KLF
|
|
|
|
"x %7lu %7llu %7llu %s %s\n" :
|
|
|
|
"%08lx %7lu %7llu %7llu %s %s\n",
|
|
|
|
start,
|
|
|
|
(unsigned long)(diff >> 10), rss,
|
|
|
|
(private_dirty + shared_dirty),
|
|
|
|
flags, cp2);
|
|
|
|
/* reset some counters */
|
|
|
|
rss = shared_dirty = private_dirty =
|
|
|
|
0ull;
|
2011-12-26 03:41:27 +05:30
|
|
|
diff = 0;
|
2011-06-05 21:35:26 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Other keys */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-02-26 04:30:11 +05:30
|
|
|
sscanf(mapbuf, "%" KLF "x-%" KLF "x %31s %llx %x:%x %llu", &start,
|
2011-06-05 21:35:26 +05:30
|
|
|
&end, flags, &file_offset, &dev_major, &dev_minor,
|
|
|
|
&inode);
|
|
|
|
|
2012-03-03 22:21:51 +05:30
|
|
|
if (end - 1 < range_low)
|
2011-06-05 21:35:26 +05:30
|
|
|
continue;
|
2012-03-03 22:21:51 +05:30
|
|
|
if (range_high < start)
|
|
|
|
break;
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
tmp = strchr(mapbuf, '\n');
|
|
|
|
if (tmp)
|
|
|
|
*tmp = '\0';
|
|
|
|
tmp = mapbuf;
|
|
|
|
while (*tmp) {
|
|
|
|
if (!isprint(*tmp))
|
|
|
|
*tmp = '?';
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = end - start;
|
|
|
|
if (flags[3] == 's')
|
|
|
|
total_shared += diff;
|
|
|
|
if (flags[3] == 'p') {
|
|
|
|
flags[3] = '-';
|
|
|
|
if (flags[1] == 'w')
|
|
|
|
total_private_writeable += diff;
|
|
|
|
else
|
|
|
|
total_private_readonly += diff;
|
|
|
|
}
|
|
|
|
/* format used by Solaris 9 and procps-3.2.0+ an 'R'
|
|
|
|
* if swap not reserved (MAP_NORESERVE, SysV ISM
|
|
|
|
* shared mem, etc.)
|
|
|
|
*/
|
|
|
|
flags[4] = '-';
|
|
|
|
flags[5] = '\0';
|
|
|
|
|
|
|
|
if (x_option) {
|
|
|
|
cp2 =
|
|
|
|
mapping_name(p, start, diff, mapbuf, 0, dev_major,
|
|
|
|
dev_minor, inode);
|
|
|
|
/* printed with the keys */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (d_option) {
|
|
|
|
const char *cp =
|
|
|
|
mapping_name(p, start, diff, mapbuf, 0, dev_major,
|
|
|
|
dev_minor, inode);
|
|
|
|
printf((sizeof(KLONG) == 8)
|
2012-02-26 04:30:11 +05:30
|
|
|
? "%016" KLF "x %7lu %s %016llx %03x:%05x %s\n"
|
|
|
|
: "%08lx %7lu %s %016llx %03x:%05x %s\n",
|
2011-06-05 21:35:26 +05:30
|
|
|
start,
|
|
|
|
(unsigned long)(diff >> 10),
|
|
|
|
flags, file_offset, dev_major, dev_minor, cp);
|
|
|
|
}
|
|
|
|
if (!x_option && !d_option) {
|
|
|
|
const char *cp =
|
|
|
|
mapping_name(p, start, diff, mapbuf, 1, dev_major,
|
|
|
|
dev_minor, inode);
|
|
|
|
printf((sizeof(KLONG) == 8)
|
|
|
|
? "%016" KLF "x %6luK %s %s\n"
|
|
|
|
: "%08lx %6luK %s %s\n",
|
|
|
|
start, (unsigned long)(diff >> 10), flags, cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-11-02 23:20:51 +05:30
|
|
|
fclose(fp);
|
2011-06-05 21:35:26 +05:30
|
|
|
if (!q_option) {
|
|
|
|
if (x_option) {
|
|
|
|
if (sizeof(KLONG) == 8) {
|
|
|
|
printf
|
|
|
|
("---------------- ------ ------ ------\n");
|
2011-10-09 05:35:14 +05:30
|
|
|
printf(_("total kB %15ld %7llu %7llu\n"),
|
2011-06-05 21:35:26 +05:30
|
|
|
(total_shared + total_private_writeable +
|
|
|
|
total_private_readonly) >> 10,
|
|
|
|
total_rss,
|
|
|
|
(total_shared_dirty +
|
|
|
|
total_private_dirty)
|
|
|
|
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
printf
|
|
|
|
("-------- ------- ------- ------- -------\n");
|
|
|
|
printf
|
2011-10-09 05:35:14 +05:30
|
|
|
(_("total kB %7ld - - -\n"),
|
2011-06-05 21:35:26 +05:30
|
|
|
(total_shared + total_private_writeable +
|
|
|
|
total_private_readonly) >> 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d_option) {
|
|
|
|
printf
|
2011-10-09 05:35:14 +05:30
|
|
|
(_("mapped: %ldK writeable/private: %ldK shared: %ldK\n"),
|
2011-06-05 21:35:26 +05:30
|
|
|
(total_shared + total_private_writeable +
|
|
|
|
total_private_readonly) >> 10,
|
|
|
|
total_private_writeable >> 10, total_shared >> 10);
|
|
|
|
}
|
|
|
|
if (!x_option && !d_option) {
|
|
|
|
if (sizeof(KLONG) == 8)
|
2011-12-17 23:02:47 +05:30
|
|
|
/* Translation Hint: keep total string length
|
|
|
|
* as 24 characters. Adjust %16 if needed*/
|
2011-10-09 05:35:14 +05:30
|
|
|
printf(_(" total %16ldK\n"),
|
2011-06-05 21:35:26 +05:30
|
|
|
(total_shared + total_private_writeable +
|
|
|
|
total_private_readonly) >> 10);
|
|
|
|
else
|
2011-12-17 23:02:47 +05:30
|
|
|
/* Translation Hint: keep total string length
|
|
|
|
* as 16 characters. Adjust %8 if needed*/
|
2011-10-09 05:35:14 +05:30
|
|
|
printf(_(" total %8ldK\n"),
|
2011-06-05 21:35:26 +05:30
|
|
|
(total_shared + total_private_writeable +
|
|
|
|
total_private_readonly) >> 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-03-03 21:41:45 +05:30
|
|
|
static void range_arguments(char *optarg)
|
|
|
|
{
|
|
|
|
char *arg1;
|
|
|
|
char *arg2;
|
|
|
|
|
|
|
|
arg1 = xstrdup(optarg);
|
|
|
|
arg2 = strchr(arg1, ',');
|
|
|
|
if (arg2)
|
|
|
|
*arg2 = '\0';
|
|
|
|
if (arg2)
|
|
|
|
++arg2;
|
|
|
|
else
|
|
|
|
arg2 = arg1;
|
|
|
|
if (arg1 && *arg1)
|
|
|
|
range_low = STRTOUKL(arg1, &arg1, 16);
|
|
|
|
if (*arg2)
|
|
|
|
range_high = STRTOUKL(arg2, &arg2, 16);
|
|
|
|
if (arg1 && (*arg1 || *arg2))
|
|
|
|
xerrx(EXIT_FAILURE, "%s: '%s'", _("failed to parse argument"),
|
|
|
|
optarg);
|
|
|
|
}
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-11-02 23:20:51 +05:30
|
|
|
unsigned long *pidlist;
|
2011-06-05 21:35:26 +05:30
|
|
|
unsigned count = 0;
|
|
|
|
PROCTAB *PT;
|
|
|
|
proc_t p;
|
|
|
|
int ret = 0, c;
|
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"extended", no_argument, NULL, 'x'},
|
|
|
|
{"device", no_argument, NULL, 'd'},
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"range", required_argument, NULL, 'A'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2012-01-03 13:18:43 +05:30
|
|
|
program_invocation_name = program_invocation_short_name;
|
2011-12-07 17:57:21 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-03-23 18:02:24 +05:30
|
|
|
atexit(close_stdout);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
x_option = d_option = q_option = 0;
|
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
while ((c = getopt_long(argc, argv, "xXrdqA:hV", longopts, NULL)) != -1)
|
2011-06-05 21:35:26 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'x':
|
|
|
|
x_option = 1;
|
|
|
|
break;
|
2012-09-27 17:38:04 +05:30
|
|
|
case 'X':
|
|
|
|
X_option++;
|
|
|
|
break;
|
2011-06-05 21:35:26 +05:30
|
|
|
case 'r':
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("option -r is ignored as SunOS compatibility"));
|
2011-06-05 21:35:26 +05:30
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
d_option = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
q_option = 1;
|
|
|
|
break;
|
|
|
|
case 'A':
|
2012-03-03 21:41:45 +05:30
|
|
|
range_arguments(optarg);
|
2011-06-05 21:35:26 +05:30
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
case 'V':
|
2011-10-09 05:35:14 +05:30
|
|
|
printf(PROCPS_NG_VERSION);
|
2011-06-05 21:35:26 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'a': /* Sun prints anon/swap reservations */
|
|
|
|
case 'F': /* Sun forces hostile ptrace-like grab */
|
|
|
|
case 'l': /* Sun shows unresolved dynamic names */
|
|
|
|
case 'L': /* Sun shows lgroup info */
|
|
|
|
case 's': /* Sun shows page sizes */
|
|
|
|
case 'S': /* Sun shows swap reservations */
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("argument missing"));
|
2012-09-27 17:38:04 +05:30
|
|
|
if (d_option && (x_option || X_option) ||
|
|
|
|
x_option && (d_option || X_option))
|
|
|
|
xerrx(EXIT_FAILURE, _("options -d, -x, -X are mutually exclusive"));
|
2011-06-05 21:35:26 +05:30
|
|
|
|
2012-11-02 23:20:50 +05:30
|
|
|
pidlist = xmalloc(sizeof(unsigned long) * argc);
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
while (*argv) {
|
|
|
|
char *walk = *argv++;
|
|
|
|
char *endp;
|
|
|
|
unsigned long pid;
|
|
|
|
if (!strncmp("/proc/", walk, 6)) {
|
|
|
|
walk += 6;
|
|
|
|
/* user allowed to do: pmap /proc/PID */
|
|
|
|
if (*walk < '0' || *walk > '9')
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*walk < '0' || *walk > '9')
|
|
|
|
usage(stderr);
|
|
|
|
pid = strtoul(walk, &endp, 0);
|
|
|
|
if (pid < 1ul || pid > 0x7ffffffful || *endp)
|
|
|
|
usage(stderr);
|
|
|
|
pidlist[count++] = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
discover_shm_minor();
|
|
|
|
|
|
|
|
memset(&p, '\0', sizeof(p));
|
|
|
|
/* old libproc interface is zero-terminated */
|
|
|
|
pidlist[count] = 0;
|
|
|
|
PT = openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, pidlist);
|
|
|
|
while (readproc(PT, &p)) {
|
|
|
|
ret |= one_proc(&p);
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
closeproc(PT);
|
2012-11-02 23:20:51 +05:30
|
|
|
free(pidlist);
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
if (count)
|
|
|
|
/* didn't find all processes asked for */
|
|
|
|
ret |= 42;
|
|
|
|
return ret;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|