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" |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 11 | |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 12 | #include <libproc.h> |
| 13 | #include <sys/proc.h> |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 14 | #include <sys/stat.h> |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 16 | |
| 17 | #include "lldb/Core/ArchSpec.h" |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Communication.h" |
| 19 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 20 | #include "lldb/Core/FileSpec.h" |
| 21 | #include "lldb/Core/Log.h" |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StreamFile.h" |
| 23 | #include "lldb/Core/StreamString.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 25 | #include "cfcpp/CFCBundle.h" |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 26 | #include "cfcpp/CFCMutableArray.h" |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 27 | #include "cfcpp/CFCMutableDictionary.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 28 | #include "cfcpp/CFCReleaser.h" |
| 29 | #include "cfcpp/CFCString.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 31 | #include <objc/objc-auto.h> |
| 32 | |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 33 | #include <ApplicationServices/ApplicationServices.h> |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 34 | #include <Carbon/Carbon.h> |
| 35 | #include <Foundation/Foundation.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | class MacOSXDarwinThread |
| 41 | { |
| 42 | public: |
| 43 | MacOSXDarwinThread(const char *thread_name) : |
| 44 | m_pool (nil) |
| 45 | { |
| 46 | // Register our thread with the collector if garbage collection is enabled. |
| 47 | if (objc_collectingEnabled()) |
| 48 | { |
| 49 | #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 |
| 50 | // On Leopard and earlier there is no way objc_registerThreadWithCollector |
| 51 | // function, so we do it manually. |
| 52 | auto_zone_register_thread(auto_zone()); |
| 53 | #else |
| 54 | // On SnowLoepard and later we just call the thread registration function. |
| 55 | objc_registerThreadWithCollector(); |
| 56 | #endif |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | m_pool = [[NSAutoreleasePool alloc] init]; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name); |
| 65 | } |
| 66 | |
| 67 | ~MacOSXDarwinThread() |
| 68 | { |
| 69 | if (m_pool) |
| 70 | [m_pool release]; |
| 71 | } |
| 72 | |
| 73 | static void PThreadDestructor (void *v) |
| 74 | { |
| 75 | delete (MacOSXDarwinThread*)v; |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | NSAutoreleasePool * m_pool; |
| 80 | private: |
| 81 | DISALLOW_COPY_AND_ASSIGN (MacOSXDarwinThread); |
| 82 | }; |
| 83 | |
| 84 | static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT; |
| 85 | static pthread_key_t g_thread_create_key = 0; |
| 86 | |
| 87 | static void |
| 88 | InitThreadCreated() |
| 89 | { |
| 90 | ::pthread_key_create (&g_thread_create_key, MacOSXDarwinThread::PThreadDestructor); |
| 91 | } |
| 92 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | void |
| 94 | Host::ThreadCreated (const char *thread_name) |
| 95 | { |
| 96 | ::pthread_once (&g_thread_create_once, InitThreadCreated); |
| 97 | if (g_thread_create_key) |
| 98 | { |
| 99 | ::pthread_setspecific (g_thread_create_key, new MacOSXDarwinThread(thread_name)); |
| 100 | } |
| 101 | } |
| 102 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | |
| 104 | bool |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 105 | Host::ResolveExecutableInBundle (FileSpec &file) |
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 | #if defined (__APPLE__) |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 108 | if (file.GetFileType () == FileSpec::eFileTypeDirectory) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | { |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 110 | char path[PATH_MAX]; |
| 111 | if (file.GetPath(path, sizeof(path))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | { |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 113 | CFCBundle bundle (path); |
| 114 | CFCReleaser<CFURLRef> url(bundle.CopyExecutableURL ()); |
| 115 | if (url.get()) |
| 116 | { |
| 117 | if (::CFURLGetFileSystemRepresentation (url.get(), YES, (UInt8*)path, sizeof(path))) |
| 118 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 119 | file.SetFile(path, false); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 125 | #endif |
| 126 | return false; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 127 | } |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 128 | |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 129 | lldb::pid_t |
| 130 | Host::LaunchApplication (const FileSpec &app_file_spec) |
| 131 | { |
| 132 | char app_path[PATH_MAX]; |
| 133 | app_file_spec.GetPath(app_path, sizeof(app_path)); |
| 134 | |
| 135 | LSApplicationParameters app_params; |
| 136 | ::bzero (&app_params, sizeof (app_params)); |
| 137 | app_params.flags = kLSLaunchDefaults | |
| 138 | kLSLaunchDontAddToRecents | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 139 | kLSLaunchNewInstance; |
| 140 | |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 141 | |
| 142 | FSRef app_fsref; |
| 143 | CFCString app_cfstr (app_path, kCFStringEncodingUTF8); |
| 144 | |
| 145 | OSStatus error = ::FSPathMakeRef ((const UInt8 *)app_path, &app_fsref, false); |
| 146 | |
| 147 | // If we found the app, then store away the name so we don't have to re-look it up. |
| 148 | if (error != noErr) |
| 149 | return LLDB_INVALID_PROCESS_ID; |
| 150 | |
| 151 | app_params.application = &app_fsref; |
| 152 | |
| 153 | ProcessSerialNumber psn; |
| 154 | |
| 155 | error = ::LSOpenApplication (&app_params, &psn); |
| 156 | |
| 157 | if (error != noErr) |
| 158 | return LLDB_INVALID_PROCESS_ID; |
| 159 | |
| 160 | ::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 161 | error = ::GetProcessPID(&psn, &pid); |
| 162 | return pid; |
| 163 | } |
| 164 | |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 165 | |
| 166 | static void * |
| 167 | AcceptPIDFromInferior (void *arg) |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 168 | { |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 169 | const char *connect_url = (const char *)arg; |
Greg Clayton | ea3259d | 2010-10-19 17:03:58 +0000 | [diff] [blame] | 170 | ConnectionFileDescriptor file_conn; |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 171 | Error error; |
| 172 | if (file_conn.Connect (connect_url, &error) == eConnectionStatusSuccess) |
Greg Clayton | ea3259d | 2010-10-19 17:03:58 +0000 | [diff] [blame] | 173 | { |
| 174 | char pid_str[256]; |
| 175 | ::bzero (pid_str, sizeof(pid_str)); |
| 176 | ConnectionStatus status; |
| 177 | const size_t pid_str_len = file_conn.Read (pid_str, sizeof(pid_str), status, NULL); |
| 178 | if (pid_str_len > 0) |
| 179 | { |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 180 | int pid = atoi (pid_str); |
| 181 | return (void *)(intptr_t)pid; |
Greg Clayton | ea3259d | 2010-10-19 17:03:58 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 184 | return NULL; |
Greg Clayton | ea3259d | 2010-10-19 17:03:58 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 187 | static bool |
| 188 | WaitForProcessToSIGSTOP (const lldb::pid_t pid, const int timeout_in_seconds) |
| 189 | { |
| 190 | const int time_delta_usecs = 100000; |
| 191 | const int num_retries = timeout_in_seconds/time_delta_usecs; |
| 192 | for (int i=0; i<num_retries; i++) |
| 193 | { |
| 194 | struct proc_bsdinfo bsd_info; |
| 195 | int error = ::proc_pidinfo (pid, PROC_PIDTBSDINFO, |
| 196 | (uint64_t) 0, |
| 197 | &bsd_info, |
| 198 | PROC_PIDTBSDINFO_SIZE); |
| 199 | |
| 200 | switch (error) |
| 201 | { |
| 202 | case EINVAL: |
| 203 | case ENOTSUP: |
| 204 | case ESRCH: |
| 205 | case EPERM: |
| 206 | return false; |
| 207 | |
| 208 | default: |
| 209 | break; |
| 210 | |
| 211 | case 0: |
| 212 | if (bsd_info.pbi_status == SSTOP) |
| 213 | return true; |
| 214 | } |
| 215 | ::usleep (time_delta_usecs); |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | static lldb::pid_t |
| 221 | LaunchInNewTerminalWithCommandFile |
Greg Clayton | ea3259d | 2010-10-19 17:03:58 +0000 | [diff] [blame] | 222 | ( |
| 223 | const char **argv, |
| 224 | const char **envp, |
| 225 | const ArchSpec *arch_spec, |
| 226 | bool stop_at_entry, |
| 227 | bool disable_aslr |
| 228 | ) |
| 229 | { |
| 230 | if (!argv || !argv[0]) |
| 231 | return LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 232 | |
| 233 | OSStatus error = 0; |
| 234 | |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 235 | FileSpec program (argv[0], false); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 236 | |
| 237 | |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 238 | std::string unix_socket_name; |
| 239 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 240 | char temp_file_path[PATH_MAX]; |
| 241 | const char *tmpdir = ::getenv ("TMPDIR"); |
| 242 | if (tmpdir == NULL) |
| 243 | tmpdir = "/tmp/"; |
| 244 | ::snprintf (temp_file_path, sizeof(temp_file_path), "%s%s-XXXXXX", tmpdir, program.GetFilename().AsCString()); |
| 245 | |
| 246 | if (::mktemp (temp_file_path) == NULL) |
| 247 | return LLDB_INVALID_PROCESS_ID; |
| 248 | |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 249 | unix_socket_name.assign (temp_file_path); |
| 250 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 251 | ::strncat (temp_file_path, ".command", sizeof (temp_file_path)); |
| 252 | |
| 253 | StreamFile command_file (temp_file_path, "w"); |
| 254 | |
| 255 | FileSpec darwin_debug_file_spec; |
| 256 | if (!Host::GetLLDBPath (ePathTypeSupportExecutableDir, darwin_debug_file_spec)) |
| 257 | return LLDB_INVALID_PROCESS_ID; |
| 258 | darwin_debug_file_spec.GetFilename().SetCString("darwin-debug"); |
| 259 | |
| 260 | if (!darwin_debug_file_spec.Exists()) |
| 261 | return LLDB_INVALID_PROCESS_ID; |
| 262 | |
| 263 | char launcher_path[PATH_MAX]; |
| 264 | darwin_debug_file_spec.GetPath(launcher_path, sizeof(launcher_path)); |
| 265 | command_file.Printf("\"%s\" ", launcher_path); |
| 266 | |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 267 | command_file.Printf("--unix-socket=%s ", unix_socket_name.c_str()); |
| 268 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 269 | if (arch_spec && arch_spec->IsValid()) |
| 270 | { |
| 271 | command_file.Printf("--arch=%s ", arch_spec->AsCString()); |
| 272 | } |
| 273 | |
| 274 | if (disable_aslr) |
| 275 | { |
| 276 | command_file.PutCString("--disable-aslr "); |
| 277 | } |
| 278 | |
| 279 | command_file.PutCString("-- "); |
| 280 | |
| 281 | if (argv) |
| 282 | { |
| 283 | for (size_t i=0; argv[i] != NULL; ++i) |
| 284 | { |
| 285 | command_file.Printf("\"%s\" ", argv[i]); |
| 286 | } |
| 287 | } |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 288 | command_file.PutCString("\necho Process exited with status $?\n"); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 289 | command_file.Close(); |
| 290 | if (::chmod (temp_file_path, S_IRWXU | S_IRWXG) != 0) |
| 291 | return LLDB_INVALID_PROCESS_ID; |
| 292 | |
| 293 | CFCMutableDictionary cf_env_dict; |
| 294 | |
| 295 | const bool can_create = true; |
| 296 | if (envp) |
| 297 | { |
| 298 | for (size_t i=0; envp[i] != NULL; ++i) |
| 299 | { |
| 300 | const char *env_entry = envp[i]; |
| 301 | const char *equal_pos = strchr(env_entry, '='); |
| 302 | if (equal_pos) |
| 303 | { |
| 304 | std::string env_key (env_entry, equal_pos); |
| 305 | std::string env_val (equal_pos + 1); |
| 306 | CFCString cf_env_key (env_key.c_str(), kCFStringEncodingUTF8); |
| 307 | CFCString cf_env_val (env_val.c_str(), kCFStringEncodingUTF8); |
| 308 | cf_env_dict.AddValue (cf_env_key.get(), cf_env_val.get(), can_create); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | LSApplicationParameters app_params; |
| 314 | ::bzero (&app_params, sizeof (app_params)); |
| 315 | app_params.flags = kLSLaunchDontAddToRecents | kLSLaunchAsync; |
| 316 | app_params.argv = NULL; |
| 317 | app_params.environment = (CFDictionaryRef)cf_env_dict.get(); |
| 318 | |
| 319 | CFCReleaser<CFURLRef> command_file_url (::CFURLCreateFromFileSystemRepresentation (NULL, |
| 320 | (const UInt8 *)temp_file_path, |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 321 | strlen(temp_file_path), |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 322 | false)); |
| 323 | |
| 324 | CFCMutableArray urls; |
| 325 | |
| 326 | // Terminal.app will open the ".command" file we have created |
| 327 | // and run our process inside it which will wait at the entry point |
| 328 | // for us to attach. |
| 329 | urls.AppendValue(command_file_url.get()); |
| 330 | |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 331 | |
| 332 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 333 | |
| 334 | Error lldb_error; |
| 335 | // Sleep and wait a bit for debugserver to start to listen... |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 336 | char connect_url[128]; |
| 337 | ::snprintf (connect_url, sizeof(connect_url), "unix-accept://%s", unix_socket_name.c_str()); |
| 338 | |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 339 | // Spawn a new thread to accept incoming connection on the connect_url |
| 340 | // so we can grab the pid from the inferior |
| 341 | lldb::thread_t accept_thread = Host::ThreadCreate (unix_socket_name.c_str(), |
| 342 | AcceptPIDFromInferior, |
| 343 | connect_url, |
| 344 | &lldb_error); |
| 345 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 346 | ProcessSerialNumber psn; |
| 347 | error = LSOpenURLsWithRole(urls.get(), kLSRolesShell, NULL, &app_params, &psn, 1); |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 348 | if (error == noErr) |
| 349 | { |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 350 | thread_result_t accept_thread_result = NULL; |
| 351 | if (Host::ThreadJoin (accept_thread, &accept_thread_result, &lldb_error)) |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 352 | { |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 353 | if (accept_thread_result) |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 354 | { |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 355 | pid = (intptr_t)accept_thread_result; |
| 356 | |
| 357 | // Wait for process to be stopped the the entry point by watching |
| 358 | // for the process status to be set to SSTOP which indicates it it |
| 359 | // SIGSTOP'ed at the entry point |
| 360 | WaitForProcessToSIGSTOP (pid, 5); |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 364 | else |
| 365 | { |
| 366 | Host::ThreadCancel (accept_thread, &lldb_error); |
| 367 | } |
| 368 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 369 | return pid; |
| 370 | } |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 371 | |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 372 | const char *applscript_in_new_tty = |
| 373 | "tell application \"Terminal\"\n" |
| 374 | " do script \"%s\"\n" |
| 375 | "end tell\n"; |
| 376 | |
| 377 | |
| 378 | const char *applscript_in_existing_tty = "\ |
| 379 | set the_shell_script to \"%s\"\n\ |
| 380 | tell application \"Terminal\"\n\ |
| 381 | repeat with the_window in (get windows)\n\ |
| 382 | repeat with the_tab in tabs of the_window\n\ |
| 383 | set the_tty to tty in the_tab\n\ |
| 384 | if the_tty contains \"%s\" then\n\ |
| 385 | if the_tab is not busy then\n\ |
| 386 | set selected of the_tab to true\n\ |
| 387 | set frontmost of the_window to true\n\ |
| 388 | do script the_shell_script in the_tab\n\ |
| 389 | return\n\ |
| 390 | end if\n\ |
| 391 | end if\n\ |
| 392 | end repeat\n\ |
| 393 | end repeat\n\ |
| 394 | do script the_shell_script\n\ |
| 395 | end tell\n"; |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 396 | |
| 397 | lldb::pid_t |
| 398 | LaunchInNewTerminalWithAppleScript |
| 399 | ( |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 400 | const char *tty_name, |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 401 | const char **argv, |
| 402 | const char **envp, |
| 403 | const ArchSpec *arch_spec, |
| 404 | bool stop_at_entry, |
| 405 | bool disable_aslr |
| 406 | ) |
| 407 | { |
| 408 | if (!argv || !argv[0]) |
| 409 | return LLDB_INVALID_PROCESS_ID; |
| 410 | |
| 411 | std::string unix_socket_name; |
| 412 | |
| 413 | char temp_file_path[PATH_MAX] = "/tmp/XXXXXX"; |
| 414 | if (::mktemp (temp_file_path) == NULL) |
| 415 | return LLDB_INVALID_PROCESS_ID; |
| 416 | |
| 417 | unix_socket_name.assign (temp_file_path); |
| 418 | |
| 419 | StreamString command; |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 420 | FileSpec darwin_debug_file_spec; |
| 421 | if (!Host::GetLLDBPath (ePathTypeSupportExecutableDir, darwin_debug_file_spec)) |
| 422 | return LLDB_INVALID_PROCESS_ID; |
| 423 | darwin_debug_file_spec.GetFilename().SetCString("darwin-debug"); |
| 424 | |
| 425 | if (!darwin_debug_file_spec.Exists()) |
| 426 | return LLDB_INVALID_PROCESS_ID; |
| 427 | |
| 428 | char launcher_path[PATH_MAX]; |
| 429 | darwin_debug_file_spec.GetPath(launcher_path, sizeof(launcher_path)); |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 430 | |
| 431 | command.Printf("'%s' --unix-socket=%s", launcher_path, unix_socket_name.c_str()); |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 432 | |
| 433 | if (arch_spec && arch_spec->IsValid()) |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 434 | command.Printf(" --arch=%s", arch_spec->AsCString()); |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 435 | |
| 436 | if (disable_aslr) |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 437 | command.PutCString(" --disable-aslr"); |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 438 | |
| 439 | command.PutCString(" --"); |
| 440 | |
| 441 | if (argv) |
| 442 | { |
| 443 | for (size_t i=0; argv[i] != NULL; ++i) |
| 444 | { |
| 445 | command.Printf(" '%s'", argv[i]); |
| 446 | } |
| 447 | } |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 448 | command.PutCString (" ; echo Process exited with status $?"); |
| 449 | |
| 450 | StreamString applescript_source; |
| 451 | |
| 452 | if (tty_name && tty_name[0]) |
| 453 | { |
| 454 | applescript_source.Printf (applscript_in_existing_tty, |
| 455 | command.GetString().c_str(), |
| 456 | tty_name); |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | applescript_source.Printf (applscript_in_new_tty, |
| 461 | command.GetString().c_str()); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | |
| 466 | const char *script_source = applescript_source.GetString().c_str(); |
| 467 | //puts (script_source); |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 468 | NSAppleScript* applescript = [[NSAppleScript alloc] initWithSource:[NSString stringWithCString:script_source encoding:NSUTF8StringEncoding]]; |
| 469 | |
| 470 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 471 | |
| 472 | Error lldb_error; |
| 473 | // Sleep and wait a bit for debugserver to start to listen... |
| 474 | ConnectionFileDescriptor file_conn; |
| 475 | char connect_url[128]; |
| 476 | ::snprintf (connect_url, sizeof(connect_url), "unix-accept://%s", unix_socket_name.c_str()); |
| 477 | |
| 478 | // Spawn a new thread to accept incoming connection on the connect_url |
| 479 | // so we can grab the pid from the inferior |
| 480 | lldb::thread_t accept_thread = Host::ThreadCreate (unix_socket_name.c_str(), |
| 481 | AcceptPIDFromInferior, |
| 482 | connect_url, |
| 483 | &lldb_error); |
| 484 | |
| 485 | |
| 486 | [applescript executeAndReturnError:nil]; |
| 487 | |
| 488 | thread_result_t accept_thread_result = NULL; |
| 489 | if (Host::ThreadJoin (accept_thread, &accept_thread_result, &lldb_error)) |
| 490 | { |
| 491 | if (accept_thread_result) |
| 492 | { |
| 493 | pid = (intptr_t)accept_thread_result; |
| 494 | |
| 495 | // Wait for process to be stopped the the entry point by watching |
| 496 | // for the process status to be set to SSTOP which indicates it it |
| 497 | // SIGSTOP'ed at the entry point |
| 498 | WaitForProcessToSIGSTOP (pid, 5); |
| 499 | } |
| 500 | } |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 501 | ::unlink (unix_socket_name.c_str()); |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 502 | [applescript release]; |
| 503 | return pid; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | #define LLDB_HOST_USE_APPLESCRIPT |
| 508 | |
| 509 | lldb::pid_t |
| 510 | Host::LaunchInNewTerminal |
| 511 | ( |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 512 | const char *tty_name, |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 513 | const char **argv, |
| 514 | const char **envp, |
| 515 | const ArchSpec *arch_spec, |
| 516 | bool stop_at_entry, |
| 517 | bool disable_aslr |
| 518 | ) |
| 519 | { |
| 520 | #if defined (LLDB_HOST_USE_APPLESCRIPT) |
Greg Clayton | bb0c91f | 2010-10-19 23:16:00 +0000 | [diff] [blame] | 521 | return LaunchInNewTerminalWithAppleScript (tty_name, argv, envp, arch_spec, stop_at_entry, disable_aslr); |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 522 | #else |
| 523 | return LaunchInNewTerminalWithCommandFile (argv, envp, arch_spec, stop_at_entry, disable_aslr); |
Greg Clayton | ea3259d | 2010-10-19 17:03:58 +0000 | [diff] [blame] | 524 | #endif |
Greg Clayton | 6bd1a9e | 2010-10-19 18:15:50 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 527 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 528 | bool |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 529 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 530 | { |
| 531 | // We attach this to an 'odoc' event to specify a particular selection |
| 532 | typedef struct { |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 533 | int16_t reserved0; // must be zero |
| 534 | int16_t fLineNumber; |
| 535 | int32_t fSelStart; |
| 536 | int32_t fSelEnd; |
| 537 | uint32_t reserved1; // must be zero |
| 538 | uint32_t reserved2; // must be zero |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 539 | } BabelAESelInfo; |
| 540 | |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 541 | Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 542 | char file_path[PATH_MAX]; |
| 543 | file_spec.GetPath(file_path, PATH_MAX); |
| 544 | CFCString file_cfstr (file_path, kCFStringEncodingUTF8); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 545 | CFCReleaser<CFURLRef> file_URL (::CFURLCreateWithFileSystemPath (NULL, |
| 546 | file_cfstr.get(), |
| 547 | kCFURLPOSIXPathStyle, |
| 548 | false)); |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 549 | |
| 550 | if (log) |
| 551 | 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] | 552 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 553 | OSStatus error; |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 554 | BabelAESelInfo file_and_line_info = |
| 555 | { |
| 556 | 0, // reserved0 |
| 557 | line_no - 1, // fLineNumber (zero based line number) |
| 558 | 1, // fSelStart |
| 559 | 1024, // fSelEnd |
| 560 | 0, // reserved1 |
| 561 | 0 // reserved2 |
| 562 | }; |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 563 | |
| 564 | AEKeyDesc file_and_line_desc; |
| 565 | |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 566 | error = ::AECreateDesc (typeUTF8Text, |
| 567 | &file_and_line_info, |
| 568 | sizeof (file_and_line_info), |
| 569 | &(file_and_line_desc.descContent)); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 570 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 571 | if (error != noErr) |
| 572 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 573 | if (log) |
| 574 | log->Printf("Error creating AEDesc: %d.\n", error); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
| 578 | file_and_line_desc.descKey = keyAEPosition; |
| 579 | |
Greg Clayton | cb0989a | 2010-08-31 18:56:24 +0000 | [diff] [blame] | 580 | static std::string g_app_name; |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 581 | static FSRef g_app_fsref; |
| 582 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 583 | LSApplicationParameters app_params; |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 584 | ::bzero (&app_params, sizeof (app_params)); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 585 | app_params.flags = kLSLaunchDefaults | |
| 586 | kLSLaunchDontAddToRecents | |
| 587 | kLSLaunchDontSwitch; |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 588 | |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 589 | char *external_editor = ::getenv ("LLDB_EXTERNAL_EDITOR"); |
| 590 | |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 591 | if (external_editor) |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 592 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 593 | if (log) |
| 594 | log->Printf("Looking for external editor \"%s\".\n", external_editor); |
| 595 | |
Greg Clayton | cb0989a | 2010-08-31 18:56:24 +0000 | [diff] [blame] | 596 | 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] | 597 | { |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 598 | CFCString editor_name (external_editor, kCFStringEncodingUTF8); |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 599 | error = ::LSFindApplicationForInfo (kLSUnknownCreator, |
| 600 | NULL, |
| 601 | editor_name.get(), |
| 602 | &g_app_fsref, |
| 603 | NULL); |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 604 | |
| 605 | // 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] | 606 | if (error != noErr) |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 607 | { |
| 608 | if (log) |
| 609 | log->Printf("Could not find External Editor application, error: %d.\n", error); |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 610 | return false; |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 611 | } |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 612 | |
| 613 | } |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 614 | app_params.application = &g_app_fsref; |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 617 | ProcessSerialNumber psn; |
| 618 | 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] | 619 | error = ::LSOpenURLsWithRole (file_array.get(), |
Jim Ingham | 363180d | 2010-08-30 23:48:25 +0000 | [diff] [blame] | 620 | kLSRolesAll, |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 621 | &file_and_line_desc, |
| 622 | &app_params, |
| 623 | &psn, |
| 624 | 1); |
| 625 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 626 | AEDisposeDesc (&(file_and_line_desc.descContent)); |
| 627 | |
| 628 | if (error != noErr) |
| 629 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 630 | if (log) |
| 631 | log->Printf("LSOpenURLsWithRole failed, error: %d.\n", error); |
| 632 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 633 | return false; |
| 634 | } |
| 635 | |
| 636 | ProcessInfoRec which_process; |
| 637 | bzero(&which_process, sizeof(which_process)); |
| 638 | unsigned char ap_name[PATH_MAX]; |
| 639 | which_process.processName = ap_name; |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 640 | error = ::GetProcessInformation (&psn, &which_process); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 641 | |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 642 | bool using_xcode; |
| 643 | if (error != noErr) |
| 644 | { |
| 645 | if (log) |
| 646 | log->Printf("GetProcessInformation failed, error: %d.\n", error); |
| 647 | using_xcode = false; |
| 648 | } |
| 649 | else |
| 650 | 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] | 651 | |
| 652 | // Xcode doesn't obey the line number in the Open Apple Event. So I have to send |
| 653 | // it an AppleScript to focus on the right line. |
| 654 | |
| 655 | if (using_xcode) |
| 656 | { |
| 657 | static ComponentInstance osa_component = NULL; |
| 658 | static const char *as_template = "tell application \"Xcode\"\n" |
| 659 | "set doc to the first document whose path is \"%s\"\n" |
| 660 | "set the selection to paragraph %d of doc\n" |
| 661 | "--- set the selected paragraph range to {%d, %d} of doc\n" |
| 662 | "end tell\n"; |
| 663 | const int chars_for_int = 32; |
| 664 | static int as_template_len = strlen (as_template); |
| 665 | |
| 666 | |
| 667 | char *as_str; |
| 668 | AEDesc as_desc; |
| 669 | |
| 670 | if (osa_component == NULL) |
| 671 | { |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 672 | osa_component = ::OpenDefaultComponent (kOSAComponentType, |
| 673 | kAppleScriptSubtype); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (osa_component == NULL) |
| 677 | { |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 678 | if (log) |
| 679 | log->Printf("Could not get default AppleScript component.\n"); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | |
| 683 | uint32_t as_str_size = as_template_len + strlen (file_path) + 3 * chars_for_int + 1; |
| 684 | as_str = (char *) malloc (as_str_size); |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 685 | ::snprintf (as_str, |
| 686 | as_str_size - 1, |
| 687 | as_template, |
| 688 | file_path, |
| 689 | line_no, |
| 690 | line_no, |
| 691 | line_no); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 692 | |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 693 | error = ::AECreateDesc (typeChar, |
| 694 | as_str, |
| 695 | strlen (as_str), |
| 696 | &as_desc); |
| 697 | |
| 698 | ::free (as_str); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 699 | |
| 700 | if (error != noErr) |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 701 | { |
| 702 | if (log) |
| 703 | log->Printf("Failed to create AEDesc for Xcode AppleEvent: %d.\n", error); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 704 | return false; |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 705 | } |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 706 | |
| 707 | OSAID ret_OSAID; |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 708 | error = ::OSACompileExecute (osa_component, |
| 709 | &as_desc, |
| 710 | kOSANullScript, |
| 711 | kOSAModeNeverInteract, |
| 712 | &ret_OSAID); |
| 713 | |
| 714 | ::OSADispose (osa_component, ret_OSAID); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 715 | |
Greg Clayton | f4fa8a9 | 2010-08-30 22:00:34 +0000 | [diff] [blame] | 716 | ::AEDisposeDesc (&as_desc); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 717 | |
| 718 | if (error != noErr) |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 719 | { |
| 720 | if (log) |
| 721 | log->Printf("Sending AppleEvent to Xcode failed, error: %d.\n", error); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 722 | return false; |
Jim Ingham | b0fff35 | 2010-08-31 18:05:13 +0000 | [diff] [blame] | 723 | } |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | return true; |
| 727 | } |