sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A simple parser for /proc/self/maps on Linux 2.4.X ---*/ |
| 4 | /*--- vg_procselfmaps.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, a dynamic binary instrumentation |
| 9 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2005 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 33 | #include "core.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 35 | /* Size of a smallish table used to read /proc/self/map entries. */ |
| 36 | #define M_PROCMAP_BUF 50000 |
| 37 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 38 | /* static ... to keep it out of the stack frame. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 39 | static Char procmap_buf[M_PROCMAP_BUF]; |
| 40 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 41 | /* Records length of /proc/self/maps read into procmap_buf. */ |
| 42 | static Int buf_n_tot; |
| 43 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 44 | |
| 45 | /* Helper fns. */ |
| 46 | |
| 47 | static Int hexdigit ( Char c ) |
| 48 | { |
| 49 | if (c >= '0' && c <= '9') return (Int)(c - '0'); |
| 50 | if (c >= 'a' && c <= 'f') return 10 + (Int)(c - 'a'); |
| 51 | if (c >= 'A' && c <= 'F') return 10 + (Int)(c - 'A'); |
| 52 | return -1; |
| 53 | } |
| 54 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 55 | static Int decdigit ( Char c ) |
| 56 | { |
| 57 | if (c >= '0' && c <= '9') return (Int)(c - '0'); |
| 58 | return -1; |
| 59 | } |
| 60 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 61 | static Int readchar ( const Char* buf, Char* ch ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 62 | { |
| 63 | if (*buf == 0) return 0; |
| 64 | *ch = *buf; |
| 65 | return 1; |
| 66 | } |
| 67 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 68 | static Int readhex ( const Char* buf, UWord* val ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 69 | { |
| 70 | Int n = 0; |
| 71 | *val = 0; |
| 72 | while (hexdigit(*buf) >= 0) { |
| 73 | *val = (*val << 4) + hexdigit(*buf); |
| 74 | n++; buf++; |
| 75 | } |
| 76 | return n; |
| 77 | } |
| 78 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 79 | static Int readdec ( const Char* buf, UInt* val ) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 80 | { |
| 81 | Int n = 0; |
| 82 | *val = 0; |
| 83 | while (hexdigit(*buf) >= 0) { |
| 84 | *val = (*val * 10) + decdigit(*buf); |
| 85 | n++; buf++; |
| 86 | } |
| 87 | return n; |
| 88 | } |
| 89 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 90 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 91 | /* Read /proc/self/maps, store the contents into a static buffer. If |
| 92 | there's a syntax error or other failure, just abort. */ |
| 93 | |
| 94 | static void read_procselfmaps ( void ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 95 | { |
| 96 | Int n_chunk, fd; |
| 97 | |
| 98 | /* Read the initial memory mapping from the /proc filesystem. */ |
| 99 | fd = VG_(open) ( "/proc/self/maps", VKI_O_RDONLY, 0 ); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 100 | if (fd < 0) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 101 | VG_(message)(Vg_UserMsg, "FATAL: can't open /proc/self/maps"); |
| 102 | VG_(exit)(1); |
| 103 | } |
| 104 | buf_n_tot = 0; |
| 105 | do { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 106 | n_chunk = VG_(read) ( fd, &procmap_buf[buf_n_tot], |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 107 | M_PROCMAP_BUF - buf_n_tot ); |
| 108 | buf_n_tot += n_chunk; |
| 109 | } while ( n_chunk > 0 && buf_n_tot < M_PROCMAP_BUF ); |
| 110 | VG_(close)(fd); |
| 111 | if (buf_n_tot >= M_PROCMAP_BUF-5) { |
| 112 | VG_(message)(Vg_UserMsg, "FATAL: M_PROCMAP_BUF is too small; " |
| 113 | "increase it and recompile"); |
| 114 | VG_(exit)(1); |
| 115 | } |
| 116 | if (buf_n_tot == 0) { |
| 117 | VG_(message)(Vg_UserMsg, "FATAL: I/O error on /proc/self/maps" ); |
| 118 | VG_(exit)(1); |
| 119 | } |
| 120 | procmap_buf[buf_n_tot] = 0; |
| 121 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 122 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 123 | /* Parse /proc/self/maps. For each map entry, call |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 124 | record_mapping, passing it, in this order: |
| 125 | |
| 126 | start address in memory |
| 127 | length |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 128 | page protections (using the VKI_PROT_* flags) |
| 129 | mapped file device and inode |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 130 | offset in file, or zero if no file |
| 131 | filename, zero terminated, or NULL if no file |
| 132 | |
| 133 | So the sig of the called fn might be |
| 134 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 135 | void (*record_mapping)( Addr start, SizeT size, UInt prot, |
| 136 | UInt dev, UInt info, |
nethercote | d925548 | 2004-10-26 10:19:30 +0000 | [diff] [blame] | 137 | ULong foffset, UChar* filename ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 138 | |
| 139 | Note that the supplied filename is transiently stored; record_mapping |
| 140 | should make a copy if it wants to keep it. |
| 141 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 142 | Nb: it is important that this function does not alter the contents of |
| 143 | procmap_buf! |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 144 | */ |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 145 | void VG_(parse_procselfmaps) ( |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 146 | void (*record_mapping)( Addr addr, SizeT len, UInt prot, |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 147 | UInt dev, UInt ino, ULong foff, const UChar* filename ) |
| 148 | ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 149 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 150 | Int i, j, i_eol; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 151 | Addr start, endPlusOne; |
| 152 | UChar* filename; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 153 | UChar rr, ww, xx, pp, ch, tmp; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 154 | UInt ino, prot; |
nethercote | 6a27d83 | 2004-09-07 10:17:02 +0000 | [diff] [blame] | 155 | UWord foffset, maj, min; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 156 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 157 | read_procselfmaps(); |
| 158 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 159 | tl_assert( '\0' != procmap_buf[0] && 0 != buf_n_tot); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 160 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 161 | if (0) |
| 162 | VG_(message)(Vg_DebugMsg, "raw:\n%s", procmap_buf ); |
| 163 | |
| 164 | /* Ok, it's safely aboard. Parse the entries. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 165 | i = 0; |
| 166 | while (True) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 167 | if (i >= buf_n_tot) break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 168 | |
sewardj | 0a54cef | 2005-02-17 09:29:33 +0000 | [diff] [blame] | 169 | /* Read (without fscanf :) the pattern %16x-%16x %c%c%c%c %16x %2x:%2x %d */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 170 | j = readhex(&procmap_buf[i], &start); |
| 171 | if (j > 0) i += j; else goto syntaxerror; |
| 172 | j = readchar(&procmap_buf[i], &ch); |
| 173 | if (j == 1 && ch == '-') i += j; else goto syntaxerror; |
| 174 | j = readhex(&procmap_buf[i], &endPlusOne); |
| 175 | if (j > 0) i += j; else goto syntaxerror; |
| 176 | |
| 177 | j = readchar(&procmap_buf[i], &ch); |
| 178 | if (j == 1 && ch == ' ') i += j; else goto syntaxerror; |
| 179 | |
| 180 | j = readchar(&procmap_buf[i], &rr); |
| 181 | if (j == 1 && (rr == 'r' || rr == '-')) i += j; else goto syntaxerror; |
| 182 | j = readchar(&procmap_buf[i], &ww); |
| 183 | if (j == 1 && (ww == 'w' || ww == '-')) i += j; else goto syntaxerror; |
| 184 | j = readchar(&procmap_buf[i], &xx); |
| 185 | if (j == 1 && (xx == 'x' || xx == '-')) i += j; else goto syntaxerror; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 186 | /* This field is the shared/private flag */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 187 | j = readchar(&procmap_buf[i], &pp); |
| 188 | if (j == 1 && (pp == 'p' || pp == '-' || pp == 's')) |
| 189 | i += j; else goto syntaxerror; |
| 190 | |
| 191 | j = readchar(&procmap_buf[i], &ch); |
| 192 | if (j == 1 && ch == ' ') i += j; else goto syntaxerror; |
| 193 | |
| 194 | j = readhex(&procmap_buf[i], &foffset); |
| 195 | if (j > 0) i += j; else goto syntaxerror; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 196 | |
| 197 | j = readchar(&procmap_buf[i], &ch); |
| 198 | if (j == 1 && ch == ' ') i += j; else goto syntaxerror; |
| 199 | |
| 200 | j = readhex(&procmap_buf[i], &maj); |
| 201 | if (j > 0) i += j; else goto syntaxerror; |
| 202 | j = readchar(&procmap_buf[i], &ch); |
| 203 | if (j == 1 && ch == ':') i += j; else goto syntaxerror; |
| 204 | j = readhex(&procmap_buf[i], &min); |
| 205 | if (j > 0) i += j; else goto syntaxerror; |
| 206 | |
| 207 | j = readchar(&procmap_buf[i], &ch); |
| 208 | if (j == 1 && ch == ' ') i += j; else goto syntaxerror; |
| 209 | |
| 210 | j = readdec(&procmap_buf[i], &ino); |
| 211 | if (j > 0) i += j; else goto syntaxerror; |
| 212 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 213 | goto read_line_ok; |
| 214 | |
| 215 | syntaxerror: |
| 216 | VG_(message)(Vg_UserMsg, "FATAL: syntax error reading /proc/self/maps"); |
| 217 | { Int k; |
| 218 | VG_(printf)("last 50 chars: `"); |
| 219 | for (k = i-50; k <= i; k++) VG_(printf)("%c", procmap_buf[k]); |
| 220 | VG_(printf)("'\n"); |
| 221 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 222 | VG_(exit)(1); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 223 | |
| 224 | read_line_ok: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 225 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 226 | /* Try and find the name of the file mapped to this segment, if |
| 227 | it exists. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 228 | while (procmap_buf[i] != '\n' && i < buf_n_tot-1) i++; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 229 | i_eol = i; |
| 230 | i--; |
| 231 | while (!VG_(isspace)(procmap_buf[i]) && i >= 0) i--; |
| 232 | i++; |
| 233 | if (i < i_eol-1 && procmap_buf[i] == '/') { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 234 | /* Minor hack: put a '\0' at the filename end for the call to |
| 235 | `record_mapping', then restore the old char with `tmp'. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 236 | filename = &procmap_buf[i]; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 237 | tmp = filename[i_eol - i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 238 | filename[i_eol - i] = '\0'; |
| 239 | } else { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 240 | tmp = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 241 | filename = NULL; |
| 242 | foffset = 0; |
| 243 | } |
| 244 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 245 | prot = 0; |
| 246 | if (rr == 'r') prot |= VKI_PROT_READ; |
| 247 | if (ww == 'w') prot |= VKI_PROT_WRITE; |
| 248 | if (xx == 'x') prot |= VKI_PROT_EXEC; |
| 249 | |
| 250 | //if (start < VG_(valgrind_last)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 251 | (*record_mapping) ( start, endPlusOne-start, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 252 | prot, maj * 256 + min, ino, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 253 | foffset, filename ); |
| 254 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 255 | if ('\0' != tmp) { |
| 256 | filename[i_eol - i] = tmp; |
| 257 | } |
| 258 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 259 | i = i_eol + 1; |
| 260 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /*--------------------------------------------------------------------*/ |
| 264 | /*--- end vg_procselfmaps.c ---*/ |
| 265 | /*--------------------------------------------------------------------*/ |