Loading...
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | // ksymoops.cc v1.7 -- A simple filter to resolve symbols in Linux Oops-logs // Copyright (C) 1995 Greg McGary <gkm@magilla.cichlid.com> // compile like so: g++ -o ksymoops ksymoops.cc -liostream ////////////////////////////////////////////////////////////////////////////// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // 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; see the file COPYING. If not, write to the // Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // This is a simple filter to resolve EIP and call-trace symbols from // a Linux kernel "Oops" log. Supply the symbol-map file name as a // command-line argument, and redirect the oops-log into stdin. Out // will come the EIP and call-trace in symbolic form. ////////////////////////////////////////////////////////////////////////////// // BUGS: // * Doesn't deal with line-prefixes prepended by syslog--strip // these off first, before submitting to ksymoops. // * Only resolves operands of jump and call instructions. #include <fstream.h> #include <iomanip.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <ctype.h> inline int strequ(char const* x, char const* y) { return (::strcmp(x, y) == 0); } inline int strnequ(char const* x, char const* y, size_t n) { return (::strncmp(x, y, n) == 0); } const int code_size = 20; ////////////////////////////////////////////////////////////////////////////// class KSym { friend class NameList; private: long address_; char* name_; long offset_; long extent_; private: istream& scan(istream&); ostream& print(ostream&) const; void set_extent(KSym const& next_ksym) { extent_ = next_ksym.address_ - address_; } public: friend istream& operator >> (istream& is, KSym& k) { return k.scan(is); } friend ostream& operator << (ostream& os, const KSym& k) { return k.print(os); } }; istream& KSym::scan(istream& is) { is >> ::hex >> address_; char type; is >> type; char name[128]; is >> name; name_ = new char [strlen(name)+1]; strcpy(name_, name); offset_ = 0; return is; } ostream& KSym::print(ostream& os) const { os << ::hex << address_ + offset_ << ' ' << '<' << name_; if (offset_) os << '+' << ::hex << offset_ << '/' << ::hex << extent_; return os << '>'; } ////////////////////////////////////////////////////////////////////////////// class NameList { private: // Caution: Fixed Allocation! // This should suffice for awhile since 1.1.86 has only 2482 symbols. KSym ksyms_0_[8000]; int cardinality_; public: NameList() : cardinality_(0) { } private: istream& scan(istream&); public: int valid() { return (cardinality_ > 0); } KSym* find(long address); void decode(unsigned char* code, long eip_addr); public: friend istream& operator >> (istream& is, NameList& n) { return n.scan(is); } }; KSym* NameList::find(long address) { if (!valid()) return 0; KSym* start = ksyms_0_; KSym* end = &ksyms_0_[cardinality_ - 1]; if (address < start->address_ || address >= end->address_) return 0; KSym* mid; while (start <= end) { mid = &start[(end - start) / 2]; if (mid->address_ < address) start = mid + 1; else if (mid->address_ > address) end = mid - 1; else return mid; } while (mid->address_ > address) --mid; mid->offset_ = address - mid->address_; if (mid->offset_ > mid->extent_) clog << "Oops! " << *mid << endl; return mid; } void NameList::decode(unsigned char* code, long eip_addr) { /* This is a hack to avoid using gcc. We create an object file by concatenating objfile_head, the twenty bytes of code, and objfile_tail. */ unsigned char objfile_head[] = { 0x07, 0x01, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; unsigned char objfile_tail[] = { 0x00, 0x90, 0x90, 0x90, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 'g', 'c', 'c', '2', '_', 'c', 'o', 'm', 'p', 'i', 'l', 'e', 'd', '.', '\0', '_', 'E', 'I', 'P', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; char const* objdump_command = "objdump -d oops_decode.o"; char const* objfile_name = &objdump_command[11]; ofstream objfile_stream(objfile_name); objfile_stream.write(objfile_head, sizeof(objfile_head)); objfile_stream.write(code, code_size); objfile_stream.write(objfile_tail, sizeof(objfile_tail)); objfile_stream.close(); FILE* objdump_FILE = popen(objdump_command, "r"); if (objdump_FILE == 0) { clog << "Sorry, without " << objdump_command << ", I can't disassemble the `Code' section." << endl; return; } char buf[1024]; int lines = 0; while (fgets(buf, sizeof(buf), objdump_FILE)) { if (!strnequ(&buf[9], "<_EIP", 5)) continue; if (strstr(buf, " is out of bounds")) break; lines++; cout << "Code: "; if (!valid()) { cout << buf; continue; } long offset = strtol(buf, 0, 16); char* bp_0 = strchr(buf, '>') + 2; KSym* ksym = find(eip_addr + offset); if (ksym) cout << *ksym << ' '; char* bp = bp_0; while (!isspace(*bp)) bp++; while (isspace(*bp)) bp++; if (*bp != '0') { cout << bp_0; } else if (*bp_0 == 'j' || strnequ(bp_0, "call", 4)) { // a jump or call insn long rel_addr = strtol(bp, 0, 16); ksym = find(eip_addr + rel_addr); if (ksym) { *bp++ = '\0'; cout << bp_0 << *ksym << endl; } else cout << bp_0; } else { cout << bp_0; } } if (!lines) clog << "Sorry, your " << objdump_command << " can't disassemble--you must upgrade your binutils." << endl; pclose(objdump_FILE); unlink(objfile_name); } istream& NameList::scan(istream& is) { KSym* ksyms = ksyms_0_; int cardinality = 0; while (!is.eof()) { is >> *ksyms; if (cardinality++ > 0) ksyms[-1].set_extent(*ksyms); ksyms++; } cardinality_ = --cardinality; return is; } ////////////////////////////////////////////////////////////////////////////// char const* program_name; void usage() { clog << "Usage: " << program_name << " [ System.map ] < oops-log" << endl; exit(1); } int main(int argc, char** argv) { char c; program_name = (argc--, *argv++); NameList names; if (argc > 1) usage(); else if (argc == 1) { char const* map_file_name = (argc--, *argv++); ifstream map(map_file_name); if (map.bad()) clog << program_name << ": Can't open `" << map_file_name << "'" << endl; else { map >> names; cout << "Using `" << map_file_name << "' to map addresses to symbols." << endl; } } if (!names.valid()) cout << "No symbol map. I'll only show you disassembled code." << endl; cout << endl; char buffer[1024]; while (!cin.eof()) { long eip_addr; cin >> buffer; if (strequ(buffer, "EIP:") && names.valid()) { cin >> ::hex >> eip_addr; cin >> c >> c >> c; cin >> ::hex >> eip_addr; cin >> c >> c >> buffer; if (!strequ(buffer, "EFLAGS:")) { clog << "Please strip the line-prefixes and rerun " << program_name << endl; exit(1); } KSym* ksym = names.find(eip_addr); if (ksym) cout << ">>EIP: " << *ksym << endl; } else if (strequ(buffer, "Trace:") && names.valid()) { long address; while ((cin >> buffer) && (sscanf(buffer, " [<%x>]", &address) == 1) && address > 0xc) { cout << "Trace: "; KSym* ksym = names.find(address); if (ksym) cout << *ksym; else cout << ::hex << address; cout << endl; } cout << endl; } if (strequ(buffer, "ode:") || strequ(buffer, "Code:")) { // The 'C' might have been consumed as a hex number unsigned char code[code_size]; unsigned char* cp = code; unsigned char* end = &code[code_size]; while (cp < end) { int c; cin >> ::hex >> c; *cp++ = c; } names.decode(code, eip_addr); } } cout << flush; return 0; } |