1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #if !defined(_WINDOWS) && !defined(__APPLE__)
  28 #include "decoder_elf.hpp"
  29 
  30 ElfDecoder::~ElfDecoder() {
  31   if (_opened_elf_files != NULL) {
  32     delete _opened_elf_files;
  33     _opened_elf_files = NULL;
  34   }
  35 }
  36 
  37 bool ElfDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* filepath) {
  38   assert(filepath, "null file path");
  39   assert(buf != NULL && buflen > 0, "Invalid buffer");
  40   if (has_error()) return false;
  41   ElfFile* file = get_elf_file(filepath);
  42   if (file == NULL) {
  43     return false;
  44   }
  45 
  46   if (!file->decode(addr, buf, buflen, offset)) {
  47     return false;
  48   }
  49   if (buf[0] != '\0') {
  50     demangle(buf, buf, buflen);
  51   } 
  52   return true;
  53 }
  54 
  55 ElfFile* ElfDecoder::get_elf_file(const char* filepath) {
  56   ElfFile* file;
  57 
  58   file = _opened_elf_files;
  59   while (file != NULL) {
  60     if (file->same_elf_file(filepath)) {
  61       return file;
  62     }
  63     file = file->next();
  64   }
  65 
  66   file = new (std::nothrow)ElfFile(filepath);
  67   if (file != NULL) {
  68     if (_opened_elf_files != NULL) {
  69       file->set_next(_opened_elf_files);
  70     }
  71     _opened_elf_files = file;
  72   }
  73 
  74   return file;
  75 }
  76 #endif