-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc_maps.c
168 lines (141 loc) · 4.54 KB
/
proc_maps.c
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
/*
* This file is part of libsysperf
*
* Copyright (C) 2001, 2004-2007 by Nokia Corporation.
*
* Contact: Eero Tamminen <eero.tamminen@nokia.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/* ========================================================================= *
* File: proc_maps.c
*
* Author: Simo Piiroinen
*
* History:
*
* 21-Sep-2006 Simo Piiroinen
* - fixed include paths
*
* 29-May-2006 Simo Piiroinen
* - modified to use libsysperf API
* - moved from track2 source tree
* ========================================================================= */
#include <stdio.h>
#include "xmalloc.h"
#include "cstring.h"
#include "proc_maps.h"
/* ------------------------------------------------------------------------- *
* proc_maps_ctor
* ------------------------------------------------------------------------- */
void
proc_maps_ctor(proc_maps_t *self,
unsigned begin, unsigned end,
char *prot, char *path)
{
self->begin = begin;
self->end = end;
self->prot = xstrdup(prot);
self->path = xstrdup(path);
}
/* ------------------------------------------------------------------------- *
* proc_maps_dtor
* ------------------------------------------------------------------------- */
void
proc_maps_dtor(proc_maps_t *self)
{
xstrset(&self->prot, 0);
xstrset(&self->path, 0);
}
/* ------------------------------------------------------------------------- *
* proc_maps_create
* ------------------------------------------------------------------------- */
proc_maps_t *
proc_maps_create(unsigned begin, unsigned end,
char *prot, char *path)
{
proc_maps_t *self = xcalloc(1, sizeof *self);
proc_maps_ctor(self, begin, end, prot, path);
return self;
}
/* ------------------------------------------------------------------------- *
* proc_maps_delete
* ------------------------------------------------------------------------- */
void
proc_maps_delete(proc_maps_t *self)
{
if( self != 0 )
{
proc_maps_dtor(self);
xfree(self);
}
}
/* ------------------------------------------------------------------------- *
* proc_maps_cmp_begin
* ------------------------------------------------------------------------- */
int
proc_maps_cmp_begin(const void *a, const void *b)
{
const proc_maps_t *A = (const proc_maps_t *)a;
const proc_maps_t *B = (const proc_maps_t *)b;
return (int)(A->begin - B->begin);
}
/* ------------------------------------------------------------------------- *
* proc_maps_cmp_begin_indirect
* ------------------------------------------------------------------------- */
int
proc_maps_cmp_begin_indirect(const void *a, const void *b)
{
const proc_maps_t *A = *(const proc_maps_t **)a;
const proc_maps_t *B = *(const proc_maps_t **)b;
return (int)(A->begin - B->begin);
}
/* ------------------------------------------------------------------------- *
* proc_maps_parse
* ------------------------------------------------------------------------- */
void
proc_maps_parse(array_t *arr, const char *path)
{
char *data = cstring_from_file(path);
if( data != 0 )
{
char *line = data;
while( *line != 0 )
{
char *pos = cstring_split_at_char(line,&line, '\n');
char *b = cstring_split_at_char(pos, &pos, '-');
char *e = cstring_split_at_white(pos, &pos);
char *p = cstring_split_at_white(pos, &pos);
char *f;
cstring_split_at_white(pos, &pos);
cstring_split_at_white(pos, &pos);
cstring_split_at_white(pos, &pos);
f = cstring_split_at_white(pos, &pos);
array_add(arr, proc_maps_create(strtoul(b,0,16), strtoul(e,0,16), p, f));
}
xfree(data);
}
}
/* ------------------------------------------------------------------------- *
* proc_maps_repr
* ------------------------------------------------------------------------- */
void
proc_maps_repr(const proc_maps_t *obj, FILE *out)
{
fprintf(out, "%08x-%08x %s %s\n",
obj->begin, obj->end,
obj->prot, obj->path);
}