/thirdparty/breakpad/client/mac/tests/spawn_child_process.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 149 lines · 96 code · 21 blank · 32 comment · 14 complexity · da6988ac5d698ae8cd9132b609cdd099 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. // Utility functions for spawning a helper process using a different
  30. // CPU architecture.
  31. #ifndef GOOGLE_BREAKPAD_CLIENT_MAC_TESTS_SPAWN_CHILD_PROCESS
  32. #define GOOGLE_BREAKPAD_CLIENT_MAC_TESTS_SPAWN_CHILD_PROCESS
  33. #include <AvailabilityMacros.h>
  34. #ifndef MAC_OS_X_VERSION_10_6
  35. #define MAC_OS_X_VERSION_10_6 1060
  36. #endif
  37. #include <crt_externs.h>
  38. #include <mach-o/dyld.h>
  39. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
  40. #include <spawn.h>
  41. #endif
  42. #include <string>
  43. #include <vector>
  44. #include "google_breakpad/common/minidump_format.h"
  45. namespace google_breakpad_test {
  46. using std::string;
  47. using std::vector;
  48. const MDCPUArchitecture kNativeArchitecture =
  49. #if defined(__i386__)
  50. MD_CPU_ARCHITECTURE_X86
  51. #elif defined(__x86_64__)
  52. MD_CPU_ARCHITECTURE_AMD64
  53. #elif defined(__ppc__) || defined(__ppc64__)
  54. MD_CPU_ARCHITECTURE_PPC
  55. #else
  56. #error "This file has not been ported to this CPU architecture."
  57. #endif
  58. ;
  59. const u_int32_t kNativeContext =
  60. #if defined(__i386__)
  61. MD_CONTEXT_X86
  62. #elif defined(__x86_64__)
  63. MD_CONTEXT_AMD64
  64. #elif defined(__ppc__) || defined(__ppc64__)
  65. MD_CONTEXT_PPC
  66. #else
  67. #error "This file has not been ported to this CPU architecture."
  68. #endif
  69. ;
  70. string GetExecutablePath() {
  71. char self_path[PATH_MAX];
  72. uint32_t size = sizeof(self_path);
  73. if (_NSGetExecutablePath(self_path, &size) != 0)
  74. return "";
  75. return self_path;
  76. }
  77. string GetHelperPath() {
  78. string helper_path(GetExecutablePath());
  79. size_t pos = helper_path.rfind('/');
  80. if (pos == string::npos)
  81. return "";
  82. helper_path.erase(pos + 1);
  83. helper_path += "minidump_generator_test_helper";
  84. return helper_path;
  85. }
  86. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
  87. pid_t spawn_child_process(const char** argv) {
  88. posix_spawnattr_t spawnattr;
  89. if (posix_spawnattr_init(&spawnattr) != 0)
  90. return (pid_t)-1;
  91. cpu_type_t pref_cpu_types[2] = {
  92. #if defined(__x86_64__)
  93. CPU_TYPE_X86,
  94. #elif defined(__i386__)
  95. CPU_TYPE_X86_64,
  96. #endif
  97. CPU_TYPE_ANY
  98. };
  99. // Set spawn attributes.
  100. size_t attr_count = sizeof(pref_cpu_types) / sizeof(pref_cpu_types[0]);
  101. size_t attr_ocount = 0;
  102. if (posix_spawnattr_setbinpref_np(&spawnattr,
  103. attr_count,
  104. pref_cpu_types,
  105. &attr_ocount) != 0 ||
  106. attr_ocount != attr_count) {
  107. posix_spawnattr_destroy(&spawnattr);
  108. return (pid_t)-1;
  109. }
  110. // Create an argv array.
  111. vector<char*> argv_v;
  112. while (*argv) {
  113. argv_v.push_back(strdup(*argv));
  114. argv++;
  115. }
  116. argv_v.push_back(NULL);
  117. pid_t new_pid = 0;
  118. int result = posix_spawnp(&new_pid, argv_v[0], NULL, &spawnattr,
  119. &argv_v[0], *_NSGetEnviron());
  120. posix_spawnattr_destroy(&spawnattr);
  121. for (unsigned i = 0; i < argv_v.size(); i++) {
  122. free(argv_v[i]);
  123. }
  124. return result == 0 ? new_pid : -1;
  125. }
  126. #endif
  127. } // namespace google_breakpad_test
  128. #endif // GOOGLE_BREAKPAD_CLIENT_MAC_TESTS_SPAWN_CHILD_PROCESS