exception_handler_test.cc revision 61e88c7ad7eb072977b4d4d26bcf8929b75af2d4
15ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// Copyright (c) 2006, Google Inc. 25ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// All rights reserved. 35ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// 45ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// Redistribution and use in source and binary forms, with or without 55ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// modification, are permitted provided that the following conditions are 65ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// met: 75ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// 85ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// * Redistributions of source code must retain the above copyright 95ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// notice, this list of conditions and the following disclaimer. 105ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// * Redistributions in binary form must reproduce the above 115ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// copyright notice, this list of conditions and the following disclaimer 125ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// in the documentation and/or other materials provided with the 135ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// distribution. 145ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// * Neither the name of Google Inc. nor the names of its 155ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// contributors may be used to endorse or promote products derived from 165ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// this software without specific prior written permission. 175ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// 185ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 305ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis/* 311cb4ad73a6b77f47dbec69dcb5551c16e37d267dnealsidg++ -framework CoreFoundation -I../../.. ../../minidump_file_writer.cc ../../../common/convert_UTF.c ../../../common/string_conversion.cc ../../../common/mac/string_utilities.cc exception_handler.cc minidump_generator.cc exception_handler_test.cc -o exception_handler_test -mmacosx-version-min=10.4 ../../../common/mac/file_id.cc dynamic_images.cc ../../../common/mac/macho_id.cc ../../../common/mac/macho_walker.cc -lcrypto ../../../common/mac/macho_utilities.cc 325ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis*/ 33e5dc60822e5938fea2ae892ccddb906641ba174emmentovai 345ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis#include <pthread.h> 355ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis#include <pwd.h> 365ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis#include <unistd.h> 375ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 385ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis#include <CoreFoundation/CoreFoundation.h> 395ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 405ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis#include "exception_handler.h" 415ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis#include "minidump_generator.h" 425ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 435ac2b9a569890f165478f91670dcdd553ce2d10ewaylonisusing std::string; 44e5dc60822e5938fea2ae892ccddb906641ba174emmentovaiusing google_breakpad::ExceptionHandler; 455ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 465ac2b9a569890f165478f91670dcdd553ce2d10ewaylonisstatic void *SleepyFunction(void *) { 475ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis while (1) { 485ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis sleep(10000); 495ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis } 503ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return NULL; 515ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis} 525ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 535ac2b9a569890f165478f91670dcdd553ce2d10ewaylonisstatic void Crasher() { 5461e88c7ad7eb072977b4d4d26bcf8929b75af2d4ted.mielczarek int *a = (int*)0x42; 555ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 565ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis fprintf(stdout, "Going to crash...\n"); 575ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis fprintf(stdout, "A = %d", *a); 585ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis} 595ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 605ac2b9a569890f165478f91670dcdd553ce2d10ewaylonisstatic void SoonToCrash() { 615ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis Crasher(); 625ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis} 635ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 645ac2b9a569890f165478f91670dcdd553ce2d10ewaylonisbool MDCallback(const char *dump_dir, const char *file_name, 655ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis void *context, bool success) { 665ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis string path(dump_dir); 675ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis string dest(dump_dir); 685ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis path.append(file_name); 695ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis path.append(".dmp"); 705ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 715ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis fprintf(stdout, "Minidump: %s\n", path.c_str()); 725ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis // Indicate that we've handled the callback 733ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid exit(0); 745ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis} 755ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 765ac2b9a569890f165478f91670dcdd553ce2d10ewaylonisint main(int argc, char * const argv[]) { 775ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis char buffer[PATH_MAX]; 785ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 795ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis // Home dir 803ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid snprintf(buffer, sizeof(buffer), "/tmp/"); 815ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 825ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis string path(buffer); 835ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis ExceptionHandler eh(path, NULL, MDCallback, NULL, true); 845ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis pthread_t t; 855ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 865ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis if (pthread_create(&t, NULL, SleepyFunction, NULL) == 0) { 875ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis pthread_detach(t); 885ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis } else { 895ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis perror("pthread_create"); 905ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis } 915ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 923ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// // Dump a test 933ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// eh.WriteMinidump(); 945ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 955ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis // Test the handler 965ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis SoonToCrash(); 975ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis 985ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis return 0; 995ac2b9a569890f165478f91670dcdd553ce2d10ewaylonis} 100