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
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
const char *nls_Address,
|
|
|
|
*nls_Offset,
|
|
|
|
*nls_Device,
|
|
|
|
*nls_Mapping,
|
2013-03-16 10:30:00 +05:30
|
|
|
*nls_Perm,
|
2013-03-16 10:30:00 +05:30
|
|
|
*nls_Inode,
|
|
|
|
*nls_Kbytes,
|
|
|
|
*nls_Mode,
|
|
|
|
*nls_RSS,
|
|
|
|
*nls_Dirty;
|
|
|
|
|
|
|
|
static void nls_initialize(void)
|
|
|
|
{
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
|
|
|
/* these are the headings shared across all options,
|
|
|
|
though their widths when output might differ */
|
|
|
|
nls_Address = _("Address");
|
|
|
|
nls_Offset = _("Offset");
|
|
|
|
nls_Device = _("Device");
|
|
|
|
nls_Mapping = _("Mapping");
|
|
|
|
|
|
|
|
/* these headings are used only by the -X/-XX options,
|
|
|
|
and are not derived literally from /proc/#/smaps */
|
2013-03-16 10:30:00 +05:30
|
|
|
nls_Perm = _("Perm");
|
2013-03-16 10:30:00 +05:30
|
|
|
nls_Inode = _("Inode");
|
|
|
|
|
|
|
|
/* these are potentially used for options other than -X/-XX, */
|
|
|
|
nls_Kbytes = _("Kbytes");
|
|
|
|
nls_Mode = _("Mode");
|
|
|
|
nls_RSS = _("RSS");
|
|
|
|
nls_Dirty = _("Dirty");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int justify_print(const char *str, int width, int right)
|
|
|
|
{
|
|
|
|
if (width < 1)
|
|
|
|
puts(str);
|
|
|
|
else {
|
|
|
|
int len = strlen(str);
|
|
|
|
if (width < len) width = len;
|
|
|
|
printf(right ? "%*.*s " : "%-*.*s ", width, width, str);
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
2013-02-07 22:26:55 +05:30
|
|
|
|
|
|
|
static int integer_width(unsigned KLONG number)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = !(number > 0);
|
|
|
|
while (number) {
|
|
|
|
result++;
|
|
|
|
number /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
static void __attribute__ ((__noreturn__))
|
2013-03-16 10:30:00 +05:30
|
|
|
usage(FILE * out)
|
2011-06-05 21:35:26 +05:30
|
|
|
{
|
2011-10-09 05:35:14 +05:30
|
|
|
fputs(USAGE_HEADER, out);
|
2011-06-05 21:35:26 +05:30
|
|
|
fprintf(out,
|
2014-02-02 12:43:01 +05:30
|
|
|
_(" %s [options] PID [PID ...]\n"), program_invocation_short_name);
|
2011-10-09 05:35:14 +05:30
|
|
|
fputs(USAGE_OPTIONS, out);
|
2013-12-28 03:55:39 +05:30
|
|
|
fputs(_(" -x, --extended show details\n"), out);
|
|
|
|
fputs(_(" -X show even more details\n"), out);
|
|
|
|
fputs(_(" WARNING: format changes according to /proc/PID/smaps\n"), out);
|
|
|
|
fputs(_(" -XX show everything the kernel provides\n"), out);
|
|
|
|
fputs(_(" -c, --read-rc read the default rc\n"), out);
|
|
|
|
fputs(_(" -C, --read-rc-from=<file> read the rc from file\n"), out);
|
|
|
|
fputs(_(" -n, --create-rc create new default rc\n"), out);
|
|
|
|
fputs(_(" -N, --create-rc-to=<file> create new rc to file\n"), out);
|
|
|
|
fputs(_(" NOTE: pid arguments are not allowed with -n, -N\n"), out);
|
|
|
|
fputs(_(" -d, --device show the device format\n"), out);
|
|
|
|
fputs(_(" -q, --quiet do not display header and footer\n"), out);
|
|
|
|
fputs(_(" -p, --show-path show path in the mapping\n"), out);
|
|
|
|
fputs(_(" -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;
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
static int c_option;
|
|
|
|
static int C_option;
|
2004-01-29 09:03:44 +05:30
|
|
|
static int d_option;
|
2013-03-18 20:35:44 +05:30
|
|
|
static int n_option;
|
|
|
|
static int N_option;
|
2004-01-29 09:03:44 +05:30
|
|
|
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
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
static int map_desc_showpath;
|
|
|
|
|
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)) {
|
2013-03-16 10:30:00 +05:30
|
|
|
char perms[32];
|
2011-06-05 21:35:26 +05:30
|
|
|
/* 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,
|
2013-03-16 10:30:00 +05:30
|
|
|
&end, perms, &file_offset, &dev_major, &dev_minor,
|
2011-06-05 21:35:26 +05:30
|
|
|
&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;
|
2013-03-16 10:30:00 +05:30
|
|
|
if (perms[3] != 's')
|
2011-06-05 21:35:26 +05:30
|
|
|
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
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
out_destroy:
|
2011-06-05 21:35:26 +05:30
|
|
|
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 */
|
2013-03-18 20:35:44 +05:30
|
|
|
#define VMFLAGS_LENGTH 81 /* There are 27 posible 2 character vmflags as of this patch */
|
2012-09-27 17:38:04 +05:30
|
|
|
|
|
|
|
struct listnode {
|
|
|
|
char description[DETAIL_LENGTH];
|
|
|
|
char value_str[NUM_LENGTH];
|
|
|
|
unsigned KLONG value;
|
|
|
|
unsigned KLONG total;
|
|
|
|
int max_width;
|
|
|
|
struct listnode *next;
|
|
|
|
};
|
|
|
|
|
2013-02-07 03:31:54 +05:30
|
|
|
static struct listnode *listhead=NULL, *listtail=NULL, *listnode;
|
2012-09-27 17:38:04 +05:30
|
|
|
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
struct cnf_listnode {
|
|
|
|
char description[DETAIL_LENGTH];
|
|
|
|
struct cnf_listnode *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cnf_listnode *cnf_listhead=NULL, *cnf_listnode;
|
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
static int is_unimportant (const char *s)
|
2012-09-27 17:38:04 +05:30
|
|
|
{
|
|
|
|
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;
|
2013-03-18 20:35:44 +05:30
|
|
|
if (strcmp(s, "VmFlags") == 0) return 1;
|
2012-09-27 17:38:04 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
/* check, whether we want to display the field or not */
|
2013-03-16 10:30:00 +05:30
|
|
|
static int is_enabled (const char *s)
|
2013-03-18 20:35:44 +05:30
|
|
|
{
|
|
|
|
if (X_option == 1) return !is_unimportant(s);
|
|
|
|
|
|
|
|
if (c_option) { /* taking the list of disabled fields from the rc file */
|
|
|
|
|
|
|
|
for (cnf_listnode = cnf_listhead; cnf_listnode; cnf_listnode = cnf_listnode -> next) {
|
|
|
|
if (!strcmp(s, cnf_listnode -> description)) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
static void print_extended_maps (FILE *f)
|
|
|
|
{
|
2013-03-16 10:30:00 +05:30
|
|
|
char perms[DETAIL_LENGTH], map_desc[128],
|
2012-09-27 17:38:04 +05:30
|
|
|
detail_desc[DETAIL_LENGTH], value_str[NUM_LENGTH],
|
|
|
|
start[NUM_LENGTH], end[NUM_LENGTH],
|
|
|
|
offset[NUM_LENGTH], inode[NUM_LENGTH],
|
2013-03-16 10:30:00 +05:30
|
|
|
dev[64], vmflags[VMFLAGS_LENGTH];
|
2013-01-23 09:16:24 +05:30
|
|
|
int maxw1=0, maxw2=0, maxw3=0, maxw4=0, maxw5=0, maxwv=0;
|
2013-03-19 00:13:12 +05:30
|
|
|
int nfields, firstmapping, footer_gap, i, maxw_;
|
2013-03-16 10:30:00 +05:30
|
|
|
char *ret, *map_basename, c, has_vmflags = 0;
|
2012-09-27 17:38:04 +05:30
|
|
|
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
2013-02-07 03:31:54 +05:30
|
|
|
firstmapping = 2;
|
2012-09-27 17:38:04 +05:30
|
|
|
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",
|
2013-03-16 10:30:00 +05:30
|
|
|
start, end, perms, offset,
|
2012-09-27 17:38:04 +05:30
|
|
|
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];
|
|
|
|
}
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
/* Store maximum widths for printing nice later */
|
2013-03-18 20:35:44 +05:30
|
|
|
if (strlen(start ) > maxw1) maxw1 = strlen(start);
|
2013-03-16 10:30:00 +05:30
|
|
|
if (strlen(perms ) > maxw2) maxw2 = strlen(perms);
|
2013-03-18 20:35:44 +05:30
|
|
|
if (strlen(offset) > maxw3) maxw3 = strlen(offset);
|
|
|
|
if (strlen(dev ) > maxw4) maxw4 = strlen(dev);
|
|
|
|
if (strlen(inode ) > maxw5) maxw5 = strlen(inode);
|
2012-09-27 17:38:04 +05:30
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
if (!is_enabled(detail_desc)) goto loop_end;
|
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
/* === CREATE LIST AND FILL description FIELD === */
|
|
|
|
if (listnode == NULL) {
|
2013-02-07 03:31:54 +05:30
|
|
|
assert(firstmapping == 2);
|
2012-09-27 17:38:04 +05:30
|
|
|
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);
|
2013-03-19 00:13:12 +05:30
|
|
|
if (!q_option) listnode->max_width = strlen(detail_desc);
|
|
|
|
else listnode->max_width = 0;
|
2012-09-27 17:38:04 +05:30
|
|
|
} else {
|
|
|
|
/* === LIST EXISTS === */
|
2013-02-07 03:31:54 +05:30
|
|
|
if (strcmp(listnode->description, detail_desc) != 0)
|
2012-09-27 17:38:04 +05:30
|
|
|
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);
|
2013-02-07 22:26:55 +05:30
|
|
|
if (firstmapping == 2) {
|
|
|
|
listnode->total += listnode->value;
|
2013-03-19 00:13:12 +05:30
|
|
|
if (q_option) {
|
|
|
|
maxw_ = strlen(listnode->value_str);
|
|
|
|
if (maxw_ > listnode->max_width)
|
|
|
|
listnode->max_width = maxw_;
|
|
|
|
}
|
2013-02-07 22:26:55 +05:30
|
|
|
}
|
2012-09-27 17:38:04 +05:30
|
|
|
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);
|
|
|
|
}
|
2013-01-23 09:16:24 +05:30
|
|
|
|
|
|
|
/* === GET VMFLAGS === */
|
|
|
|
nfields = sscanf(mapbuf, "VmFlags: %[a-z ]", vmflags);
|
|
|
|
if (nfields == 1) {
|
|
|
|
if (! has_vmflags) has_vmflags = 1;
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
|
|
|
if (strlen(vmflags) > maxwv) maxwv = strlen(vmflags);
|
|
|
|
}
|
|
|
|
|
2013-02-07 03:31:54 +05:30
|
|
|
if (firstmapping == 2) { /* width measurement stage, do not print anything yet */
|
|
|
|
if (ret == NULL) { /* once the end of file is reached ...*/
|
|
|
|
firstmapping = 1; /* ... we reset the file position to the beginning of the file */
|
|
|
|
fseek(f, 0, SEEK_SET); /* ... and repeat the process with printing enabled */
|
|
|
|
ret = fgets(mapbuf, sizeof mapbuf, f); /* this is not ideal and needs to be redesigned one day */
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-19 00:13:12 +05:30
|
|
|
if (!q_option) {
|
|
|
|
/* calculate width of totals */
|
|
|
|
for (listnode=listhead; listnode!=NULL; listnode=listnode->next) {
|
|
|
|
maxw_ = integer_width(listnode->total);
|
|
|
|
if (maxw_ > listnode->max_width)
|
|
|
|
listnode->max_width = maxw_;
|
|
|
|
}
|
2013-03-18 20:35:44 +05:30
|
|
|
}
|
|
|
|
|
2013-02-07 03:31:54 +05:30
|
|
|
}
|
|
|
|
} else { /* the maximum widths have been measured, we've already reached the printing stage */
|
|
|
|
/* === PRINT THIS MAPPING === */
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-02-07 03:31:54 +05:30
|
|
|
/* Print header */
|
|
|
|
if (firstmapping && !q_option) {
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
maxw1 = justify_print(nls_Address, maxw1, 1);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Perm))
|
|
|
|
maxw2 = justify_print(nls_Perm, maxw2, 1);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Offset))
|
|
|
|
maxw3 = justify_print(nls_Offset, maxw3, 1);
|
2013-02-07 03:31:54 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Device))
|
|
|
|
maxw4 = justify_print(nls_Device, maxw4, 1);
|
2013-03-16 10:30:00 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Inode))
|
|
|
|
maxw5 = justify_print(nls_Inode, maxw5, 1);
|
2013-03-16 10:30:00 +05:30
|
|
|
|
|
|
|
for (listnode=listhead; listnode!=NULL; listnode=listnode->next)
|
2013-03-16 10:30:00 +05:30
|
|
|
justify_print(listnode->description, listnode->max_width, 1);
|
2013-02-07 03:31:54 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (has_vmflags && is_enabled("VmFlags"))
|
|
|
|
printf(" %*s", maxwv, "VmFlags");
|
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Mapping))
|
|
|
|
justify_print(nls_Mapping, 0, 0);
|
|
|
|
else
|
|
|
|
printf("\n");
|
2013-02-07 03:31:54 +05:30
|
|
|
}
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-02-07 03:31:54 +05:30
|
|
|
/* Print data */
|
2013-03-16 10:30:00 +05:30
|
|
|
printf("%*s", maxw1, start); /* Address field is always enabled */
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Perm))
|
|
|
|
printf(" %*s", maxw2, perms);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Offset))
|
2013-03-16 10:30:00 +05:30
|
|
|
printf(" %*s", maxw3, offset);
|
2012-09-27 17:38:04 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Device))
|
2013-03-16 10:30:00 +05:30
|
|
|
printf(" %*s", maxw4, dev);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Inode))
|
2013-03-16 10:30:00 +05:30
|
|
|
printf(" %*s", maxw5, inode);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
for (listnode=listhead; listnode!=NULL; listnode=listnode->next)
|
|
|
|
printf(" %*s", listnode->max_width, listnode->value_str);
|
2013-01-23 09:16:24 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (has_vmflags && is_enabled("VmFlags"))
|
|
|
|
printf(" %*s", maxwv, vmflags);
|
2013-01-23 09:16:24 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Mapping)) {
|
2013-03-18 20:35:44 +05:30
|
|
|
if (map_desc_showpath) {
|
|
|
|
printf(" %s", map_desc);
|
|
|
|
} else {
|
|
|
|
map_basename = strrchr(map_desc, '/');
|
|
|
|
if (!map_basename) {
|
|
|
|
printf(" %s", map_desc);
|
|
|
|
} else {
|
|
|
|
printf(" %s", map_basename + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2012-09-27 17:38:04 +05:30
|
|
|
|
2013-02-07 03:31:54 +05:30
|
|
|
firstmapping = 0;
|
2013-01-23 09:16:24 +05:30
|
|
|
|
|
|
|
}
|
2012-09-27 17:38:04 +05:30
|
|
|
}
|
|
|
|
/* === PRINT TOTALS === */
|
2013-03-18 20:35:44 +05:30
|
|
|
if (!q_option && listhead!=NULL) { /* footer enabled and non-empty */
|
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
footer_gap = maxw1 + 1; /* Address field is always enabled */
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Perm )) footer_gap += maxw2 + 1;
|
2013-03-16 10:30:00 +05:30
|
|
|
if (is_enabled(nls_Offset)) footer_gap += maxw3 + 1;
|
|
|
|
if (is_enabled(nls_Device)) footer_gap += maxw4 + 1;
|
|
|
|
if (is_enabled(nls_Inode )) footer_gap += maxw5 + 1;
|
2013-03-18 20:35:44 +05:30
|
|
|
|
|
|
|
for (i=0; i<footer_gap; i++) putc(' ', stdout);
|
|
|
|
|
|
|
|
for (listnode=listhead; listnode!=NULL; listnode=listnode->next) {
|
2012-09-27 17:38:04 +05:30
|
|
|
for (i=0; i<listnode->max_width; i++)
|
|
|
|
putc('=', stdout);
|
|
|
|
putc(' ', stdout);
|
|
|
|
}
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
putc('\n', stdout);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
|
|
|
for (i=0; i<footer_gap; i++) putc(' ', stdout);
|
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
for (listnode=listhead; listnode!=NULL; listnode=listnode->next)
|
|
|
|
printf("%*lu ", listnode->max_width, listnode->total);
|
2013-03-18 20:35:44 +05:30
|
|
|
|
2012-09-27 17:38:04 +05:30
|
|
|
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;
|
2013-03-18 20:35:44 +05:30
|
|
|
KLONG diff = 0;
|
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;
|
2013-03-26 15:51:39 +05:30
|
|
|
int maxw1=0, maxw2=0, maxw3=0, maxw4=0, maxw5=0;
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
/* Overkill, but who knows what is proper? The "w" prog uses
|
|
|
|
* the tty width to determine this.
|
|
|
|
*/
|
|
|
|
int maxcmd = 0xfffff;
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
if (x_option || X_option || c_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);
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
if (X_option || c_option) {
|
2012-09-27 17:38:04 +05:30
|
|
|
print_extended_maps(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (x_option) {
|
|
|
|
maxw1 = 16;
|
|
|
|
if (sizeof(KLONG) == 4) maxw1 = 8;
|
|
|
|
maxw2 = maxw3 = maxw4 = 7;
|
|
|
|
maxw5 = 5;
|
|
|
|
if (!q_option) {
|
|
|
|
maxw1 = justify_print(nls_Address, maxw1, 0);
|
|
|
|
maxw2 = justify_print(nls_Kbytes, maxw2, 1);
|
|
|
|
maxw3 = justify_print(nls_RSS, maxw3, 1);
|
|
|
|
maxw4 = justify_print(nls_Dirty, maxw4, 1);
|
|
|
|
maxw5 = justify_print(nls_Mode, maxw5, 0);
|
|
|
|
justify_print(nls_Mapping, 0, 0);
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
2013-03-16 10:30:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (d_option) {
|
|
|
|
maxw1 = 16;
|
|
|
|
if (sizeof(KLONG) == 4) maxw1 = 8;
|
|
|
|
maxw2 = 7;
|
|
|
|
maxw3 = 5;
|
|
|
|
maxw4 = 16;
|
|
|
|
maxw5 = 9;
|
|
|
|
if (!q_option) {
|
|
|
|
maxw1 = justify_print(nls_Address, maxw1, 0);
|
|
|
|
maxw2 = justify_print(nls_Kbytes, maxw2, 1);
|
|
|
|
maxw3 = justify_print(nls_Mode, maxw3, 0);
|
|
|
|
maxw4 = justify_print(nls_Offset, maxw4, 0);
|
|
|
|
maxw5 = justify_print(nls_Device, maxw5, 0);
|
|
|
|
justify_print(nls_Mapping, 0, 0);
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(mapbuf, sizeof mapbuf, fp)) {
|
2013-03-16 10:30:00 +05:30
|
|
|
char perms[32];
|
2011-06-05 21:35:26 +05:30
|
|
|
/* 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;
|
2013-05-23 03:52:37 +05:30
|
|
|
char smap_key[21];
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
/* 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 */
|
2013-03-16 10:30:00 +05:30
|
|
|
printf("%0*" KLF "x %*lu %*llu %*llu %*s %s\n",
|
|
|
|
maxw1, start,
|
|
|
|
maxw2, (unsigned long)(diff >> 10),
|
|
|
|
maxw3, rss,
|
|
|
|
maxw4, (private_dirty + shared_dirty),
|
2013-03-16 10:30:00 +05:30
|
|
|
maxw5, perms,
|
2013-03-16 10:30:00 +05:30
|
|
|
cp2);
|
2011-06-05 21:35:26 +05:30
|
|
|
/* reset some counters */
|
2013-03-16 10:30:00 +05:30
|
|
|
rss = shared_dirty = private_dirty = 0ull;
|
|
|
|
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,
|
2013-03-16 10:30:00 +05:30
|
|
|
&end, perms, &file_offset, &dev_major, &dev_minor,
|
2011-06-05 21:35:26 +05:30
|
|
|
&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;
|
2013-03-16 10:30:00 +05:30
|
|
|
if (perms[3] == 's')
|
2011-06-05 21:35:26 +05:30
|
|
|
total_shared += diff;
|
2013-03-16 10:30:00 +05:30
|
|
|
if (perms[3] == 'p') {
|
|
|
|
perms[3] = '-';
|
|
|
|
if (perms[1] == 'w')
|
2011-06-05 21:35:26 +05:30
|
|
|
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.)
|
|
|
|
*/
|
2013-03-16 10:30:00 +05:30
|
|
|
perms[4] = '-';
|
|
|
|
perms[5] = '\0';
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
if (x_option) {
|
|
|
|
cp2 =
|
2013-03-18 20:35:44 +05:30
|
|
|
mapping_name(p, start, diff, mapbuf, map_desc_showpath, dev_major,
|
2011-06-05 21:35:26 +05:30
|
|
|
dev_minor, inode);
|
|
|
|
/* printed with the keys */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (d_option) {
|
|
|
|
const char *cp =
|
2013-03-18 20:35:44 +05:30
|
|
|
mapping_name(p, start, diff, mapbuf, map_desc_showpath, dev_major,
|
2011-06-05 21:35:26 +05:30
|
|
|
dev_minor, inode);
|
2013-03-16 10:30:00 +05:30
|
|
|
printf("%0*" KLF "x %*lu %*s %0*llx %*.*s%03x:%05x %s\n",
|
|
|
|
maxw1, start,
|
|
|
|
maxw2, (unsigned long)(diff >> 10),
|
2013-03-16 10:30:00 +05:30
|
|
|
maxw3, perms,
|
2013-03-16 10:30:00 +05:30
|
|
|
maxw4, file_offset,
|
|
|
|
(maxw5-9), (maxw5-9), " ", dev_major, dev_minor,
|
|
|
|
cp);
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
|
|
|
if (!x_option && !d_option) {
|
|
|
|
const char *cp =
|
2013-03-18 20:35:44 +05:30
|
|
|
mapping_name(p, start, diff, mapbuf, map_desc_showpath, dev_major,
|
2011-06-05 21:35:26 +05:30
|
|
|
dev_minor, inode);
|
|
|
|
printf((sizeof(KLONG) == 8)
|
2013-03-16 10:30:00 +05:30
|
|
|
? "%016" KLF "x %6luK %s %s\n"
|
|
|
|
: "%08lx %6luK %s %s\n",
|
2013-03-16 10:30:00 +05:30
|
|
|
start, (unsigned long)(diff >> 10), perms, cp);
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-03-16 10:30:00 +05:30
|
|
|
fclose(fp);
|
2011-06-05 21:35:26 +05:30
|
|
|
if (!q_option) {
|
|
|
|
if (x_option) {
|
2013-03-16 10:30:00 +05:30
|
|
|
if (sizeof(KLONG) == 4)
|
|
|
|
justify_print("--------", maxw1, 0);
|
|
|
|
else
|
|
|
|
justify_print("----------------", maxw1, 0);
|
|
|
|
justify_print("-------", maxw2, 1);
|
|
|
|
justify_print("-------", maxw3, 1);
|
|
|
|
justify_print("-------", maxw4, 1);
|
|
|
|
printf("\n");
|
2013-01-24 16:56:27 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
printf("%-*s ", maxw1, _("total kB"));
|
|
|
|
printf("%*ld %*llu %*llu\n",
|
|
|
|
maxw2, (total_shared +
|
|
|
|
total_private_writeable +
|
|
|
|
total_private_readonly) >> 10,
|
|
|
|
maxw3, total_rss,
|
|
|
|
maxw4, (total_shared_dirty +
|
|
|
|
total_private_dirty));
|
2011-06-05 21:35:26 +05:30
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
|
|
|
|
#define MAX_CNF_LINE_LEN 1024
|
|
|
|
|
|
|
|
#define SECTION_ID_NONE 0
|
|
|
|
#define SECTION_ID_UNSUPPORTED 1
|
|
|
|
|
|
|
|
#define SECTION_STR_FIELDS_DISPLAY "[Fields Display]"
|
|
|
|
#define SECTION_STR_FIELDS_DISPLAY_LEN (sizeof(SECTION_STR_FIELDS_DISPLAY) - 1)
|
|
|
|
#define SECTION_ID_FIELDS_DISPLAY 2
|
|
|
|
|
|
|
|
#define SECTION_STR_MAPPING "[Mapping]"
|
|
|
|
#define SECTION_STR_MAPPING_LEN (sizeof(SECTION_STR_MAPPING) - 1)
|
|
|
|
#define SECTION_ID_MAPPING 3
|
|
|
|
|
|
|
|
static int config_read (char *rc_filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char line_buf[MAX_CNF_LINE_LEN + 1];
|
|
|
|
char tmp_buf [MAX_CNF_LINE_LEN + 1];
|
|
|
|
char *trimmed;
|
|
|
|
int length;
|
2013-03-26 15:04:22 +05:30
|
|
|
char *tail, *token;
|
2013-03-18 20:35:44 +05:30
|
|
|
int line_cnt, section_id;
|
|
|
|
|
|
|
|
f = fopen(rc_filename, "r");
|
|
|
|
|
|
|
|
if (!f) return 0; /* can't open the file for reading */
|
|
|
|
|
|
|
|
line_cnt = 0;
|
|
|
|
section_id = SECTION_ID_NONE;
|
|
|
|
|
|
|
|
while (fgets (line_buf, MAX_CNF_LINE_LEN + 1, f)) {
|
|
|
|
|
|
|
|
line_cnt++;
|
|
|
|
|
|
|
|
/* get rid of the LF char */
|
|
|
|
length = strlen(line_buf);
|
|
|
|
if (length > 0 && line_buf[length - 1] == '\n') {
|
|
|
|
line_buf[length - 1] = '\0';
|
|
|
|
} else if (length == MAX_CNF_LINE_LEN) { /* no LF char -> line too long */
|
|
|
|
xwarnx(_("config line too long - line %d"), line_cnt);
|
|
|
|
/* ignoring the tail */
|
|
|
|
while (fgets (tmp_buf, MAX_CNF_LINE_LEN + 1, f) &&
|
|
|
|
(length = strlen(tmp_buf))>0 &&
|
|
|
|
tmp_buf[length - 1] != '\n') ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trim leading whitespaces */
|
|
|
|
trimmed = line_buf;
|
|
|
|
while (*trimmed == ' ' || *trimmed == '\t') trimmed++;
|
|
|
|
|
|
|
|
/* skip comments and empty lines */
|
|
|
|
if (*trimmed == '#' || *trimmed == '\0') continue;
|
|
|
|
|
|
|
|
if (*trimmed == '[') { /* section */
|
|
|
|
if (!strncmp(trimmed, SECTION_STR_FIELDS_DISPLAY, SECTION_STR_FIELDS_DISPLAY_LEN)) {
|
|
|
|
trimmed += SECTION_STR_FIELDS_DISPLAY_LEN;
|
|
|
|
section_id = SECTION_ID_FIELDS_DISPLAY;
|
|
|
|
} else if (!strncmp(trimmed, SECTION_STR_MAPPING, SECTION_STR_MAPPING_LEN)) {
|
|
|
|
trimmed += SECTION_STR_MAPPING_LEN;
|
|
|
|
section_id = SECTION_ID_MAPPING;
|
|
|
|
} else {
|
|
|
|
while (*trimmed != ']' || *trimmed == '\0') trimmed++;
|
|
|
|
if (*trimmed == ']') {
|
|
|
|
section_id = SECTION_ID_UNSUPPORTED;
|
|
|
|
xwarnx(_("unsupported section found in the config - line %d"), line_cnt);
|
|
|
|
trimmed++;
|
|
|
|
} else {
|
|
|
|
xwarnx(_("syntax error found in the config - line %d"), line_cnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trim trailing whitespaces */
|
|
|
|
while (*trimmed == ' ' || *trimmed == '\t') trimmed++;
|
|
|
|
|
|
|
|
/* skip comments and empty tails */
|
|
|
|
if (*trimmed == '#' || *trimmed == '\0') continue;
|
|
|
|
|
|
|
|
/* anything else found on the section line ??? */
|
|
|
|
xwarnx(_("syntax error found in the config - line %d"), line_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (section_id) {
|
|
|
|
case SECTION_ID_FIELDS_DISPLAY:
|
|
|
|
token = strtok (trimmed, " \t");
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
tail = strtok (NULL, " \t");
|
|
|
|
|
|
|
|
if (tail && *tail != '#') {
|
|
|
|
xwarnx(_("syntax error found in the config - line %d"), line_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the field in the list */
|
|
|
|
cnf_listnode = calloc(1, sizeof *cnf_listnode);
|
|
|
|
snprintf(cnf_listnode -> description, sizeof(cnf_listnode -> description), "%s", token);
|
|
|
|
cnf_listnode -> next = cnf_listhead;
|
|
|
|
cnf_listhead = cnf_listnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SECTION_ID_MAPPING:
|
|
|
|
token = strtok (trimmed, " \t");
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
tail = strtok (NULL, " \t");
|
|
|
|
|
|
|
|
if (tail && *tail != '#') {
|
|
|
|
xwarnx(_("syntax error found in the config - line %d"), line_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(token,"ShowPath")) map_desc_showpath = !map_desc_showpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SECTION_ID_UNSUPPORTED:
|
|
|
|
break; /* ignore the content */
|
|
|
|
|
|
|
|
default:
|
|
|
|
xwarnx(_("syntax error found in the config - line %d"), line_cnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int config_create (char *rc_filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* check if rc exists */
|
|
|
|
f = fopen(rc_filename, "r");
|
|
|
|
|
|
|
|
if (f) { /* file exists ... let user to delete/remove it first */
|
|
|
|
fclose(f);
|
|
|
|
xwarnx(_("the file already exists - delete or rename it first"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* file doesn't exist */
|
|
|
|
|
|
|
|
f = fopen(rc_filename, "w");
|
|
|
|
|
|
|
|
if (!f) return 0; /* can't open the file for writing */
|
|
|
|
|
|
|
|
/* current rc template, might change in the future */
|
|
|
|
fprintf(f,"# pmap's Config File\n");
|
|
|
|
fprintf(f,"\n");
|
|
|
|
fprintf(f,"# All the entries are case sensitive.\n");
|
|
|
|
fprintf(f,"# Unsupported entries are ignored!\n");
|
|
|
|
fprintf(f,"\n");
|
|
|
|
fprintf(f,"[Fields Display]\n");
|
|
|
|
fprintf(f,"\n");
|
|
|
|
fprintf(f,"# To enable a field uncomment its entry\n");
|
|
|
|
fprintf(f,"\n");
|
2013-03-16 10:30:00 +05:30
|
|
|
fprintf(f,"#%s\n", nls_Perm);
|
2013-03-16 10:30:00 +05:30
|
|
|
fprintf(f,"#%s\n", nls_Offset);
|
|
|
|
fprintf(f,"#%s\n", nls_Device);
|
|
|
|
fprintf(f,"#%s\n", nls_Inode);
|
2013-03-18 20:35:44 +05:30
|
|
|
fprintf(f,"#Size\n");
|
|
|
|
fprintf(f,"#Rss\n");
|
|
|
|
fprintf(f,"#Pss\n");
|
|
|
|
fprintf(f,"#Shared_Clean\n");
|
|
|
|
fprintf(f,"#Shared_Dirty\n");
|
|
|
|
fprintf(f,"#Private_Clean\n");
|
|
|
|
fprintf(f,"#Private_Dirty\n");
|
|
|
|
fprintf(f,"#Referenced\n");
|
|
|
|
fprintf(f,"#Anonymous\n");
|
|
|
|
fprintf(f,"#AnonHugePages\n");
|
|
|
|
fprintf(f,"#Swap\n");
|
|
|
|
fprintf(f,"#KernelPageSize\n");
|
|
|
|
fprintf(f,"#MMUPageSize\n");
|
|
|
|
fprintf(f,"#Locked\n");
|
|
|
|
fprintf(f,"#VmFlags\n");
|
2013-03-16 10:30:00 +05:30
|
|
|
fprintf(f,"#%s\n", nls_Mapping);
|
2013-03-18 20:35:44 +05:30
|
|
|
fprintf(f,"\n");
|
|
|
|
fprintf(f,"\n");
|
|
|
|
fprintf(f,"[Mapping]\n");
|
|
|
|
fprintf(f,"\n");
|
|
|
|
fprintf(f,"# to show paths in the mapping column uncomment the following line\n");
|
|
|
|
fprintf(f,"#ShowPath\n");
|
|
|
|
fprintf(f,"\n");
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns rc filename based on the program_invocation_short_name */
|
|
|
|
static char *get_default_rc_filename(void)
|
|
|
|
{
|
|
|
|
char *rc_filename;
|
2013-03-26 15:04:22 +05:30
|
|
|
int rc_filename_len;
|
2013-03-18 20:35:44 +05:30
|
|
|
const char *homedir;
|
|
|
|
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
if (!homedir) {
|
|
|
|
xwarnx(_("HOME variable undefined"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_filename_len = snprintf(NULL, 0, "%s/.%src", homedir, program_invocation_short_name);
|
|
|
|
|
|
|
|
rc_filename = (char *) calloc (1, rc_filename_len + 1);
|
|
|
|
if (!rc_filename) {
|
|
|
|
xwarnx(_("memory allocation failed"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(rc_filename, rc_filename_len + 1, "%s/.%src", homedir, program_invocation_short_name);
|
|
|
|
|
|
|
|
return rc_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-02-04 22:51:39 +05:30
|
|
|
pid_t *pidlist;
|
2011-06-05 21:35:26 +05:30
|
|
|
unsigned count = 0;
|
|
|
|
PROCTAB *PT;
|
|
|
|
proc_t p;
|
2013-03-18 20:35:44 +05:30
|
|
|
int ret = 0, c, conf_ret;
|
2013-03-26 15:04:22 +05:30
|
|
|
char *rc_filename = NULL;
|
2011-06-05 21:35:26 +05:30
|
|
|
|
|
|
|
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'},
|
2013-03-18 20:35:44 +05:30
|
|
|
{"read-rc", no_argument, NULL, 'c'},
|
|
|
|
{"read-rc-from", required_argument, NULL, 'C'},
|
|
|
|
{"create-rc", no_argument, NULL, 'n'},
|
|
|
|
{"create-rc-to", required_argument, NULL, 'N'},
|
|
|
|
{"show-path", no_argument, NULL, 'p'},
|
2011-06-05 21:35:26 +05:30
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2013-02-20 23:01:48 +05:30
|
|
|
#ifdef HAVE_PROGRAM_INVOCATION_NAME
|
2013-03-18 20:35:44 +05:30
|
|
|
program_invocation_name = program_invocation_short_name;
|
2013-02-20 23:01:48 +05:30
|
|
|
#endif
|
2013-03-16 10:30:00 +05:30
|
|
|
nls_initialize();
|
2012-03-23 18:02:24 +05:30
|
|
|
atexit(close_stdout);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
2013-03-16 10:30:00 +05:30
|
|
|
if (argc < 2)
|
|
|
|
usage(stderr);
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
while ((c = getopt_long(argc, argv, "xXrdqA:hVcC:nN:p", 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;
|
2013-03-18 20:35:44 +05:30
|
|
|
case 'c':
|
|
|
|
c_option = 1;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
C_option = 1;
|
|
|
|
rc_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
n_option = 1;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
N_option = 1;
|
|
|
|
rc_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
map_desc_showpath = 1;
|
|
|
|
break;
|
2011-06-05 21:35:26 +05:30
|
|
|
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;
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
if (c_option + C_option + d_option + n_option + N_option + x_option + !!X_option > 1)
|
|
|
|
xerrx(EXIT_FAILURE, _("options -c, -C, -d, -n, -N, -x, -X are mutually exclusive"));
|
|
|
|
|
2013-03-19 15:17:36 +05:30
|
|
|
if ((n_option || N_option) && (q_option || map_desc_showpath))
|
2013-03-18 20:35:44 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("options -p, -q are mutually exclusive with -n, -N"));
|
|
|
|
|
|
|
|
if ((n_option || N_option) && argc > 0)
|
|
|
|
xerrx(EXIT_FAILURE, _("too many arguments"));
|
|
|
|
|
|
|
|
if (N_option) {
|
|
|
|
if (config_create(rc_filename)) {
|
|
|
|
xwarnx(_("rc file successfully created, feel free to edit the content"));
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
xerrx(EXIT_FAILURE, _("couldn't create the rc file"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_option) {
|
|
|
|
rc_filename = get_default_rc_filename();
|
|
|
|
|
|
|
|
if (!rc_filename) return(EXIT_FAILURE);
|
|
|
|
|
|
|
|
conf_ret = config_create(rc_filename); free(rc_filename);
|
|
|
|
|
|
|
|
if (conf_ret) {
|
|
|
|
xwarnx(_("~/.%src file successfully created, feel free to edit the content"), program_invocation_short_name);
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
xerrx(EXIT_FAILURE, _("couldn't create ~/.%src"), program_invocation_short_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-05 21:35:26 +05:30
|
|
|
if (argc < 1)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("argument missing"));
|
2013-03-18 20:35:44 +05:30
|
|
|
|
|
|
|
if (C_option) c_option = 1;
|
|
|
|
|
|
|
|
if (c_option) {
|
|
|
|
|
|
|
|
if (!C_option) rc_filename = get_default_rc_filename();
|
|
|
|
|
|
|
|
if (!rc_filename) return(EXIT_FAILURE);
|
|
|
|
|
|
|
|
conf_ret = config_read(rc_filename);
|
|
|
|
|
|
|
|
if (!conf_ret) {
|
|
|
|
if (C_option) {
|
|
|
|
xerrx(EXIT_FAILURE, _("couldn't read the rc file"));
|
|
|
|
} else {
|
|
|
|
xwarnx(_("couldn't read ~/.%src"), program_invocation_short_name);
|
|
|
|
free(rc_filename);
|
|
|
|
return(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-06-05 21:35:26 +05:30
|
|
|
|
2013-02-14 18:25:51 +05:30
|
|
|
pidlist = xmalloc(sizeof(pid_t) * (argc+1));
|
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
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
/* cleaning the list used for the -c/-X/-XX modes */
|
2013-02-07 03:31:54 +05:30
|
|
|
for (listnode = listhead; listnode != NULL ; ) {
|
|
|
|
listnode = listnode -> next;
|
|
|
|
free(listhead);
|
|
|
|
listhead = listnode;
|
|
|
|
}
|
|
|
|
|
2013-03-18 20:35:44 +05:30
|
|
|
/* cleaning the list used for the -c mode */
|
|
|
|
for (cnf_listnode = cnf_listhead; cnf_listnode != NULL ; ) {
|
|
|
|
cnf_listnode = cnf_listnode -> next;
|
|
|
|
free(cnf_listhead);
|
|
|
|
cnf_listhead = cnf_listnode;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|