1 | /* $Id: procmap.c,v 1.1.1.2 2004-10-03 04:59:48 ghudson Exp $ */ |
---|
2 | |
---|
3 | /* Copyright (C) 1998-99 Martin Baulig |
---|
4 | This file is part of LibGTop 1.0. |
---|
5 | |
---|
6 | Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. |
---|
7 | |
---|
8 | LibGTop is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, |
---|
11 | or (at your option) any later version. |
---|
12 | |
---|
13 | LibGTop is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
16 | for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with LibGTop; see the file COPYING. If not, write to the |
---|
20 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
21 | Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <locale.h> |
---|
25 | #include <libintl.h> |
---|
26 | #include <stdio.h> |
---|
27 | |
---|
28 | #include <glibtop.h> |
---|
29 | #include <glibtop/open.h> |
---|
30 | #include <glibtop/close.h> |
---|
31 | |
---|
32 | #include <glibtop/parameter.h> |
---|
33 | |
---|
34 | #include <glibtop/procmap.h> |
---|
35 | |
---|
36 | #ifdef GLIBTOP_INODEDB |
---|
37 | #include <glibtop/inodedb.h> |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <sys/resource.h> |
---|
41 | #include <sys/mman.h> |
---|
42 | |
---|
43 | #ifndef PROFILE_COUNT |
---|
44 | #define PROFILE_COUNT 1 |
---|
45 | #endif |
---|
46 | |
---|
47 | int |
---|
48 | main (int argc, char *argv []) |
---|
49 | { |
---|
50 | #ifdef GLIBTOP_INODEDB |
---|
51 | glibtop_inodedb *inodedb; |
---|
52 | #endif |
---|
53 | glibtop_proc_map procmap; |
---|
54 | glibtop_map_entry *maps; |
---|
55 | unsigned method, count, port, i; |
---|
56 | char buffer [BUFSIZ]; |
---|
57 | pid_t pid; |
---|
58 | |
---|
59 | count = PROFILE_COUNT; |
---|
60 | |
---|
61 | setlocale (LC_ALL, ""); |
---|
62 | bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR); |
---|
63 | textdomain (GETTEXT_PACKAGE); |
---|
64 | |
---|
65 | glibtop_init_r (&glibtop_global_server, 0, GLIBTOP_INIT_NO_OPEN); |
---|
66 | |
---|
67 | glibtop_get_parameter (GLIBTOP_PARAM_METHOD, &method, sizeof (method)); |
---|
68 | |
---|
69 | printf ("Method = %d\n", method); |
---|
70 | |
---|
71 | count = glibtop_get_parameter (GLIBTOP_PARAM_COMMAND, buffer, BUFSIZ); |
---|
72 | buffer [count] = 0; |
---|
73 | |
---|
74 | printf ("Command = '%s'\n", buffer); |
---|
75 | |
---|
76 | count = glibtop_get_parameter (GLIBTOP_PARAM_HOST, buffer, BUFSIZ); |
---|
77 | buffer [count] = 0; |
---|
78 | |
---|
79 | glibtop_get_parameter (GLIBTOP_PARAM_PORT, &port, sizeof (port)); |
---|
80 | |
---|
81 | printf ("Host = '%s' - %u\n\n", buffer, port); |
---|
82 | |
---|
83 | glibtop_init_r (&glibtop_global_server, 0, 0); |
---|
84 | |
---|
85 | if ((argc != 2) || (sscanf (argv [1], "%d", (int *) &pid) != 1)) |
---|
86 | glibtop_error ("Usage: %s pid", argv [0]); |
---|
87 | |
---|
88 | #ifdef GLIBTOP_INODEDB |
---|
89 | inodedb = glibtop_inodedb_open (0, 0); |
---|
90 | #endif |
---|
91 | |
---|
92 | fprintf (stderr, "Getting memory maps for pid %d.\n\n", (int) pid); |
---|
93 | |
---|
94 | maps = glibtop_get_proc_map (&procmap, pid); |
---|
95 | |
---|
96 | for (i = 0; i < procmap.number; i++) { |
---|
97 | const char *filename = NULL; |
---|
98 | unsigned device, device_major, device_minor; |
---|
99 | char perm [5]; |
---|
100 | |
---|
101 | if (maps [i].flags & (1L << GLIBTOP_MAP_ENTRY_FILENAME)) |
---|
102 | filename = maps [i].filename; |
---|
103 | |
---|
104 | #ifdef GLIBTOP_INODEDB |
---|
105 | if (inodedb && !filename) |
---|
106 | filename = glibtop_inodedb_lookup |
---|
107 | (inodedb, maps [i].device, maps [i].inode); |
---|
108 | #endif |
---|
109 | |
---|
110 | perm [0] = (maps [i].perm & GLIBTOP_MAP_PERM_READ) ? 'r' : '-'; |
---|
111 | perm [1] = (maps [i].perm & GLIBTOP_MAP_PERM_WRITE) ? 'w' : '-'; |
---|
112 | perm [2] = (maps [i].perm & GLIBTOP_MAP_PERM_EXECUTE) ? 'x' : '-'; |
---|
113 | perm [3] = (maps [i].perm & GLIBTOP_MAP_PERM_SHARED) ? 's' : '-'; |
---|
114 | perm [4] = (maps [i].perm & GLIBTOP_MAP_PERM_PRIVATE) ? 'p' : '-'; |
---|
115 | |
---|
116 | device = (unsigned long) maps [i].device; |
---|
117 | device_minor = (device & 255); |
---|
118 | device_major = ((device >> 8) & 255); |
---|
119 | |
---|
120 | if (filename) { |
---|
121 | char *format; |
---|
122 | |
---|
123 | if (sizeof (void*) == 8) |
---|
124 | format = "%016lx-%016lx %016lx - " |
---|
125 | "%02x:%02x %08lu - %4s - %s\n"; |
---|
126 | else |
---|
127 | format = "%08lx-%08lx %08lx - " |
---|
128 | "%02x:%02x %08lu - %4s - %s\n"; |
---|
129 | |
---|
130 | fprintf (stderr, format, |
---|
131 | (unsigned long) maps [i].start, |
---|
132 | (unsigned long) maps [i].end, |
---|
133 | (unsigned long) maps [i].offset, |
---|
134 | device_major, device_minor, |
---|
135 | (unsigned long) maps [i].inode, |
---|
136 | perm, filename); |
---|
137 | } else { |
---|
138 | char * format; |
---|
139 | |
---|
140 | if (sizeof (void*) == 8) |
---|
141 | format = "%016lx-%016lx %016lx - " |
---|
142 | "%02x:%02x %08lu - %4s\n"; |
---|
143 | else |
---|
144 | format = "%08lx-%08lx %08lx - " |
---|
145 | "%02x:%02x %08lu - %4s\n"; |
---|
146 | |
---|
147 | fprintf (stderr, format, |
---|
148 | (unsigned long) maps [i].start, |
---|
149 | (unsigned long) maps [i].end, |
---|
150 | (unsigned long) maps [i].offset, |
---|
151 | device_major, device_minor, |
---|
152 | (unsigned long) maps [i].inode, |
---|
153 | perm); |
---|
154 | } |
---|
155 | |
---|
156 | if (filename && (filename != maps [i].filename)) |
---|
157 | g_free ((void*)filename); |
---|
158 | } |
---|
159 | |
---|
160 | g_free (maps); |
---|
161 | |
---|
162 | glibtop_close (); |
---|
163 | |
---|
164 | exit (0); |
---|
165 | } |
---|