diff --git a/src/processor/crash_report_processor.cc b/src/processor/crash_report_processor.cc index 1bb894a4..672ca7f0 100644 --- a/src/processor/crash_report_processor.cc +++ b/src/processor/crash_report_processor.cc @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include -#include #include "google/crash_report.h" #include "google/crash_report_processor.h" #include "processor/minidump.h" @@ -31,12 +28,7 @@ CrashReportProcessor::~CrashReportProcessor() { bool CrashReportProcessor::ProcessReport(CrashReport *report, const string &minidump_file) { - int fd = open(minidump_file.c_str(), O_RDONLY); - if (fd == -1) { - return false; - } - - Minidump dump(fd); + Minidump dump(minidump_file); if (!dump.Read()) { return false; } diff --git a/src/processor/minidump.cc b/src/processor/minidump.cc index 6c4226b3..1a82e0f0 100644 --- a/src/processor/minidump.cc +++ b/src/processor/minidump.cc @@ -19,14 +19,18 @@ // Author: Mark Mentovai +#include #include #include #include #ifdef _WIN32 #include typedef SSIZE_T ssize_t; +#define open _open #define read _read #define lseek _lseek +#else // _WIN32 +#define O_BINARY 0 #endif // _WIN32 #include @@ -1693,11 +1697,12 @@ void MinidumpMiscInfo::Print() { // -Minidump::Minidump(int fd) +Minidump::Minidump(const string& path) : header_(), directory_(NULL), stream_map_(NULL), - fd_(fd), + path_(path), + fd_(-1), swap_(false), valid_(false) { } @@ -1706,6 +1711,25 @@ Minidump::Minidump(int fd) Minidump::~Minidump() { delete directory_; delete stream_map_; + if (fd_ != -1) + close(fd_); +} + + +bool Minidump::Open() { + if (fd_ != -1) { + // The file is already open. Seek to the beginning, which is the position + // the file would be at if it were opened anew. + return SeekSet(0); + } + + // O_BINARY is useful (and defined) on Windows. On other platforms, it's + // useless, and because it's defined as 0 above, harmless. + fd_ = open(path_.c_str(), O_RDONLY | O_BINARY); + if (fd_ == -1) + return false; + + return true; } @@ -1718,6 +1742,9 @@ bool Minidump::Read() { valid_ = false; + if (!Open()) + return false; + if (!ReadBytes(&header_, sizeof(MDRawHeader))) return false; diff --git a/src/processor/minidump.h b/src/processor/minidump.h index 8ad7c9f5..cdc6d39e 100644 --- a/src/processor/minidump.h +++ b/src/processor/minidump.h @@ -561,10 +561,8 @@ class MinidumpMiscInfo : public MinidumpStream { // and provides access to the minidump's top-level stream directory. class Minidump { public: - // fd is a randomly seekable file descriptor that is open and is - // positioned at the beginning of the MDRawHeader structure (byte offset - // 0). - Minidump(int fd); + // path is the pathname of a file containing the minidump. + Minidump(const string& path); ~Minidump(); @@ -652,6 +650,9 @@ class Minidump { template T* GetStream(T** stream); + // Opens the minidump file, or if already open, seeks to the beginning. + bool Open(); + MDRawHeader header_; // The list of streams. @@ -660,7 +661,11 @@ class Minidump { // Access to streams using the stream type as the key. MinidumpStreamMap* stream_map_; + // The pathname of the minidump file to process, set in the constructor. + const string path_; + // The file descriptor for all file I/O. Used by ReadBytes and SeekSet. + // Set based on the |path_| member by Open, which is called by Read. int fd_; // swap_ is true if the minidump file should be byte-swapped. If the diff --git a/src/processor/minidump_dump.cc b/src/processor/minidump_dump.cc index cf707b54..482e5ee1 100644 --- a/src/processor/minidump_dump.cc +++ b/src/processor/minidump_dump.cc @@ -17,22 +17,15 @@ // // Author: Mark Mentovai -#include -#include #include #include -#include -#ifndef _WIN32 -#include -#define O_BINARY 0 -#else // !_WIN32 -#include -#define open _open -#endif // !_WIN32 + +#include #include "processor/minidump.h" +using std::string; using namespace google_airbag; @@ -42,13 +35,7 @@ int main(int argc, char** argv) { exit(1); } - int fd = open(argv[1], O_RDONLY | O_BINARY); - if (fd == -1) { - printf("open failed\n"); - exit(1); - } - - Minidump minidump(fd); + Minidump minidump(argv[1]); if (!minidump.Read()) { printf("minidump.Read() failed\n"); exit(1); diff --git a/src/processor/minidump_stackwalk.cc b/src/processor/minidump_stackwalk.cc index db4437cd..b6049b8b 100644 --- a/src/processor/minidump_stackwalk.cc +++ b/src/processor/minidump_stackwalk.cc @@ -17,23 +17,16 @@ // // Author: Mark Mentovai -#include -#include #include #include -#include -#ifndef _WIN32 -#include -#define O_BINARY 0 -#else // !_WIN32 -#include -#define open _open -#endif // !_WIN32 + +#include #include "processor/minidump.h" #include "processor/stackwalker_x86.h" +using std::string; using namespace google_airbag; @@ -43,13 +36,7 @@ int main(int argc, char** argv) { exit(1); } - int fd = open(argv[1], O_RDONLY | O_BINARY); - if (fd == -1) { - fprintf(stderr, "open failed\n"); - exit(1); - } - - Minidump minidump(fd); + Minidump minidump(argv[1]); if (!minidump.Read()) { fprintf(stderr, "minidump.Read() failed\n"); exit(1);