michael@0: // Copyright (c) 2010 Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // dwarf_line_to_module.cc: Implementation of DwarfLineToModule class. michael@0: // See dwarf_line_to_module.h for details. michael@0: michael@0: #include michael@0: michael@0: #include "common/dwarf_line_to_module.h" michael@0: #include "common/using_std_string.h" michael@0: #include "common/logging.h" michael@0: michael@0: // Trying to support Windows paths in a reasonable way adds a lot of michael@0: // variations to test; it would be better to just put off dealing with michael@0: // it until we actually have to deal with DWARF on Windows. michael@0: michael@0: // Return true if PATH is an absolute path, false if it is relative. michael@0: static bool PathIsAbsolute(const string &path) { michael@0: return (path.size() >= 1 && path[0] == '/'); michael@0: } michael@0: michael@0: static bool HasTrailingSlash(const string &path) { michael@0: return (path.size() >= 1 && path[path.size() - 1] == '/'); michael@0: } michael@0: michael@0: // If PATH is an absolute path, return PATH. If PATH is a relative path, michael@0: // treat it as relative to BASE and return the combined path. michael@0: static string ExpandPath(const string &path, michael@0: const string &base) { michael@0: if (PathIsAbsolute(path) || base.empty()) michael@0: return path; michael@0: return base + (HasTrailingSlash(base) ? "" : "/") + path; michael@0: } michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: void DwarfLineToModule::DefineDir(const string &name, uint32 dir_num) { michael@0: // Directory number zero is reserved to mean the compilation michael@0: // directory. Silently ignore attempts to redefine it. michael@0: if (dir_num != 0) michael@0: directories_[dir_num] = ExpandPath(name, compilation_dir_); michael@0: } michael@0: michael@0: void DwarfLineToModule::DefineFile(const string &name, int32 file_num, michael@0: uint32 dir_num, uint64 mod_time, michael@0: uint64 length) { michael@0: if (file_num == -1) michael@0: file_num = ++highest_file_number_; michael@0: else if (file_num > highest_file_number_) michael@0: highest_file_number_ = file_num; michael@0: michael@0: string dir_name; michael@0: if (dir_num == 0) { michael@0: // Directory number zero is the compilation directory, and is stored as michael@0: // an attribute on the compilation unit, rather than in the program table. michael@0: dir_name = compilation_dir_; michael@0: } else { michael@0: DirectoryTable::const_iterator directory_it = directories_.find(dir_num); michael@0: if (directory_it != directories_.end()) { michael@0: dir_name = directory_it->second; michael@0: } else { michael@0: if (!warned_bad_directory_number_) { michael@0: BPLOG(INFO) << "warning: DWARF line number data refers to undefined" michael@0: << " directory numbers"; michael@0: warned_bad_directory_number_ = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: string full_name = ExpandPath(name, dir_name); michael@0: michael@0: // Find a Module::File object of the given name, and add it to the michael@0: // file table. michael@0: files_[file_num] = module_->FindFile(full_name); michael@0: } michael@0: michael@0: void DwarfLineToModule::AddLine(uint64 address, uint64 length, michael@0: uint32 file_num, uint32 line_num, michael@0: uint32 column_num) { michael@0: if (length == 0) michael@0: return; michael@0: michael@0: // Clip lines not to extend beyond the end of the address space. michael@0: if (address + length < address) michael@0: length = -address; michael@0: michael@0: // Should we omit this line? (See the comments for omitted_line_end_.) michael@0: if (address == 0 || address == omitted_line_end_) { michael@0: omitted_line_end_ = address + length; michael@0: return; michael@0: } else { michael@0: omitted_line_end_ = 0; michael@0: } michael@0: michael@0: // Find the source file being referred to. michael@0: Module::File *file = files_[file_num]; michael@0: if (!file) { michael@0: if (!warned_bad_file_number_) { michael@0: BPLOG(INFO) << "warning: DWARF line number data refers to " michael@0: << "undefined file numbers"; michael@0: warned_bad_file_number_ = true; michael@0: } michael@0: return; michael@0: } michael@0: Module::Line line; michael@0: line.address = address; michael@0: // We set the size when we get the next line or the EndSequence call. michael@0: line.size = length; michael@0: line.file = file; michael@0: line.number = line_num; michael@0: lines_->push_back(line); michael@0: } michael@0: michael@0: } // namespace google_breakpad