toolkit/crashreporter/google-breakpad/src/common/solaris/dump_symbols.cc

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Copyright (c) 2010 Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 // Author: Alfred Peng
michael@0 31
michael@0 32 #include <demangle.h>
michael@0 33 #include <fcntl.h>
michael@0 34 #include <gelf.h>
michael@0 35 #include <link.h>
michael@0 36 #include <sys/mman.h>
michael@0 37 #include <stab.h>
michael@0 38 #include <sys/stat.h>
michael@0 39 #include <sys/types.h>
michael@0 40 #include <unistd.h>
michael@0 41
michael@0 42 #include <functional>
michael@0 43 #include <map>
michael@0 44 #include <vector>
michael@0 45
michael@0 46 #include "common/scoped_ptr.h"
michael@0 47 #include "common/solaris/dump_symbols.h"
michael@0 48 #include "common/solaris/file_id.h"
michael@0 49 #include "common/solaris/guid_creator.h"
michael@0 50
michael@0 51 // This namespace contains helper functions.
michael@0 52 namespace {
michael@0 53
michael@0 54 using std::make_pair;
michael@0 55
michael@0 56 #if defined(_LP64)
michael@0 57 typedef Elf64_Sym Elf_Sym;
michael@0 58 #else
michael@0 59 typedef Elf32_Sym Elf_Sym;
michael@0 60 #endif
michael@0 61
michael@0 62 // Symbol table entry from stabs. Sun CC specific.
michael@0 63 struct slist {
michael@0 64 // String table index.
michael@0 65 unsigned int n_strx;
michael@0 66 // Stab type.
michael@0 67 unsigned char n_type;
michael@0 68 char n_other;
michael@0 69 short n_desc;
michael@0 70 unsigned long n_value;
michael@0 71 };
michael@0 72
michael@0 73 // Symbol table entry
michael@0 74 struct SymbolEntry {
michael@0 75 // Offset from the start of the file.
michael@0 76 GElf_Addr offset;
michael@0 77 // Function size.
michael@0 78 GElf_Word size;
michael@0 79 };
michael@0 80
michael@0 81 // Infomation of a line.
michael@0 82 struct LineInfo {
michael@0 83 // Offset from start of the function.
michael@0 84 // Load from stab symbol.
michael@0 85 GElf_Off rva_to_func;
michael@0 86 // Offset from base of the loading binary.
michael@0 87 GElf_Off rva_to_base;
michael@0 88 // Size of the line.
michael@0 89 // The first line: equals to rva_to_func.
michael@0 90 // The other lines: the difference of rva_to_func of the line and
michael@0 91 // rva_to_func of the previous N_SLINE.
michael@0 92 uint32_t size;
michael@0 93 // Line number.
michael@0 94 uint32_t line_num;
michael@0 95 };
michael@0 96
michael@0 97 // Information of a function.
michael@0 98 struct FuncInfo {
michael@0 99 // Name of the function.
michael@0 100 const char *name;
michael@0 101 // Offset from the base of the loading address.
michael@0 102 GElf_Off rva_to_base;
michael@0 103 // Virtual address of the function.
michael@0 104 // Load from stab symbol.
michael@0 105 GElf_Addr addr;
michael@0 106 // Size of the function.
michael@0 107 // Equal to rva_to_func of the last function line.
michael@0 108 uint32_t size;
michael@0 109 // Total size of stack parameters.
michael@0 110 uint32_t stack_param_size;
michael@0 111 // Line information array.
michael@0 112 std::vector<struct LineInfo> line_info;
michael@0 113 };
michael@0 114
michael@0 115 // Information of a source file.
michael@0 116 struct SourceFileInfo {
michael@0 117 // Name of the source file.
michael@0 118 const char *name;
michael@0 119 // Starting address of the source file.
michael@0 120 GElf_Addr addr;
michael@0 121 // Id of the source file.
michael@0 122 int source_id;
michael@0 123 // Functions information.
michael@0 124 std::vector<struct FuncInfo> func_info;
michael@0 125 };
michael@0 126
michael@0 127 struct CompareString {
michael@0 128 bool operator()(const char *s1, const char *s2) const {
michael@0 129 return strcmp(s1, s2) < 0;
michael@0 130 }
michael@0 131 };
michael@0 132
michael@0 133 typedef std::map<const char *, struct SymbolEntry *, CompareString> SymbolMap;
michael@0 134
michael@0 135 // Information of a symbol table.
michael@0 136 // This is the root of all types of symbol.
michael@0 137 struct SymbolInfo {
michael@0 138 std::vector<struct SourceFileInfo> source_file_info;
michael@0 139 // Symbols information.
michael@0 140 SymbolMap symbol_entries;
michael@0 141 };
michael@0 142
michael@0 143 // Stab section name.
michael@0 144 const char *kStabName = ".stab";
michael@0 145
michael@0 146 // Stab str section name.
michael@0 147 const char *kStabStrName = ".stabstr";
michael@0 148
michael@0 149 // Symtab section name.
michael@0 150 const char *kSymtabName = ".symtab";
michael@0 151
michael@0 152 // Strtab section name.
michael@0 153 const char *kStrtabName = ".strtab";
michael@0 154
michael@0 155 // Default buffer lenght for demangle.
michael@0 156 const int demangleLen = 20000;
michael@0 157
michael@0 158 // Offset to the string table.
michael@0 159 uint64_t stringOffset = 0;
michael@0 160
michael@0 161 // Update the offset to the start of the string index of the next
michael@0 162 // object module for every N_ENDM stabs.
michael@0 163 inline void RecalculateOffset(struct slist* cur_list, char *stabstr) {
michael@0 164 while ((--cur_list)->n_strx == 0) ;
michael@0 165 stringOffset += cur_list->n_strx;
michael@0 166
michael@0 167 char *temp = stabstr + stringOffset;
michael@0 168 while (*temp != '\0') {
michael@0 169 ++stringOffset;
michael@0 170 ++temp;
michael@0 171 }
michael@0 172 // Skip the extra '\0'
michael@0 173 ++stringOffset;
michael@0 174 }
michael@0 175
michael@0 176 // Demangle using demangle library on Solaris.
michael@0 177 std::string Demangle(const char *mangled) {
michael@0 178 int status = 0;
michael@0 179 std::string str(mangled);
michael@0 180 char *demangled = (char *)malloc(demangleLen);
michael@0 181
michael@0 182 if (!demangled) {
michael@0 183 fprintf(stderr, "no enough memory.\n");
michael@0 184 goto out;
michael@0 185 }
michael@0 186
michael@0 187 if ((status = cplus_demangle(mangled, demangled, demangleLen)) ==
michael@0 188 DEMANGLE_ESPACE) {
michael@0 189 fprintf(stderr, "incorrect demangle.\n");
michael@0 190 goto out;
michael@0 191 }
michael@0 192
michael@0 193 str = demangled;
michael@0 194 free(demangled);
michael@0 195
michael@0 196 out:
michael@0 197 return str;
michael@0 198 }
michael@0 199
michael@0 200 bool WriteFormat(int fd, const char *fmt, ...) {
michael@0 201 va_list list;
michael@0 202 char buffer[4096];
michael@0 203 ssize_t expected, written;
michael@0 204 va_start(list, fmt);
michael@0 205 vsnprintf(buffer, sizeof(buffer), fmt, list);
michael@0 206 expected = strlen(buffer);
michael@0 207 written = write(fd, buffer, strlen(buffer));
michael@0 208 va_end(list);
michael@0 209 return expected == written;
michael@0 210 }
michael@0 211
michael@0 212 bool IsValidElf(const GElf_Ehdr *elf_header) {
michael@0 213 return memcmp(elf_header, ELFMAG, SELFMAG) == 0;
michael@0 214 }
michael@0 215
michael@0 216 static bool FindSectionByName(Elf *elf, const char *name,
michael@0 217 int shstrndx,
michael@0 218 GElf_Shdr *shdr) {
michael@0 219 assert(name != NULL);
michael@0 220
michael@0 221 if (strlen(name) == 0)
michael@0 222 return false;
michael@0 223
michael@0 224 Elf_Scn *scn = NULL;
michael@0 225
michael@0 226 while ((scn = elf_nextscn(elf, scn)) != NULL) {
michael@0 227 if (gelf_getshdr(scn, shdr) == (GElf_Shdr *)0) {
michael@0 228 fprintf(stderr, "failed to read section header: %s\n", elf_errmsg(0));
michael@0 229 return false;
michael@0 230 }
michael@0 231
michael@0 232 const char *section_name = elf_strptr(elf, shstrndx, shdr->sh_name);
michael@0 233 if (!section_name) {
michael@0 234 fprintf(stderr, "Section name error: %s\n", elf_errmsg(-1));
michael@0 235 continue;
michael@0 236 }
michael@0 237
michael@0 238 if (strcmp(section_name, name) == 0)
michael@0 239 return true;
michael@0 240 }
michael@0 241
michael@0 242 return false;
michael@0 243 }
michael@0 244
michael@0 245 // The parameter size is used for FPO-optimized code, and
michael@0 246 // this is all tied up with the debugging data for Windows x86.
michael@0 247 // Set it to 0 on Solaris.
michael@0 248 int LoadStackParamSize(struct slist *list,
michael@0 249 struct slist *list_end,
michael@0 250 struct FuncInfo *func_info) {
michael@0 251 struct slist *cur_list = list;
michael@0 252 int step = 1;
michael@0 253 while (cur_list < list_end && cur_list->n_type == N_PSYM) {
michael@0 254 ++cur_list;
michael@0 255 ++step;
michael@0 256 }
michael@0 257
michael@0 258 func_info->stack_param_size = 0;
michael@0 259 return step;
michael@0 260 }
michael@0 261
michael@0 262 int LoadLineInfo(struct slist *list,
michael@0 263 struct slist *list_end,
michael@0 264 struct FuncInfo *func_info) {
michael@0 265 struct slist *cur_list = list;
michael@0 266 do {
michael@0 267 // Skip non line information.
michael@0 268 while (cur_list < list_end && cur_list->n_type != N_SLINE) {
michael@0 269 // Only exit when got another function, or source file, or end stab.
michael@0 270 if (cur_list->n_type == N_FUN || cur_list->n_type == N_SO ||
michael@0 271 cur_list->n_type == N_ENDM) {
michael@0 272 return cur_list - list;
michael@0 273 }
michael@0 274 ++cur_list;
michael@0 275 }
michael@0 276 struct LineInfo line;
michael@0 277 while (cur_list < list_end && cur_list->n_type == N_SLINE) {
michael@0 278 line.rva_to_func = cur_list->n_value;
michael@0 279 // n_desc is a signed short
michael@0 280 line.line_num = (unsigned short)cur_list->n_desc;
michael@0 281 func_info->line_info.push_back(line);
michael@0 282 ++cur_list;
michael@0 283 }
michael@0 284 if (cur_list == list_end && cur_list->n_type == N_ENDM)
michael@0 285 break;
michael@0 286 } while (list < list_end);
michael@0 287
michael@0 288 return cur_list - list;
michael@0 289 }
michael@0 290
michael@0 291 int LoadFuncSymbols(struct slist *list,
michael@0 292 struct slist *list_end,
michael@0 293 char *stabstr,
michael@0 294 GElf_Word base,
michael@0 295 struct SourceFileInfo *source_file_info) {
michael@0 296 struct slist *cur_list = list;
michael@0 297 assert(cur_list->n_type == N_SO);
michael@0 298 ++cur_list;
michael@0 299
michael@0 300 source_file_info->func_info.clear();
michael@0 301 while (cur_list < list_end) {
michael@0 302 // Go until the function symbol.
michael@0 303 while (cur_list < list_end && cur_list->n_type != N_FUN) {
michael@0 304 if (cur_list->n_type == N_SO) {
michael@0 305 return cur_list - list;
michael@0 306 }
michael@0 307 ++cur_list;
michael@0 308 if (cur_list->n_type == N_ENDM)
michael@0 309 RecalculateOffset(cur_list, stabstr);
michael@0 310 continue;
michael@0 311 }
michael@0 312 while (cur_list->n_type == N_FUN) {
michael@0 313 struct FuncInfo func_info;
michael@0 314 memset(&func_info, 0, sizeof(func_info));
michael@0 315 func_info.name = stabstr + cur_list->n_strx + stringOffset;
michael@0 316 // The n_value field is always 0 from stab generated by Sun CC.
michael@0 317 // TODO(Alfred): Find the correct value.
michael@0 318 func_info.addr = cur_list->n_value;
michael@0 319 ++cur_list;
michael@0 320 if (cur_list->n_type == N_ENDM)
michael@0 321 RecalculateOffset(cur_list, stabstr);
michael@0 322 if (cur_list->n_type != N_ESYM && cur_list->n_type != N_ISYM &&
michael@0 323 cur_list->n_type != N_FUN) {
michael@0 324 // Stack parameter size.
michael@0 325 cur_list += LoadStackParamSize(cur_list, list_end, &func_info);
michael@0 326 // Line info.
michael@0 327 cur_list += LoadLineInfo(cur_list, list_end, &func_info);
michael@0 328 }
michael@0 329 if (cur_list < list_end && cur_list->n_type == N_ENDM)
michael@0 330 RecalculateOffset(cur_list, stabstr);
michael@0 331 // Functions in this module should have address bigger than the module
michael@0 332 // starting address.
michael@0 333 //
michael@0 334 // These two values are always 0 with Sun CC.
michael@0 335 // TODO(Alfred): Get the correct value or remove the condition statement.
michael@0 336 if (func_info.addr >= source_file_info->addr) {
michael@0 337 source_file_info->func_info.push_back(func_info);
michael@0 338 }
michael@0 339 }
michael@0 340 }
michael@0 341 return cur_list - list;
michael@0 342 }
michael@0 343
michael@0 344 // Compute size and rva information based on symbols loaded from stab section.
michael@0 345 bool ComputeSizeAndRVA(struct SymbolInfo *symbols) {
michael@0 346 std::vector<struct SourceFileInfo> *sorted_files =
michael@0 347 &(symbols->source_file_info);
michael@0 348 SymbolMap *symbol_entries = &(symbols->symbol_entries);
michael@0 349 for (size_t i = 0; i < sorted_files->size(); ++i) {
michael@0 350 struct SourceFileInfo &source_file = (*sorted_files)[i];
michael@0 351 std::vector<struct FuncInfo> *sorted_functions = &(source_file.func_info);
michael@0 352 int func_size = sorted_functions->size();
michael@0 353
michael@0 354 for (size_t j = 0; j < func_size; ++j) {
michael@0 355 struct FuncInfo &func_info = (*sorted_functions)[j];
michael@0 356 int line_count = func_info.line_info.size();
michael@0 357
michael@0 358 // Discard the ending part of the name.
michael@0 359 std::string func_name(func_info.name);
michael@0 360 std::string::size_type last_colon = func_name.find_first_of(':');
michael@0 361 if (last_colon != std::string::npos)
michael@0 362 func_name = func_name.substr(0, last_colon);
michael@0 363
michael@0 364 // Fine the symbol offset from the loading address and size by name.
michael@0 365 SymbolMap::const_iterator it = symbol_entries->find(func_name.c_str());
michael@0 366 if (it->second) {
michael@0 367 func_info.rva_to_base = it->second->offset;
michael@0 368 func_info.size = (line_count == 0) ? 0 : it->second->size;
michael@0 369 } else {
michael@0 370 func_info.rva_to_base = 0;
michael@0 371 func_info.size = 0;
michael@0 372 }
michael@0 373
michael@0 374 // Compute function and line size.
michael@0 375 for (size_t k = 0; k < line_count; ++k) {
michael@0 376 struct LineInfo &line_info = func_info.line_info[k];
michael@0 377
michael@0 378 line_info.rva_to_base = line_info.rva_to_func + func_info.rva_to_base;
michael@0 379 if (k == line_count - 1) {
michael@0 380 line_info.size = func_info.size - line_info.rva_to_func;
michael@0 381 } else {
michael@0 382 struct LineInfo &next_line = func_info.line_info[k + 1];
michael@0 383 line_info.size = next_line.rva_to_func - line_info.rva_to_func;
michael@0 384 }
michael@0 385 } // for each line.
michael@0 386 } // for each function.
michael@0 387 } // for each source file.
michael@0 388 for (SymbolMap::iterator it = symbol_entries->begin();
michael@0 389 it != symbol_entries->end(); ++it) {
michael@0 390 free(it->second);
michael@0 391 }
michael@0 392 return true;
michael@0 393 }
michael@0 394
michael@0 395 bool LoadAllSymbols(const GElf_Shdr *stab_section,
michael@0 396 const GElf_Shdr *stabstr_section,
michael@0 397 GElf_Word base,
michael@0 398 struct SymbolInfo *symbols) {
michael@0 399 if (stab_section == NULL || stabstr_section == NULL)
michael@0 400 return false;
michael@0 401
michael@0 402 char *stabstr =
michael@0 403 reinterpret_cast<char *>(stabstr_section->sh_offset + base);
michael@0 404 struct slist *lists =
michael@0 405 reinterpret_cast<struct slist *>(stab_section->sh_offset + base);
michael@0 406 int nstab = stab_section->sh_size / sizeof(struct slist);
michael@0 407 int source_id = 0;
michael@0 408
michael@0 409 // First pass, load all symbols from the object file.
michael@0 410 for (int i = 0; i < nstab; ) {
michael@0 411 int step = 1;
michael@0 412 struct slist *cur_list = lists + i;
michael@0 413 if (cur_list->n_type == N_SO) {
michael@0 414 // FUNC <address> <size> <param_stack_size> <function>
michael@0 415 struct SourceFileInfo source_file_info;
michael@0 416 source_file_info.name = stabstr + cur_list->n_strx + stringOffset;
michael@0 417 // The n_value field is always 0 from stab generated by Sun CC.
michael@0 418 // TODO(Alfred): Find the correct value.
michael@0 419 source_file_info.addr = cur_list->n_value;
michael@0 420 if (strchr(source_file_info.name, '.'))
michael@0 421 source_file_info.source_id = source_id++;
michael@0 422 else
michael@0 423 source_file_info.source_id = -1;
michael@0 424 step = LoadFuncSymbols(cur_list, lists + nstab - 1, stabstr,
michael@0 425 base, &source_file_info);
michael@0 426 symbols->source_file_info.push_back(source_file_info);
michael@0 427 }
michael@0 428 i += step;
michael@0 429 }
michael@0 430 // Second pass, compute the size of functions and lines.
michael@0 431 return ComputeSizeAndRVA(symbols);
michael@0 432 }
michael@0 433
michael@0 434 bool LoadSymbols(Elf *elf, GElf_Ehdr *elf_header, struct SymbolInfo *symbols,
michael@0 435 void *obj_base) {
michael@0 436 GElf_Word base = reinterpret_cast<GElf_Word>(obj_base);
michael@0 437
michael@0 438 const GElf_Shdr *sections =
michael@0 439 reinterpret_cast<GElf_Shdr *>(elf_header->e_shoff + base);
michael@0 440 GElf_Shdr stab_section;
michael@0 441 if (!FindSectionByName(elf, kStabName, elf_header->e_shstrndx,
michael@0 442 &stab_section)) {
michael@0 443 fprintf(stderr, "Stab section not found.\n");
michael@0 444 return false;
michael@0 445 }
michael@0 446 GElf_Shdr stabstr_section;
michael@0 447 if (!FindSectionByName(elf, kStabStrName, elf_header->e_shstrndx,
michael@0 448 &stabstr_section)) {
michael@0 449 fprintf(stderr, "Stabstr section not found.\n");
michael@0 450 return false;
michael@0 451 }
michael@0 452 GElf_Shdr symtab_section;
michael@0 453 if (!FindSectionByName(elf, kSymtabName, elf_header->e_shstrndx,
michael@0 454 &symtab_section)) {
michael@0 455 fprintf(stderr, "Symtab section not found.\n");
michael@0 456 return false;
michael@0 457 }
michael@0 458 GElf_Shdr strtab_section;
michael@0 459 if (!FindSectionByName(elf, kStrtabName, elf_header->e_shstrndx,
michael@0 460 &strtab_section)) {
michael@0 461 fprintf(stderr, "Strtab section not found.\n");
michael@0 462 return false;
michael@0 463 }
michael@0 464
michael@0 465 Elf_Sym *symbol = (Elf_Sym *)((char *)base + symtab_section.sh_offset);
michael@0 466 for (int i = 0; i < symtab_section.sh_size/symtab_section.sh_entsize; ++i) {
michael@0 467 struct SymbolEntry *symbol_entry =
michael@0 468 (struct SymbolEntry *)malloc(sizeof(struct SymbolEntry));
michael@0 469 const char *name = reinterpret_cast<char *>(
michael@0 470 strtab_section.sh_offset + (GElf_Word)base + symbol->st_name);
michael@0 471 symbol_entry->offset = symbol->st_value;
michael@0 472 symbol_entry->size = symbol->st_size;
michael@0 473 symbols->symbol_entries.insert(make_pair(name, symbol_entry));
michael@0 474 ++symbol;
michael@0 475 }
michael@0 476
michael@0 477
michael@0 478 // Load symbols.
michael@0 479 return LoadAllSymbols(&stab_section, &stabstr_section, base, symbols);
michael@0 480 }
michael@0 481
michael@0 482 bool WriteModuleInfo(int fd, GElf_Half arch, const std::string &obj_file) {
michael@0 483 const char *arch_name = NULL;
michael@0 484 if (arch == EM_386)
michael@0 485 arch_name = "x86";
michael@0 486 else if (arch == EM_X86_64)
michael@0 487 arch_name = "x86_64";
michael@0 488 else if (arch == EM_SPARC32PLUS)
michael@0 489 arch_name = "SPARC_32+";
michael@0 490 else {
michael@0 491 printf("Please add more ARCH support\n");
michael@0 492 return false;
michael@0 493 }
michael@0 494
michael@0 495 unsigned char identifier[16];
michael@0 496 google_breakpad::FileID file_id(obj_file.c_str());
michael@0 497 if (file_id.ElfFileIdentifier(identifier)) {
michael@0 498 char identifier_str[40];
michael@0 499 file_id.ConvertIdentifierToString(identifier,
michael@0 500 identifier_str, sizeof(identifier_str));
michael@0 501 std::string filename = obj_file;
michael@0 502 size_t slash_pos = obj_file.find_last_of("/");
michael@0 503 if (slash_pos != std::string::npos)
michael@0 504 filename = obj_file.substr(slash_pos + 1);
michael@0 505 return WriteFormat(fd, "MODULE solaris %s %s %s\n", arch_name,
michael@0 506 identifier_str, filename.c_str());
michael@0 507 }
michael@0 508 return false;
michael@0 509 }
michael@0 510
michael@0 511 bool WriteSourceFileInfo(int fd, const struct SymbolInfo &symbols) {
michael@0 512 for (size_t i = 0; i < symbols.source_file_info.size(); ++i) {
michael@0 513 if (symbols.source_file_info[i].source_id != -1) {
michael@0 514 const char *name = symbols.source_file_info[i].name;
michael@0 515 if (!WriteFormat(fd, "FILE %d %s\n",
michael@0 516 symbols.source_file_info[i].source_id, name))
michael@0 517 return false;
michael@0 518 }
michael@0 519 }
michael@0 520 return true;
michael@0 521 }
michael@0 522
michael@0 523 bool WriteOneFunction(int fd, int source_id,
michael@0 524 const struct FuncInfo &func_info){
michael@0 525 // Discard the ending part of the name.
michael@0 526 std::string func_name(func_info.name);
michael@0 527 std::string::size_type last_colon = func_name.find_last_of(':');
michael@0 528 if (last_colon != std::string::npos)
michael@0 529 func_name = func_name.substr(0, last_colon);
michael@0 530 func_name = Demangle(func_name.c_str());
michael@0 531
michael@0 532 if (func_info.size <= 0)
michael@0 533 return true;
michael@0 534
michael@0 535 // rva_to_base could be unsigned long(32 bit) or unsigned long long(64 bit).
michael@0 536 if (WriteFormat(fd, "FUNC %llx %x %d %s\n",
michael@0 537 (long long)func_info.rva_to_base,
michael@0 538 func_info.size,
michael@0 539 func_info.stack_param_size,
michael@0 540 func_name.c_str())) {
michael@0 541 for (size_t i = 0; i < func_info.line_info.size(); ++i) {
michael@0 542 const struct LineInfo &line_info = func_info.line_info[i];
michael@0 543 if (line_info.line_num == 0)
michael@0 544 return true;
michael@0 545 if (!WriteFormat(fd, "%llx %x %d %d\n",
michael@0 546 (long long)line_info.rva_to_base,
michael@0 547 line_info.size,
michael@0 548 line_info.line_num,
michael@0 549 source_id))
michael@0 550 return false;
michael@0 551 }
michael@0 552 return true;
michael@0 553 }
michael@0 554 return false;
michael@0 555 }
michael@0 556
michael@0 557 bool WriteFunctionInfo(int fd, const struct SymbolInfo &symbols) {
michael@0 558 for (size_t i = 0; i < symbols.source_file_info.size(); ++i) {
michael@0 559 const struct SourceFileInfo &file_info = symbols.source_file_info[i];
michael@0 560 for (size_t j = 0; j < file_info.func_info.size(); ++j) {
michael@0 561 const struct FuncInfo &func_info = file_info.func_info[j];
michael@0 562 if (!WriteOneFunction(fd, file_info.source_id, func_info))
michael@0 563 return false;
michael@0 564 }
michael@0 565 }
michael@0 566 return true;
michael@0 567 }
michael@0 568
michael@0 569 bool DumpStabSymbols(int fd, const struct SymbolInfo &symbols) {
michael@0 570 return WriteSourceFileInfo(fd, symbols) &&
michael@0 571 WriteFunctionInfo(fd, symbols);
michael@0 572 }
michael@0 573
michael@0 574 //
michael@0 575 // FDWrapper
michael@0 576 //
michael@0 577 // Wrapper class to make sure opened file is closed.
michael@0 578 //
michael@0 579 class FDWrapper {
michael@0 580 public:
michael@0 581 explicit FDWrapper(int fd) :
michael@0 582 fd_(fd) {
michael@0 583 }
michael@0 584 ~FDWrapper() {
michael@0 585 if (fd_ != -1)
michael@0 586 close(fd_);
michael@0 587 }
michael@0 588 int get() {
michael@0 589 return fd_;
michael@0 590 }
michael@0 591 int release() {
michael@0 592 int fd = fd_;
michael@0 593 fd_ = -1;
michael@0 594 return fd;
michael@0 595 }
michael@0 596 private:
michael@0 597 int fd_;
michael@0 598 };
michael@0 599
michael@0 600 //
michael@0 601 // MmapWrapper
michael@0 602 //
michael@0 603 // Wrapper class to make sure mapped regions are unmapped.
michael@0 604 //
michael@0 605 class MmapWrapper {
michael@0 606 public:
michael@0 607 MmapWrapper(void *mapped_address, size_t mapped_size) :
michael@0 608 base_(mapped_address), size_(mapped_size) {
michael@0 609 }
michael@0 610 ~MmapWrapper() {
michael@0 611 if (base_ != NULL) {
michael@0 612 assert(size_ > 0);
michael@0 613 munmap((char *)base_, size_);
michael@0 614 }
michael@0 615 }
michael@0 616 void release() {
michael@0 617 base_ = NULL;
michael@0 618 size_ = 0;
michael@0 619 }
michael@0 620
michael@0 621 private:
michael@0 622 void *base_;
michael@0 623 size_t size_;
michael@0 624 };
michael@0 625
michael@0 626 } // namespace
michael@0 627
michael@0 628 namespace google_breakpad {
michael@0 629
michael@0 630 class AutoElfEnder {
michael@0 631 public:
michael@0 632 AutoElfEnder(Elf *elf) : elf_(elf) {}
michael@0 633 ~AutoElfEnder() { if (elf_) elf_end(elf_); }
michael@0 634 private:
michael@0 635 Elf *elf_;
michael@0 636 };
michael@0 637
michael@0 638
michael@0 639 bool DumpSymbols::WriteSymbolFile(const std::string &obj_file, int sym_fd) {
michael@0 640 if (elf_version(EV_CURRENT) == EV_NONE) {
michael@0 641 fprintf(stderr, "elf_version() failed: %s\n", elf_errmsg(0));
michael@0 642 return false;
michael@0 643 }
michael@0 644
michael@0 645 int obj_fd = open(obj_file.c_str(), O_RDONLY);
michael@0 646 if (obj_fd < 0)
michael@0 647 return false;
michael@0 648 FDWrapper obj_fd_wrapper(obj_fd);
michael@0 649 struct stat st;
michael@0 650 if (fstat(obj_fd, &st) != 0 && st.st_size <= 0)
michael@0 651 return false;
michael@0 652 void *obj_base = mmap(NULL, st.st_size,
michael@0 653 PROT_READ, MAP_PRIVATE, obj_fd, 0);
michael@0 654 if (obj_base == MAP_FAILED)
michael@0 655 return false;
michael@0 656 MmapWrapper map_wrapper(obj_base, st.st_size);
michael@0 657 GElf_Ehdr elf_header;
michael@0 658 Elf *elf = elf_begin(obj_fd, ELF_C_READ, NULL);
michael@0 659 AutoElfEnder elfEnder(elf);
michael@0 660
michael@0 661 if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr *)NULL) {
michael@0 662 fprintf(stderr, "failed to read elf header: %s\n", elf_errmsg(-1));
michael@0 663 return false;
michael@0 664 }
michael@0 665
michael@0 666 if (!IsValidElf(&elf_header)) {
michael@0 667 fprintf(stderr, "header magic doesn't match\n");
michael@0 668 return false;
michael@0 669 }
michael@0 670 struct SymbolInfo symbols;
michael@0 671 if (!LoadSymbols(elf, &elf_header, &symbols, obj_base))
michael@0 672 return false;
michael@0 673 // Write to symbol file.
michael@0 674 if (WriteModuleInfo(sym_fd, elf_header.e_machine, obj_file) &&
michael@0 675 DumpStabSymbols(sym_fd, symbols))
michael@0 676 return true;
michael@0 677
michael@0 678 return false;
michael@0 679 }
michael@0 680
michael@0 681 } // namespace google_breakpad

mercurial