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