PageRenderTime 37ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparty/breakpad/client/mac/testapp/Controller.m

http://github.com/tomahawk-player/tomahawk
Objective C | 261 lines | 170 code | 47 blank | 44 comment | 39 complexity | 730eb2b6a5cb93d554099217b6553109 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Copyright (c) 2006, 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. #import <Breakpad/Breakpad.h>
  30. #import "Controller.h"
  31. #import "TestClass.h"
  32. #import "GTMDefines.h"
  33. #include <unistd.h>
  34. #include <mach/mach.h>
  35. @implementation Controller
  36. - (void)causeCrash {
  37. float *aPtr = nil;
  38. NSLog(@"Crash!");
  39. NSLog(@"Bad programmer: %f", *aPtr);
  40. }
  41. - (void)generateReportWithoutCrash:(id)sender {
  42. BreakpadGenerateAndSendReport(breakpad_);
  43. }
  44. - (IBAction)showForkTestWindow:(id) sender {
  45. [forkTestOptions_ setIsVisible:YES];
  46. }
  47. - (IBAction)forkTestOptions:(id)sender {
  48. NSInteger tag = [[sender selectedCell] tag];
  49. NSLog(@"sender tag: %d", tag);
  50. if (tag <= 2) {
  51. bpForkOption = tag;
  52. }
  53. if (tag == 3) {
  54. useVFork = NO;
  55. }
  56. if (tag == 4) {
  57. useVFork = YES;
  58. }
  59. if (tag >= 5 && tag <= 7) {
  60. progCrashPoint = tag;
  61. }
  62. }
  63. - (IBAction)forkTestGo:(id)sender {
  64. NSString *resourcePath = [[NSBundle bundleForClass:
  65. [self class]] resourcePath];
  66. NSString *execProgname = nil;
  67. if (progCrashPoint == DURINGLAUNCH) {
  68. execProgname = [resourcePath stringByAppendingString:@"/crashduringload"];
  69. } else if (progCrashPoint == AFTERLAUNCH) {
  70. execProgname = [resourcePath stringByAppendingString:@"/crashInMain"];
  71. }
  72. const char *progName = NULL;
  73. if (progCrashPoint != BETWEENFORKEXEC) {
  74. progName = [execProgname UTF8String];
  75. }
  76. int pid;
  77. if (bpForkOption == UNINSTALL) {
  78. BreakpadRelease(breakpad_);
  79. }
  80. if (useVFork) {
  81. pid = vfork();
  82. } else {
  83. pid = fork();
  84. }
  85. if (pid == 0) {
  86. sleep(3);
  87. NSLog(@"Child continuing");
  88. FILE *fd = fopen("/tmp/childlog.txt","wt");
  89. kern_return_t kr;
  90. if (bpForkOption == RESETEXCEPTIONPORT) {
  91. kr = task_set_exception_ports(mach_task_self(),
  92. EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION |
  93. EXC_MASK_ARITHMETIC | EXC_MASK_BREAKPOINT,
  94. MACH_PORT_NULL,
  95. EXCEPTION_DEFAULT,
  96. THREAD_STATE_NONE);
  97. fprintf(fd,"task_set_exception_ports returned %d\n", kr);
  98. }
  99. if (progCrashPoint == BETWEENFORKEXEC) {
  100. fprintf(fd,"crashing post-fork\n");
  101. int *a = NULL;
  102. printf("%d\n",*a++);
  103. }
  104. fprintf(fd,"about to call exec with %s\n", progName);
  105. fclose(fd);
  106. int i = execl(progName, progName, NULL);
  107. fprintf(fd, "exec returned! %d\n", i);
  108. fclose(fd);
  109. }
  110. }
  111. - (IBAction)crash:(id)sender {
  112. NSInteger tag = [sender tag];
  113. if (tag == 1) {
  114. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  115. [self performSelector:@selector(causeCrash) withObject:nil afterDelay:10.0];
  116. [sender setState:NSOnState];
  117. return;
  118. }
  119. if (tag == 2 && breakpad_) {
  120. BreakpadRelease(breakpad_);
  121. breakpad_ = NULL;
  122. return;
  123. }
  124. [self causeCrash];
  125. }
  126. - (void)anotherThread {
  127. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  128. TestClass *tc = [[TestClass alloc] init];
  129. [tc wait];
  130. [pool release];
  131. }
  132. - (void)awakeFromNib {
  133. NSBundle *bundle = [NSBundle mainBundle];
  134. NSDictionary *info = [bundle infoDictionary];
  135. breakpad_ = BreakpadCreate(info);
  136. // Do some unit tests with keys
  137. // first a series of bogus values
  138. BreakpadSetKeyValue(breakpad_, nil, @"bad2");
  139. BreakpadSetKeyValue(nil, @"bad3", @"bad3");
  140. // Now some good ones
  141. BreakpadSetKeyValue(breakpad_,@"key1", @"value1");
  142. BreakpadSetKeyValue(breakpad_,@"key2", @"value2");
  143. BreakpadSetKeyValue(breakpad_,@"key3", @"value3");
  144. // Look for a bogus one that we didn't try to set
  145. NSString *test = BreakpadKeyValue(breakpad_, @"bad4");
  146. if (test) {
  147. NSLog(@"Bad BreakpadKeyValue (bad4)");
  148. }
  149. // Look for a bogus one we did try to set
  150. test = BreakpadKeyValue(breakpad_, @"bad1");
  151. if (test) {
  152. NSLog(@"Bad BreakpadKeyValue (bad1)");
  153. }
  154. // Test some bad args for BreakpadKeyValue
  155. test = BreakpadKeyValue(nil, @"bad5");
  156. if (test) {
  157. NSLog(@"Bad BreakpadKeyValue (bad5)");
  158. }
  159. test = BreakpadKeyValue(breakpad_, nil);
  160. if (test) {
  161. NSLog(@"Bad BreakpadKeyValue (nil)");
  162. }
  163. // Find some we did set
  164. test = BreakpadKeyValue(breakpad_, @"key1");
  165. if (![test isEqualToString:@"value1"]) {
  166. NSLog(@"Can't find BreakpadKeyValue (key1)");
  167. }
  168. test = BreakpadKeyValue(breakpad_, @"key2");
  169. if (![test isEqualToString:@"value2"]) {
  170. NSLog(@"Can't find BreakpadKeyValue (key2)");
  171. }
  172. test = BreakpadKeyValue(breakpad_, @"key3");
  173. if (![test isEqualToString:@"value3"]) {
  174. NSLog(@"Can't find BreakpadKeyValue (key3)");
  175. }
  176. // Bad args for BreakpadRemoveKeyValue
  177. BreakpadRemoveKeyValue(nil, @"bad6");
  178. BreakpadRemoveKeyValue(breakpad_, nil);
  179. // Remove one that is valid
  180. BreakpadRemoveKeyValue(breakpad_, @"key3");
  181. // Try and find it
  182. test = BreakpadKeyValue(breakpad_, @"key3");
  183. if (test) {
  184. NSLog(@"Shouldn't find BreakpadKeyValue (key3)");
  185. }
  186. // Try and remove it again
  187. BreakpadRemoveKeyValue(breakpad_, @"key3");
  188. // Try removal by setting to nil
  189. BreakpadSetKeyValue(breakpad_,@"key2", nil);
  190. // Try and find it
  191. test = BreakpadKeyValue(breakpad_, @"key2");
  192. if (test) {
  193. NSLog(@"Shouldn't find BreakpadKeyValue (key2)");
  194. }
  195. BreakpadAddUploadParameter(breakpad_,
  196. @"MeaningOfLife",
  197. @"42");
  198. [NSThread detachNewThreadSelector:@selector(anotherThread)
  199. toTarget:self withObject:nil];
  200. NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
  201. // If the user specified autocrash on the command line, toggle
  202. // Breakpad to not confirm and crash immediately. This is for
  203. // automated testing.
  204. if ([args boolForKey:@"autocrash"]) {
  205. BreakpadSetKeyValue(breakpad_,
  206. @BREAKPAD_SKIP_CONFIRM,
  207. @"YES");
  208. [self causeCrash];
  209. }
  210. progCrashPoint = DURINGLAUNCH;
  211. [window_ center];
  212. [window_ makeKeyAndOrderFront:self];
  213. }
  214. @end