/thirdparty/breakpad/client/solaris/handler/exception_handler_test.cc

http://github.com/tomahawk-player/tomahawk · C++ · 119 lines · 67 code · 16 blank · 36 comment · 5 complexity · 86126f825ac9e5ee4b5247c97d2ac5a7 MD5 · raw file

  1. // Copyright (c) 2007, 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. // Author: Alfred Peng
  30. #include <pthread.h>
  31. #include <unistd.h>
  32. #include <cassert>
  33. #include <cstdio>
  34. #include <cstdlib>
  35. #include <cstring>
  36. #include "client/solaris/handler/exception_handler.h"
  37. #include "client/solaris/handler/solaris_lwp.h"
  38. using namespace google_breakpad;
  39. // Thread use this to see if it should stop working.
  40. static bool should_exit = false;
  41. static int foo2(int arg) {
  42. // Stack variable, used for debugging stack dumps.
  43. int c = 0xcccccccc;
  44. fprintf(stderr, "Thread trying to crash: %x\n", getpid());
  45. c = *reinterpret_cast<int *>(0x5);
  46. return c;
  47. }
  48. static int foo(int arg) {
  49. // Stack variable, used for debugging stack dumps.
  50. int b = 0xbbbbbbbb;
  51. b = foo2(b);
  52. return b;
  53. }
  54. static void *thread_crash(void *) {
  55. // Stack variable, used for debugging stack dumps.
  56. int a = 0xaaaaaaaa;
  57. sleep(3);
  58. a = foo(a);
  59. printf("%x\n", a);
  60. return NULL;
  61. }
  62. static void *thread_main(void *) {
  63. while (!should_exit)
  64. sleep(1);
  65. return NULL;
  66. }
  67. static void CreateCrashThread() {
  68. pthread_t h;
  69. pthread_create(&h, NULL, thread_crash, NULL);
  70. pthread_detach(h);
  71. }
  72. // Create working threads.
  73. static void CreateThread(int num) {
  74. pthread_t h;
  75. for (int i = 0; i < num; ++i) {
  76. pthread_create(&h, NULL, thread_main, NULL);
  77. pthread_detach(h);
  78. }
  79. }
  80. // Callback when minidump written.
  81. static bool MinidumpCallback(const char *dump_path,
  82. const char *minidump_id,
  83. void *context,
  84. bool succeeded) {
  85. int index = reinterpret_cast<int>(context);
  86. if (index == 0) {
  87. should_exit = true;
  88. return true;
  89. }
  90. // Don't process it.
  91. return false;
  92. }
  93. int main(int argc, char *argv[]) {
  94. int handler_index = 1;
  95. ExceptionHandler handler_ignore(".", NULL, MinidumpCallback,
  96. (void*)handler_index, true);
  97. CreateCrashThread();
  98. CreateThread(10);
  99. while (true)
  100. sleep(20);
  101. should_exit = true;
  102. return 0;
  103. }