1 /*
2  * Copyright (c) 2011, 2015, 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 #include "memory/allocation.inline.hpp"
30 
~ElfDecoder()31 ElfDecoder::~ElfDecoder() {
32   if (_opened_elf_files != NULL) {
33     delete _opened_elf_files;
34     _opened_elf_files = NULL;
35   }
36 }
37 
decode(address addr,char * buf,int buflen,int * offset,const char * filepath,bool demangle_name)38 bool ElfDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* filepath, bool demangle_name) {
39   assert(filepath, "null file path");
40   assert(buf != NULL && buflen > 0, "Invalid buffer");
41   if (has_error()) return false;
42   ElfFile* file = get_elf_file(filepath);
43   if (file == NULL) {
44     return false;
45   }
46 
47   if (!file->decode(addr, buf, buflen, offset)) {
48     return false;
49   }
50   if (demangle_name && (buf[0] != '\0')) {
51     demangle(buf, buf, buflen);
52   }
53   return true;
54 }
55 
get_elf_file(const char * filepath)56 ElfFile* ElfDecoder::get_elf_file(const char* filepath) {
57   ElfFile* file;
58 
59   file = _opened_elf_files;
60   while (file != NULL) {
61     if (file->same_elf_file(filepath)) {
62       return file;
63     }
64     file = file->next();
65   }
66 
67   file = new (std::nothrow)ElfFile(filepath);
68   if (file != NULL) {
69     if (_opened_elf_files != NULL) {
70       file->set_next(_opened_elf_files);
71     }
72     _opened_elf_files = file;
73   }
74 
75   return file;
76 }
77 #endif // !_WINDOWS && !__APPLE__
78