simple_symbol_supplier.cc revision a14ef803d70614a01725a269acb509027fe84942
1c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// Copyright (c) 2006, Google Inc.
2c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// All rights reserved.
3c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//
4c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// Redistribution and use in source and binary forms, with or without
5c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// modification, are permitted provided that the following conditions are
6c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// met:
7c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//
8c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//     * Redistributions of source code must retain the above copyright
9c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// notice, this list of conditions and the following disclaimer.
10c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//     * Redistributions in binary form must reproduce the above
11c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// copyright notice, this list of conditions and the following disclaimer
12c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// in the documentation and/or other materials provided with the
13c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// distribution.
14c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//     * Neither the name of Google Inc. nor the names of its
15c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// contributors may be used to endorse or promote products derived from
16c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// this software without specific prior written permission.
17c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//
18c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
30c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// simple_symbol_supplier.cc: A simple SymbolSupplier implementation
31c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//
32c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// See simple_symbol_supplier.h for documentation.
33c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai//
34c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai// Author: Mark Mentovai
35c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
36a14ef803d70614a01725a269acb509027fe84942mmentovai#include <sys/types.h>
37a14ef803d70614a01725a269acb509027fe84942mmentovai#include <sys/stat.h>
38a14ef803d70614a01725a269acb509027fe84942mmentovai
39f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner#include <cassert>
40f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner
41c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai#include "processor/simple_symbol_supplier.h"
42db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai#include "google_airbag/processor/code_module.h"
4397d392dc4b60f0099cd7ad8c8a5f06581a532392mmentovai#include "google_airbag/processor/system_info.h"
44c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai#include "processor/pathname_stripper.h"
45c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
46c34850a2023442a22fa653ab8546e72738ed2dd4mmentovainamespace google_airbag {
47c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
48a14ef803d70614a01725a269acb509027fe84942mmentovaistatic bool file_exists(const string &file_name) {
49a14ef803d70614a01725a269acb509027fe84942mmentovai  struct stat sb;
50a14ef803d70614a01725a269acb509027fe84942mmentovai  return stat(file_name.c_str(), &sb) == 0;
51a14ef803d70614a01725a269acb509027fe84942mmentovai}
52a14ef803d70614a01725a269acb509027fe84942mmentovai
53a14ef803d70614a01725a269acb509027fe84942mmentovaiSymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile(
54a14ef803d70614a01725a269acb509027fe84942mmentovai    const CodeModule *module, const SystemInfo *system_info,
55a14ef803d70614a01725a269acb509027fe84942mmentovai    string *symbol_file) {
56a14ef803d70614a01725a269acb509027fe84942mmentovai  assert(symbol_file);
57a14ef803d70614a01725a269acb509027fe84942mmentovai  for (unsigned int path_index = 0; path_index < paths_.size(); ++path_index) {
58a14ef803d70614a01725a269acb509027fe84942mmentovai    SymbolResult result;
59a14ef803d70614a01725a269acb509027fe84942mmentovai    if ((result = GetSymbolFileAtPath(module, system_info, paths_[path_index],
60a14ef803d70614a01725a269acb509027fe84942mmentovai                                      symbol_file)) != NOT_FOUND) {
61a14ef803d70614a01725a269acb509027fe84942mmentovai      return result;
62a14ef803d70614a01725a269acb509027fe84942mmentovai    }
63a14ef803d70614a01725a269acb509027fe84942mmentovai  }
64a14ef803d70614a01725a269acb509027fe84942mmentovai  return NOT_FOUND;
65a14ef803d70614a01725a269acb509027fe84942mmentovai}
66a14ef803d70614a01725a269acb509027fe84942mmentovai
67f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3brynerSymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFileAtPath(
6897d392dc4b60f0099cd7ad8c8a5f06581a532392mmentovai    const CodeModule *module, const SystemInfo *system_info,
6997d392dc4b60f0099cd7ad8c8a5f06581a532392mmentovai    const string &root_path, string *symbol_file) {
70f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner  assert(symbol_file);
71c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  if (!module)
72f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner    return NOT_FOUND;
73c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
74c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  // Start with the base path.
757573d1dd4412171794f76bea833e4ae5f72929f8mmentovai  string path = root_path;
76c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
77db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  // Append the debug (pdb) file name as a directory name.
78c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  path.append("/");
79db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  string debug_file_name = PathnameStripper::File(module->debug_file());
80db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  if (debug_file_name.empty())
81f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner    return NOT_FOUND;
82db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  path.append(debug_file_name);
83c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
84db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  // Append the identifier as a directory name.
85c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  path.append("/");
86db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  string identifier = module->debug_identifier();
87db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  if (identifier.empty())
88f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner    return NOT_FOUND;
89db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  path.append(identifier);
90c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
91db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  // Transform the debug file name into one ending in .sym.  If the existing
92c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  // name ends in .pdb, strip the .pdb.  Otherwise, add .sym to the non-.pdb
93c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  // name.
94c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  path.append("/");
95db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  string debug_file_extension =
96db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai      debug_file_name.substr(debug_file_name.size() - 4);
97db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  transform(debug_file_extension.begin(), debug_file_extension.end(),
98db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai            debug_file_extension.begin(), tolower);
99db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai  if (debug_file_extension == ".pdb") {
100db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai    path.append(debug_file_name.substr(0, debug_file_name.size() - 4));
101c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  } else {
102db3342a10ec30902aa9018b80e1d9a40bd01c487mmentovai    path.append(debug_file_name);
103c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  }
104c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai  path.append(".sym");
105c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
106a14ef803d70614a01725a269acb509027fe84942mmentovai  if (!file_exists(path))
107a14ef803d70614a01725a269acb509027fe84942mmentovai    return NOT_FOUND;
108a14ef803d70614a01725a269acb509027fe84942mmentovai
109f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner  *symbol_file = path;
110f33b8d2d07a057fdd667c2e0db629ba7cbc37cc3bryner  return FOUND;
111c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai}
112c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai
113c34850a2023442a22fa653ab8546e72738ed2dd4mmentovai}  // namespace google_airbag
114