Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Host.mm -------------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 10 | #include "lldb/Host/Host.h" |
| 11 | #include "lldb/Core/FileSpec.h" |
| 12 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 14 | #include "cfcpp/CFCBundle.h" |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 15 | #include "cfcpp/CFCMutableDictionary.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 16 | #include "cfcpp/CFCReleaser.h" |
| 17 | #include "cfcpp/CFCString.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 19 | #include <objc/objc-auto.h> |
| 20 | |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 21 | #include <ApplicationServices/ApplicationServices.h> |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 22 | #include <Carbon/Carbon.h> |
| 23 | #include <Foundation/Foundation.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | class MacOSXDarwinThread |
| 29 | { |
| 30 | public: |
| 31 | MacOSXDarwinThread(const char *thread_name) : |
| 32 | m_pool (nil) |
| 33 | { |
| 34 | // Register our thread with the collector if garbage collection is enabled. |
| 35 | if (objc_collectingEnabled()) |
| 36 | { |
| 37 | #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 |
| 38 | // On Leopard and earlier there is no way objc_registerThreadWithCollector |
| 39 | // function, so we do it manually. |
| 40 | auto_zone_register_thread(auto_zone()); |
| 41 | #else |
| 42 | // On SnowLoepard and later we just call the thread registration function. |
| 43 | objc_registerThreadWithCollector(); |
| 44 | #endif |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | m_pool = [[NSAutoreleasePool alloc] init]; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name); |
| 53 | } |
| 54 | |
| 55 | ~MacOSXDarwinThread() |
| 56 | { |
| 57 | if (m_pool) |
| 58 | [m_pool release]; |
| 59 | } |
| 60 | |
| 61 | static void PThreadDestructor (void *v) |
| 62 | { |
| 63 | delete (MacOSXDarwinThread*)v; |
| 64 | } |
| 65 | |
| 66 | protected: |
| 67 | NSAutoreleasePool * m_pool; |
| 68 | private: |
| 69 | DISALLOW_COPY_AND_ASSIGN (MacOSXDarwinThread); |
| 70 | }; |
| 71 | |
| 72 | static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT; |
| 73 | static pthread_key_t g_thread_create_key = 0; |
| 74 | |
| 75 | static void |
| 76 | InitThreadCreated() |
| 77 | { |
| 78 | ::pthread_key_create (&g_thread_create_key, MacOSXDarwinThread::PThreadDestructor); |
| 79 | } |
| 80 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | void |
| 82 | Host::ThreadCreated (const char *thread_name) |
| 83 | { |
| 84 | ::pthread_once (&g_thread_create_once, InitThreadCreated); |
| 85 | if (g_thread_create_key) |
| 86 | { |
| 87 | ::pthread_setspecific (g_thread_create_key, new MacOSXDarwinThread(thread_name)); |
| 88 | } |
| 89 | } |
| 90 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | |
| 92 | bool |
| 93 | Host::ResolveExecutableInBundle (FileSpec *file) |
| 94 | { |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 95 | #if defined (__APPLE__) |
| 96 | if (file->GetFileType () == FileSpec::eFileTypeDirectory) |
| 97 | { |
| 98 | char path[PATH_MAX]; |
| 99 | if (file->GetPath(path, sizeof(path))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | { |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 101 | CFCBundle bundle (path); |
| 102 | CFCReleaser<CFURLRef> url(bundle.CopyExecutableURL ()); |
| 103 | if (url.get()) |
| 104 | { |
| 105 | if (::CFURLGetFileSystemRepresentation (url.get(), YES, (UInt8*)path, sizeof(path))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | { |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 107 | file->SetFile(path); |
| 108 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 111 | } |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 112 | } |
| 113 | #endif |
| 114 | return false; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 115 | } |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 116 | |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 117 | lldb::pid_t |
| 118 | Host::LaunchApplication (const FileSpec &app_file_spec) |
| 119 | { |
| 120 | char app_path[PATH_MAX]; |
| 121 | app_file_spec.GetPath(app_path, sizeof(app_path)); |
| 122 | |
| 123 | LSApplicationParameters app_params; |
| 124 | ::bzero (&app_params, sizeof (app_params)); |
| 125 | app_params.flags = kLSLaunchDefaults | |
| 126 | kLSLaunchDontAddToRecents | |
| 127 | kLSLaunchDontSwitch | |
| 128 | kLSLaunchNewInstance;// | 0x00001000; |
| 129 | |
| 130 | FSRef app_fsref; |
| 131 | CFCString app_cfstr (app_path, kCFStringEncodingUTF8); |
| 132 | |
| 133 | OSStatus error = ::FSPathMakeRef ((const UInt8 *)app_path, &app_fsref, false); |
| 134 | |
| 135 | // If we found the app, then store away the name so we don't have to re-look it up. |
| 136 | if (error != noErr) |
| 137 | return LLDB_INVALID_PROCESS_ID; |
| 138 | |
| 139 | app_params.application = &app_fsref; |
| 140 | |
| 141 | ProcessSerialNumber psn; |
| 142 | |
| 143 | error = ::LSOpenApplication (&app_params, &psn); |
| 144 | |
| 145 | if (error != noErr) |
| 146 | return LLDB_INVALID_PROCESS_ID; |
| 147 | |
| 148 | ::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 149 | error = ::GetProcessPID(&psn, &pid); |
| 150 | return pid; |
| 151 | } |
| 152 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 153 | bool |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 154 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 155 | { |
| 156 | // We attach this to an 'odoc' event to specify a particular selection |
| 157 | typedef struct { |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 158 | int16_t reserved0; // must be zero |
| 159 | int16_t fLineNumber; |
| 160 | int32_t fSelStart; |
| 161 | int32_t fSelEnd; |
| 162 | uint32_t reserved1; // must be zero |
| 163 | uint32_t reserved2; // must be zero |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 164 | } BabelAESelInfo; |
| 165 | |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 166 | Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 167 | char file_path[PATH_MAX]; |
| 168 | file_spec.GetPath(file_path, PATH_MAX); |
| 169 | CFCString file_cfstr (file_path, kCFStringEncodingUTF8); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 170 | CFCReleaser<CFURLRef> file_URL (::CFURLCreateWithFileSystemPath (NULL, |
| 171 | file_cfstr.get(), |
| 172 | kCFURLPOSIXPathStyle, |
| 173 | false)); |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 174 | |
| 175 | if (log) |
| 176 | log->Printf("Sending source file: \"%s\" and line: %d to external editor.\n", file_path, line_no); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 177 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 178 | OSStatus error; |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 179 | BabelAESelInfo file_and_line_info = |
| 180 | { |
| 181 | 0, // reserved0 |
| 182 | line_no - 1, // fLineNumber (zero based line number) |
| 183 | 1, // fSelStart |
| 184 | 1024, // fSelEnd |
| 185 | 0, // reserved1 |
| 186 | 0 // reserved2 |
| 187 | }; |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 188 | |
| 189 | AEKeyDesc file_and_line_desc; |
| 190 | |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 191 | error = ::AECreateDesc (typeUTF8Text, |
| 192 | &file_and_line_info, |
| 193 | sizeof (file_and_line_info), |
| 194 | &(file_and_line_desc.descContent)); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 195 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 196 | if (error != noErr) |
| 197 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 198 | if (log) |
| 199 | log->Printf("Error creating AEDesc: %d.\n", error); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
| 203 | file_and_line_desc.descKey = keyAEPosition; |
| 204 | |
Greg Clayton | cb0989a | 2010-08-31 18:56:24 +0000 | [diff] [blame] | 205 | static std::string g_app_name; |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 206 | static FSRef g_app_fsref; |
| 207 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 208 | LSApplicationParameters app_params; |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 209 | ::bzero (&app_params, sizeof (app_params)); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 210 | app_params.flags = kLSLaunchDefaults | |
| 211 | kLSLaunchDontAddToRecents | |
| 212 | kLSLaunchDontSwitch; |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 213 | |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 214 | char *external_editor = ::getenv ("LLDB_EXTERNAL_EDITOR"); |
| 215 | |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 216 | if (external_editor) |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 217 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 218 | if (log) |
| 219 | log->Printf("Looking for external editor \"%s\".\n", external_editor); |
| 220 | |
Greg Clayton | cb0989a | 2010-08-31 18:56:24 +0000 | [diff] [blame] | 221 | if (g_app_name.empty() || strcmp (g_app_name.c_str(), external_editor) != 0) |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 222 | { |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 223 | CFCString editor_name (external_editor, kCFStringEncodingUTF8); |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 224 | error = ::LSFindApplicationForInfo (kLSUnknownCreator, |
| 225 | NULL, |
| 226 | editor_name.get(), |
| 227 | &g_app_fsref, |
| 228 | NULL); |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 229 | |
| 230 | // If we found the app, then store away the name so we don't have to re-look it up. |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 231 | if (error != noErr) |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 232 | { |
| 233 | if (log) |
| 234 | log->Printf("Could not find External Editor application, error: %d.\n", error); |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 235 | return false; |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 236 | } |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 237 | |
| 238 | } |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 239 | app_params.application = &g_app_fsref; |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 242 | ProcessSerialNumber psn; |
| 243 | CFCReleaser<CFArrayRef> file_array(CFArrayCreate (NULL, (const void **) file_URL.ptr_address(false), 1, NULL)); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 244 | error = ::LSOpenURLsWithRole (file_array.get(), |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 245 | kLSRolesAll, |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 246 | &file_and_line_desc, |
| 247 | &app_params, |
| 248 | &psn, |
| 249 | 1); |
| 250 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 251 | AEDisposeDesc (&(file_and_line_desc.descContent)); |
| 252 | |
| 253 | if (error != noErr) |
| 254 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 255 | if (log) |
| 256 | log->Printf("LSOpenURLsWithRole failed, error: %d.\n", error); |
| 257 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
| 261 | ProcessInfoRec which_process; |
| 262 | bzero(&which_process, sizeof(which_process)); |
| 263 | unsigned char ap_name[PATH_MAX]; |
| 264 | which_process.processName = ap_name; |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 265 | error = ::GetProcessInformation (&psn, &which_process); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 266 | |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 267 | bool using_xcode; |
| 268 | if (error != noErr) |
| 269 | { |
| 270 | if (log) |
| 271 | log->Printf("GetProcessInformation failed, error: %d.\n", error); |
| 272 | using_xcode = false; |
| 273 | } |
| 274 | else |
| 275 | using_xcode = strncmp((char *) ap_name+1, "Xcode", (int) ap_name[0]) == 0; |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 276 | |
| 277 | // Xcode doesn't obey the line number in the Open Apple Event. So I have to send |
| 278 | // it an AppleScript to focus on the right line. |
| 279 | |
| 280 | if (using_xcode) |
| 281 | { |
| 282 | static ComponentInstance osa_component = NULL; |
| 283 | static const char *as_template = "tell application \"Xcode\"\n" |
| 284 | "set doc to the first document whose path is \"%s\"\n" |
| 285 | "set the selection to paragraph %d of doc\n" |
| 286 | "--- set the selected paragraph range to {%d, %d} of doc\n" |
| 287 | "end tell\n"; |
| 288 | const int chars_for_int = 32; |
| 289 | static int as_template_len = strlen (as_template); |
| 290 | |
| 291 | |
| 292 | char *as_str; |
| 293 | AEDesc as_desc; |
| 294 | |
| 295 | if (osa_component == NULL) |
| 296 | { |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 297 | osa_component = ::OpenDefaultComponent (kOSAComponentType, |
| 298 | kAppleScriptSubtype); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if (osa_component == NULL) |
| 302 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 303 | if (log) |
| 304 | log->Printf("Could not get default AppleScript component.\n"); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | |
| 308 | uint32_t as_str_size = as_template_len + strlen (file_path) + 3 * chars_for_int + 1; |
| 309 | as_str = (char *) malloc (as_str_size); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 310 | ::snprintf (as_str, |
| 311 | as_str_size - 1, |
| 312 | as_template, |
| 313 | file_path, |
| 314 | line_no, |
| 315 | line_no, |
| 316 | line_no); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 317 | |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 318 | error = ::AECreateDesc (typeChar, |
| 319 | as_str, |
| 320 | strlen (as_str), |
| 321 | &as_desc); |
| 322 | |
| 323 | ::free (as_str); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 324 | |
| 325 | if (error != noErr) |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 326 | { |
| 327 | if (log) |
| 328 | log->Printf("Failed to create AEDesc for Xcode AppleEvent: %d.\n", error); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 329 | return false; |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 330 | } |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 331 | |
| 332 | OSAID ret_OSAID; |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 333 | error = ::OSACompileExecute (osa_component, |
| 334 | &as_desc, |
| 335 | kOSANullScript, |
| 336 | kOSAModeNeverInteract, |
| 337 | &ret_OSAID); |
| 338 | |
| 339 | ::OSADispose (osa_component, ret_OSAID); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 340 | |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 341 | ::AEDisposeDesc (&as_desc); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 342 | |
| 343 | if (error != noErr) |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 344 | { |
| 345 | if (log) |
| 346 | log->Printf("Sending AppleEvent to Xcode failed, error: %d.\n", error); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 347 | return false; |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 348 | } |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | return true; |
| 352 | } |