/ src / common / dwarf_line_to_module.cc
dwarf_line_to_module.cc
  1  // Copyright 2010 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
 30  
 31  // dwarf_line_to_module.cc: Implementation of DwarfLineToModule class.
 32  // See dwarf_line_to_module.h for details. 
 33  
 34  #ifdef HAVE_CONFIG_H
 35  #include <config.h>  // Must come first
 36  #endif
 37  
 38  #include <stdio.h>
 39  
 40  #include <string>
 41  
 42  #include "common/dwarf_line_to_module.h"
 43  #include "common/using_std_string.h"
 44  
 45  // Trying to support Windows paths in a reasonable way adds a lot of
 46  // variations to test; it would be better to just put off dealing with
 47  // it until we actually have to deal with DWARF on Windows.
 48  
 49  // Return true if PATH is an absolute path, false if it is relative.
 50  static bool PathIsAbsolute(const string& path) {
 51    return (path.size() >= 1 && path[0] == '/');
 52  }
 53  
 54  static bool HasTrailingSlash(const string& path) {
 55    return (path.size() >= 1 && path[path.size() - 1] == '/');
 56  }
 57  
 58  // If PATH is an absolute path, return PATH.  If PATH is a relative path,
 59  // treat it as relative to BASE and return the combined path.
 60  static string ExpandPath(const string& path,
 61                           const string& base) {
 62    if (PathIsAbsolute(path) || base.empty())
 63      return path;
 64    return base + (HasTrailingSlash(base) ? "" : "/") + path;
 65  }
 66  
 67  namespace google_breakpad {
 68  
 69  void DwarfLineToModule::DefineDir(const string& name, uint32_t dir_num) {
 70    // Directory number zero is reserved to mean the compilation
 71    // directory. Silently ignore attempts to redefine it.
 72    if (dir_num != 0)
 73      directories_[dir_num] = ExpandPath(name, compilation_dir_);
 74  }
 75  
 76  void DwarfLineToModule::DefineFile(const string& name, int32_t file_num,
 77                                     uint32_t dir_num, uint64_t mod_time,
 78                                     uint64_t length) {
 79    if (file_num == -1)
 80      file_num = ++highest_file_number_;
 81    else if (file_num > highest_file_number_)
 82      highest_file_number_ = file_num;
 83  
 84    string dir_name;
 85    if (dir_num == 0) {
 86      // Directory number zero is the compilation directory, and is stored as
 87      // an attribute on the compilation unit, rather than in the program table.
 88      dir_name = compilation_dir_;
 89    } else {
 90      DirectoryTable::const_iterator directory_it = directories_.find(dir_num);
 91      if (directory_it != directories_.end()) {
 92        dir_name = directory_it->second;
 93      } else {
 94        if (!warned_bad_directory_number_) {
 95          fprintf(stderr, "warning: DWARF line number data refers to undefined"
 96                  " directory numbers\n");
 97          warned_bad_directory_number_ = true;
 98        }
 99      }
100    }
101  
102    string full_name = ExpandPath(name, dir_name);
103  
104    // Find a Module::File object of the given name, and add it to the
105    // file table.
106    (*files_)[file_num] = module_->FindFile(full_name);
107  }
108  
109  void DwarfLineToModule::AddLine(uint64_t address, uint64_t length,
110                                  uint32_t file_num, uint32_t line_num,
111                                  uint32_t column_num) {
112    if (length == 0)
113      return;
114  
115    // Clip lines not to extend beyond the end of the address space.
116    if (address + length < address)
117      length = -address;
118  
119    // Should we omit this line? (See the comments for omitted_line_end_.)
120    if (address == 0 || address == omitted_line_end_) {
121      omitted_line_end_ = address + length;
122      return;
123    } else {
124      omitted_line_end_ = 0;
125    }
126  
127    // Find the source file being referred to.
128    Module::File *file = (*files_)[file_num];
129    if (!file) {
130      if (!warned_bad_file_number_) {
131        fprintf(stderr, "warning: DWARF line number data refers to "
132                "undefined file numbers\n");
133        warned_bad_file_number_ = true;
134      }
135      return;
136    }
137    Module::Line line;
138    line.address = address;
139    // We set the size when we get the next line or the EndSequence call.
140    line.size = length;
141    line.file = file;
142    line.number = line_num;
143    lines_->push_back(line);
144  }
145  
146  } // namespace google_breakpad