[a8d9ee]: / daemon / liblegacy / opd_parse_proc.c  Maximize  Restore  History

Download this file

224 lines (180 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @file opd_parse_proc.c
* Parsing of /proc/#pid
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author John Levon
* @author Philippe Elie
*/
#include "op_libiberty.h"
#include "opd_parse_proc.h"
#include "opd_proc.h"
#include "opd_mapping.h"
#include "opd_image.h"
#include "opd_printf.h"
#include "op_file.h"
#include "op_fileio.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* opd_add_ascii_map - parse an ASCII map string for a process
* @param proc process to add map to
* @param line 0-terminated ASCII string
* @param image_name the binary application name
*
* Attempt to parse the string @line for map information
* and add the info to the process @proc. Returns %1
* on success, %0 otherwise.
*
* The parsing is based on Linux 2.4 format, which looks like this :
*
* 4001e000-400fc000 r-xp 00000000 03:04 31011 /lib/libc-2.1.2.so
*/
/* FIXME: handle (deleted) */
static int opd_add_ascii_map(struct opd_proc * proc, char const * line,
char * const image_name)
{
unsigned long offset, start, end;
struct opd_image * image;
char const * cp = line;
/* skip to protection field */
while (*cp && *cp != ' ')
cp++;
/* handle rwx */
if (!*cp || (!*(++cp)) || (!*(++cp)) || (*(++cp) != 'x'))
return 0;
/* get start and end from "40000000-4001f000" */
if (sscanf(line, "%lx-%lx", &start, &end) != 2)
return 0;
/* "p " */
cp += 2;
/* read offset */
if (sscanf(cp, "%lx", &offset) != 1)
return 0;
while (*cp && *cp != '/')
cp++;
if (!*cp)
return 0;
image = opd_get_image(cp, image_name, 0, proc->tid, proc->tgid);
if (!image)
return 0;
opd_add_mapping(proc, image, start, offset, end);
return 1;
}
/**
* opd_get_ascii_maps - read all maps for a process
* @param proc process to work on
*
* Read the /proc/<pid>/maps file and add all
* mapping information found to the process @proc.
*/
static void opd_get_ascii_maps(struct opd_proc * proc)
{
FILE * fp;
char mapsfile[20] = "/proc/";
char * line;
char exe_name[20];
char * image_name;
struct list_head * pos;
snprintf(mapsfile + 6, 6, "%hu", proc->tid);
strcpy(exe_name, mapsfile);
strcat(mapsfile, "/maps");
fp = op_try_open_file(mapsfile, "r");
if (!fp)
return;
strcat(exe_name, "/exe");
image_name = xmalloc(PATH_MAX);
if (!realpath(exe_name, image_name))
/* kernel thread are invalid symlink */
strcpy(image_name, exe_name);
verbprintf(vmisc, "image name %s for pid %u %u\n", image_name, proc->tid, proc->tgid);
while (1) {
line = op_get_line(fp);
if (!line)
break;
opd_add_ascii_map(proc, line, image_name);
free(line);
}
/* dae assume than the first map added is the primary image name, this
* is always true at exec time but not for /proc/pid so restore
* the primary image name
*/
list_for_each(pos, &proc->maps) {
struct opd_map * map = list_entry(pos, struct opd_map, next);
if (!strcmp(map->image->name, image_name)) {
if (pos != proc->maps.next) {
fprintf(stderr, "swap map for image %s from %s to %s\n", image_name, proc->name, map->image->name);
free((char *)proc->name);
proc->name = xstrdup(map->image->name);
}
break;
}
}
if (list_empty(&proc->maps)) {
/* we always need a valid proc->maps[0], we artificially give
* a map of length zero so on no samples will never go to this
* map. This is used only with --separate-samples and kernel
* thread when adding vmlinux and module maps to proc->maps[]
*/
/* FIXME: use the first field of /proc/pid/status as proc name
* for now we use /proc/%pid/exe as name */
struct opd_image * image = opd_get_image(image_name,
image_name, 0, proc->tid, proc->tgid);
if (image)
opd_add_mapping(proc, image, 0, 0, 0);
}
if (image_name)
free(image_name);
op_close_file(fp);
}
static u32 read_tgid(u32 tid)
{
char status_file[30] = "/proc/";
char * line;
FILE * fp;
u32 tgid;
snprintf(status_file + 6, 6, "%hu", tid);
strcat(status_file, "/status");
fp = op_try_open_file(status_file, "r");
if (!fp)
return 0;
while (1) {
line = op_get_line(fp);
if (!line)
break;
if (sscanf(line, "Tgid: %u", &tgid) == 1) {
free(line);
op_close_file(fp);
return tgid;
}
free(line);
}
op_close_file(fp);
return 0;
}
void opd_get_ascii_procs(void)
{
DIR * dir;
struct dirent * dirent;
struct opd_proc * proc;
u32 pid;
if (!(dir = opendir("/proc"))) {
perror("oprofiled: /proc directory could not be opened. ");
exit(EXIT_FAILURE);
}
while ((dirent = readdir(dir))) {
if (sscanf(dirent->d_name, "%u", &pid) == 1) {
u32 tgid = read_tgid(pid);
verbprintf(vmisc, "ASCII added %u %u\n", pid, tgid);
proc = opd_get_proc(pid, tgid);
if (!proc)
proc = opd_new_proc(pid, tgid);
opd_get_ascii_maps(proc);
}
}
closedir(dir);
}