~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to thirdparty/breakpad/common/linux/elf_core_dump.h

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2011, Google Inc.
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
//
 
8
//     * Redistributions of source code must retain the above copyright
 
9
// notice, this list of conditions and the following disclaimer.
 
10
//     * Redistributions in binary form must reproduce the above
 
11
// copyright notice, this list of conditions and the following disclaimer
 
12
// in the documentation and/or other materials provided with the
 
13
// distribution.
 
14
//     * Neither the name of Google Inc. nor the names of its
 
15
// contributors may be used to endorse or promote products derived from
 
16
// this software without specific prior written permission.
 
17
//
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
// elf_core_dump.h: Define the google_breakpad::ElfCoreDump class, which
 
31
// encapsulates an ELF core dump file mapped into memory.
 
32
 
 
33
#ifndef COMMON_LINUX_ELF_CORE_DUMP_H_
 
34
#define COMMON_LINUX_ELF_CORE_DUMP_H_
 
35
 
 
36
#include <elf.h>
 
37
#if !defined(__ANDROID__)
 
38
#include <link.h>
 
39
#endif
 
40
#include <stddef.h>
 
41
 
 
42
#include "common/memory_range.h"
 
43
#if defined(__ANDROID__)
 
44
#include "common/linux/android_link.h"
 
45
#endif
 
46
 
 
47
namespace google_breakpad {
 
48
 
 
49
// A class encapsulating an ELF core dump file mapped into memory, which
 
50
// provides methods for accessing program headers and the note section.
 
51
class ElfCoreDump {
 
52
 public:
 
53
  // ELF types based on the value of __WORDSIZE.
 
54
  typedef ElfW(Ehdr) Ehdr;
 
55
  typedef ElfW(Nhdr) Nhdr;
 
56
  typedef ElfW(Phdr) Phdr;
 
57
  typedef ElfW(Word) Word;
 
58
  typedef ElfW(Addr) Addr;
 
59
#if __WORDSIZE == 32
 
60
  static const int kClass = ELFCLASS32;
 
61
#elif __WORDSIZE == 64
 
62
  static const int kClass = ELFCLASS64;
 
63
#else
 
64
#error "Unsupported __WORDSIZE for ElfCoreDump."
 
65
#endif
 
66
 
 
67
  // A class encapsulating the note content in a core dump, which provides
 
68
  // methods for accessing the name and description of a note.
 
69
  class Note {
 
70
   public:
 
71
    Note();
 
72
 
 
73
    // Constructor that takes the note content from |content|.
 
74
    explicit Note(const MemoryRange& content);
 
75
 
 
76
    // Returns true if this note is valid, i,e. a note header is found in
 
77
    // |content_|, or false otherwise.
 
78
    bool IsValid() const;
 
79
 
 
80
    // Returns the note header, or NULL if no note header is found in
 
81
    // |content_|.
 
82
    const Nhdr* GetHeader() const;
 
83
 
 
84
    // Returns the note type, or 0 if no note header is found in |content_|.
 
85
    Word GetType() const;
 
86
 
 
87
    // Returns a memory range covering the note name, or an empty range
 
88
    // if no valid note name is found in |content_|.
 
89
    MemoryRange GetName() const;
 
90
 
 
91
    // Returns a memory range covering the note description, or an empty
 
92
    // range if no valid note description is found in |content_|.
 
93
    MemoryRange GetDescription() const;
 
94
 
 
95
    // Returns the note following this note, or an empty note if no valid
 
96
    // note is found after this note.
 
97
    Note GetNextNote() const;
 
98
 
 
99
   private:
 
100
    // Returns the size in bytes round up to the word alignment, specified
 
101
    // for the note section, of a given size in bytes.
 
102
    static size_t AlignedSize(size_t size);
 
103
 
 
104
    // Note content.
 
105
    MemoryRange content_;
 
106
  };
 
107
 
 
108
  ElfCoreDump();
 
109
 
 
110
  // Constructor that takes the core dump content from |content|.
 
111
  explicit ElfCoreDump(const MemoryRange& content);
 
112
 
 
113
  // Sets the core dump content to |content|.
 
114
  void SetContent(const MemoryRange& content);
 
115
 
 
116
  // Returns true if a valid ELF header in the core dump, or false otherwise.
 
117
  bool IsValid() const;
 
118
 
 
119
  // Returns the ELF header in the core dump, or NULL if no ELF header
 
120
  // is found in |content_|.
 
121
  const Ehdr* GetHeader() const;
 
122
 
 
123
  // Returns the |index|-th program header in the core dump, or NULL if no
 
124
  // ELF header is found in |content_| or |index| is out of bounds.
 
125
  const Phdr* GetProgramHeader(unsigned index) const;
 
126
 
 
127
  // Returns the first program header of |type| in the core dump, or NULL if
 
128
  // no ELF header is found in |content_| or no program header of |type| is
 
129
  // found.
 
130
  const Phdr* GetFirstProgramHeaderOfType(Word type) const;
 
131
 
 
132
  // Returns the number of program headers in the core dump, or 0 if no
 
133
  // ELF header is found in |content_|.
 
134
  unsigned GetProgramHeaderCount() const;
 
135
 
 
136
  // Copies |length| bytes of data starting at |virtual_address| in the core
 
137
  // dump to |buffer|. |buffer| should be a valid pointer to a buffer of at
 
138
  // least |length| bytes. Returns true if the data to be copied is found in
 
139
  // the core dump, or false otherwise.
 
140
  bool CopyData(void* buffer, Addr virtual_address, size_t length);
 
141
 
 
142
  // Returns the first note found in the note section of the core dump, or
 
143
  // an empty note if no note is found.
 
144
  Note GetFirstNote() const;
 
145
 
 
146
 private:
 
147
  // Core dump content.
 
148
  MemoryRange content_;
 
149
};
 
150
 
 
151
}  // namespace google_breakpad
 
152
 
 
153
#endif  // COMMON_LINUX_ELF_CORE_DUMP_H_