2 * Copyright (C) 2010 gonzoj
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "proc_utils.h"
32 static char *line
= NULL
;
45 line
= (char *) realloc(line
, i
+ 512);
48 printf("err: %s\n", strerror(errno
));
53 char chr
= fgetc(file
);
70 proc_get_object_base(const char *object
, vaddr_t
*base
)
73 (char *) malloc(strlen("/proc/") + PID_MAX
+ strlen("/maps") + 1);
76 printf("err: %s\n", strerror(errno
));
79 sprintf(path
, "/proc/%u/maps", pid
);
80 FILE *maps
= fopen(path
, "r");
83 printf("err: %s\n", strerror(errno
));
90 while ((line
= read_line(maps
)))
92 if (strstr(line
, object
))
94 char *tmp
= strchr(line
, '-');
98 sscanf(line
, "%lx", (long unsigned *) base
);
112 proc_get_object_path(vaddr_t object_base
)
114 static char *object_path
= NULL
;
123 (char *) malloc(strlen("/proc/") + PID_MAX
+ strlen("/maps") + 1);
126 printf("err: %s\n", strerror(errno
));
129 sprintf(path
, "/proc/%u/maps", pid
);
130 FILE *maps
= fopen(path
, "r");
133 printf("err: %s\n", strerror(errno
));
139 while ((line
= read_line(maps
)))
141 char *tmp
= strchr(line
, '-');
146 sscanf(line
, "%lx", (long unsigned *) &base
);
147 if (base
== object_base
)
149 object_path
= (char *) malloc(strlen(tmp
));
150 strcpy(object_path
, strchr(tmp
, '/'));
163 proc_get_executable()
165 static char *executable
;
173 char *path
= (char *) malloc(strlen("/proc/") + PID_MAX
+ strlen("/exe") + 1);
176 printf("err: %s\n", strerror(errno
));
179 sprintf(path
, "/proc/%u/exe", pid
);
180 executable
= (char *) malloc(PATH_MAX
); // let's hope that one is defined :(
181 if (readlink(path
, executable
, PATH_MAX
) == -1)
183 printf("err: %s\n", strerror(errno
));
194 proc_iterate_addrspace(vaddr_t
*base
)
196 static FILE *maps
= NULL
;
199 char *path
= (char *) malloc(strlen("/proc/") + PID_MAX
+ strlen("/maps")
203 printf("err: %s\n", strerror(errno
));
206 sprintf(path
, "/proc/%u/maps", pid
);
207 maps
= fopen(path
, "r");
210 printf("err: %s\n", strerror(errno
));
218 while ((line
= read_line(maps
)))
220 char *tmp
= strchr(line
, '-');
225 tmp
= strtok(NULL
, " ");
226 if (strchr(tmp
, 'r'))
231 tmp
= strtok(NULL
, " ");
234 if (strstr(tmp
, "/dev") == tmp
)
239 sscanf(line
, "%lx", (long unsigned *) base
);