/thirdparty/breakpad/client/mac/crash_generation/crash_generation_server.cc

http://github.com/tomahawk-player/tomahawk · C++ · 160 lines · 106 code · 21 blank · 33 comment · 22 complexity · 58cc655ea9a0a3cfcf676a8a280cc5bf MD5 · raw file

  1. // Copyright (c) 2010 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. #include "client/mac/crash_generation/crash_generation_server.h"
  30. #include "client/mac/crash_generation/client_info.h"
  31. #include "client/mac/handler/minidump_generator.h"
  32. #include "common/mac/scoped_task_suspend-inl.h"
  33. namespace google_breakpad {
  34. CrashGenerationServer::CrashGenerationServer(
  35. const char *mach_port_name,
  36. OnClientDumpRequestCallback dump_callback,
  37. void *dump_context,
  38. OnClientExitingCallback exit_callback,
  39. void *exit_context,
  40. bool generate_dumps,
  41. const std::string &dump_path)
  42. : dump_callback_(dump_callback),
  43. dump_context_(dump_context),
  44. exit_callback_(exit_callback),
  45. exit_context_(exit_context),
  46. generate_dumps_(generate_dumps),
  47. dump_dir_(dump_path.empty() ? "/tmp" : dump_path),
  48. started_(false),
  49. receive_port_(mach_port_name),
  50. mach_port_name_(mach_port_name) {
  51. }
  52. CrashGenerationServer::~CrashGenerationServer() {
  53. if (started_)
  54. Stop();
  55. }
  56. bool CrashGenerationServer::Start() {
  57. int thread_create_result = pthread_create(&server_thread_, NULL,
  58. &WaitForMessages, this);
  59. started_ = thread_create_result == 0;
  60. return started_;
  61. }
  62. bool CrashGenerationServer::Stop() {
  63. if (!started_)
  64. return false;
  65. // Send a quit message to the background thread, and then join it.
  66. MachPortSender sender(mach_port_name_.c_str());
  67. MachSendMessage quit_message(kQuitMessage);
  68. const mach_msg_timeout_t kSendTimeoutMs = 2 * 1000;
  69. kern_return_t result = sender.SendMessage(quit_message, kSendTimeoutMs);
  70. if (result == KERN_SUCCESS) {
  71. int thread_join_result = pthread_join(server_thread_, NULL);
  72. started_ = thread_join_result != 0;
  73. }
  74. return !started_;
  75. }
  76. // static
  77. void *CrashGenerationServer::WaitForMessages(void *server) {
  78. CrashGenerationServer *self =
  79. reinterpret_cast<CrashGenerationServer*>(server);
  80. while (self->WaitForOneMessage()) {}
  81. return NULL;
  82. }
  83. bool CrashGenerationServer::WaitForOneMessage() {
  84. MachReceiveMessage message;
  85. kern_return_t result = receive_port_.WaitForMessage(&message,
  86. MACH_MSG_TIMEOUT_NONE);
  87. if (result == KERN_SUCCESS) {
  88. switch (message.GetMessageID()) {
  89. case kDumpRequestMessage: {
  90. ExceptionInfo &info = (ExceptionInfo &)*message.GetData();
  91. mach_port_t remote_task = message.GetTranslatedPort(0);
  92. mach_port_t crashing_thread = message.GetTranslatedPort(1);
  93. mach_port_t handler_thread = message.GetTranslatedPort(2);
  94. mach_port_t ack_port = message.GetTranslatedPort(3);
  95. pid_t remote_pid = -1;
  96. pid_for_task(remote_task, &remote_pid);
  97. ClientInfo client(remote_pid);
  98. bool result;
  99. std::string dump_path;
  100. if (generate_dumps_) {
  101. ScopedTaskSuspend suspend(remote_task);
  102. MinidumpGenerator generator(remote_task, handler_thread);
  103. dump_path = generator.UniqueNameInDirectory(dump_dir_, NULL);
  104. if (info.exception_type && info.exception_code) {
  105. generator.SetExceptionInformation(info.exception_type,
  106. info.exception_code,
  107. info.exception_subcode,
  108. crashing_thread);
  109. }
  110. result = generator.Write(dump_path.c_str());
  111. } else {
  112. result = true;
  113. }
  114. if (result && dump_callback_) {
  115. dump_callback_(dump_context_, client, dump_path);
  116. }
  117. // TODO(ted): support a way for the client to send additional data,
  118. // perhaps with a callback so users of the server can read the data
  119. // themselves?
  120. if (ack_port != MACH_PORT_DEAD && ack_port != MACH_PORT_NULL) {
  121. MachPortSender sender(ack_port);
  122. MachSendMessage ack_message(kAcknowledgementMessage);
  123. const mach_msg_timeout_t kSendTimeoutMs = 2 * 1000;
  124. sender.SendMessage(ack_message, kSendTimeoutMs);
  125. }
  126. if (exit_callback_) {
  127. exit_callback_(exit_context_, client);
  128. }
  129. break;
  130. }
  131. case kQuitMessage:
  132. return false;
  133. }
  134. } else { // result != KERN_SUCCESS
  135. return false;
  136. }
  137. return true;
  138. }
  139. } // namespace google_breakpad