1
// Copyright (c) 2011, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// elf_core_dump.h: Define the google_breakpad::ElfCoreDump class, which
31
// encapsulates an ELF core dump file mapped into memory.
33
#ifndef COMMON_LINUX_ELF_CORE_DUMP_H_
34
#define COMMON_LINUX_ELF_CORE_DUMP_H_
37
#if !defined(__ANDROID__)
42
#include "common/memory_range.h"
43
#if defined(__ANDROID__)
44
#include "common/linux/android_link.h"
47
namespace google_breakpad {
49
// A class encapsulating an ELF core dump file mapped into memory, which
50
// provides methods for accessing program headers and the note section.
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;
60
static const int kClass = ELFCLASS32;
61
#elif __WORDSIZE == 64
62
static const int kClass = ELFCLASS64;
64
#error "Unsupported __WORDSIZE for ElfCoreDump."
67
// A class encapsulating the note content in a core dump, which provides
68
// methods for accessing the name and description of a note.
73
// Constructor that takes the note content from |content|.
74
explicit Note(const MemoryRange& content);
76
// Returns true if this note is valid, i,e. a note header is found in
77
// |content_|, or false otherwise.
80
// Returns the note header, or NULL if no note header is found in
82
const Nhdr* GetHeader() const;
84
// Returns the note type, or 0 if no note header is found in |content_|.
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;
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;
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;
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);
105
MemoryRange content_;
110
// Constructor that takes the core dump content from |content|.
111
explicit ElfCoreDump(const MemoryRange& content);
113
// Sets the core dump content to |content|.
114
void SetContent(const MemoryRange& content);
116
// Returns true if a valid ELF header in the core dump, or false otherwise.
117
bool IsValid() const;
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;
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;
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
130
const Phdr* GetFirstProgramHeaderOfType(Word type) const;
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;
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);
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;
147
// Core dump content.
148
MemoryRange content_;
151
} // namespace google_breakpad
153
#endif // COMMON_LINUX_ELF_CORE_DUMP_H_