File: | tools/lldb/source/Host/common/Host.cpp |
Location: | line 578, column 28 |
Description: | Call to function 'mktemp' is insecure as it always creates or uses insecure temporary file. Use 'mkstemp' instead |
1 | //===-- Host.cpp ------------------------------------------------*- 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 | |
10 | #include "lldb/lldb-python.h" |
11 | |
12 | // C includes |
13 | #include <errno(*__errno_location ()).h> |
14 | #include <limits.h> |
15 | #include <stdlib.h> |
16 | #include <sys/types.h> |
17 | #ifndef _WIN32 |
18 | #include <unistd.h> |
19 | #include <dlfcn.h> |
20 | #include <grp.h> |
21 | #include <netdb.h> |
22 | #include <pwd.h> |
23 | #include <sys/stat.h> |
24 | #endif |
25 | |
26 | #if defined (__APPLE__) |
27 | #include <mach/mach_port.h> |
28 | #include <mach/mach_init.h> |
29 | #include <mach-o/dyld.h> |
30 | #endif |
31 | |
32 | #if defined (__linux__1) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) || defined(__NetBSD__) |
33 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
34 | #include <spawn.h> |
35 | #endif |
36 | #include <sys/wait.h> |
37 | #include <sys/syscall.h> |
38 | #endif |
39 | |
40 | #if defined (__FreeBSD__) |
41 | #include <pthread_np.h> |
42 | #endif |
43 | |
44 | // C++ includes |
45 | #include <limits> |
46 | |
47 | #include "lldb/Host/Host.h" |
48 | #include "lldb/Host/HostInfo.h" |
49 | #include "lldb/Core/ArchSpec.h" |
50 | #include "lldb/Core/Debugger.h" |
51 | #include "lldb/Core/Error.h" |
52 | #include "lldb/Core/Log.h" |
53 | #include "lldb/Core/Module.h" |
54 | #include "lldb/Host/FileSpec.h" |
55 | #include "lldb/Host/HostProcess.h" |
56 | #include "lldb/Host/MonitoringProcessLauncher.h" |
57 | #include "lldb/Host/ProcessLauncher.h" |
58 | #include "lldb/Host/ThreadLauncher.h" |
59 | #include "lldb/lldb-private-forward.h" |
60 | #include "lldb/Target/FileAction.h" |
61 | #include "lldb/Target/ProcessLaunchInfo.h" |
62 | #include "lldb/Target/TargetList.h" |
63 | #include "lldb/Utility/CleanUp.h" |
64 | |
65 | #if defined(_WIN32) |
66 | #include "lldb/Host/windows/ProcessLauncherWindows.h" |
67 | #else |
68 | #include "lldb/Host/posix/ProcessLauncherPosix.h" |
69 | #endif |
70 | |
71 | #if defined (__APPLE__) |
72 | #ifndef _POSIX_SPAWN_DISABLE_ASLR |
73 | #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 |
74 | #endif |
75 | |
76 | extern "C" |
77 | { |
78 | int __pthread_chdir(const char *path); |
79 | int __pthread_fchdir (int fildes); |
80 | } |
81 | |
82 | #endif |
83 | |
84 | using namespace lldb; |
85 | using namespace lldb_private; |
86 | |
87 | #if !defined (__APPLE__) && !defined (_WIN32) |
88 | struct MonitorInfo |
89 | { |
90 | lldb::pid_t pid; // The process ID to monitor |
91 | Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals |
92 | void *callback_baton; // The callback baton for the callback function |
93 | bool monitor_signals; // If true, call the callback when "pid" gets signaled. |
94 | }; |
95 | |
96 | static thread_result_t |
97 | MonitorChildProcessThreadFunction (void *arg); |
98 | |
99 | HostThread |
100 | Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals) |
101 | { |
102 | MonitorInfo * info_ptr = new MonitorInfo(); |
103 | |
104 | info_ptr->pid = pid; |
105 | info_ptr->callback = callback; |
106 | info_ptr->callback_baton = callback_baton; |
107 | info_ptr->monitor_signals = monitor_signals; |
108 | |
109 | char thread_name[256]; |
110 | ::snprintf(thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64"l" "u" ")>", pid); |
111 | return ThreadLauncher::LaunchThread(thread_name, MonitorChildProcessThreadFunction, info_ptr, NULL__null); |
112 | } |
113 | |
114 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
115 | //------------------------------------------------------------------ |
116 | // Scoped class that will disable thread canceling when it is |
117 | // constructed, and exception safely restore the previous value it |
118 | // when it goes out of scope. |
119 | //------------------------------------------------------------------ |
120 | class ScopedPThreadCancelDisabler |
121 | { |
122 | public: |
123 | ScopedPThreadCancelDisabler() |
124 | { |
125 | // Disable the ability for this thread to be cancelled |
126 | int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLEPTHREAD_CANCEL_DISABLE, &m_old_state); |
127 | if (err != 0) |
128 | m_old_state = -1; |
129 | } |
130 | |
131 | ~ScopedPThreadCancelDisabler() |
132 | { |
133 | // Restore the ability for this thread to be cancelled to what it |
134 | // previously was. |
135 | if (m_old_state != -1) |
136 | ::pthread_setcancelstate (m_old_state, 0); |
137 | } |
138 | private: |
139 | int m_old_state; // Save the old cancelability state. |
140 | }; |
141 | #endif // __ANDROID_NDK__ |
142 | |
143 | static thread_result_t |
144 | MonitorChildProcessThreadFunction (void *arg) |
145 | { |
146 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS(1u << 1))); |
147 | const char *function = __FUNCTION__; |
148 | if (log) |
149 | log->Printf ("%s (arg = %p) thread starting...", function, arg); |
150 | |
151 | MonitorInfo *info = (MonitorInfo *)arg; |
152 | |
153 | const Host::MonitorChildProcessCallback callback = info->callback; |
154 | void * const callback_baton = info->callback_baton; |
155 | const bool monitor_signals = info->monitor_signals; |
156 | |
157 | assert (info->pid <= UINT32_MAX)((info->pid <= (4294967295U)) ? static_cast<void> (0) : __assert_fail ("info->pid <= (4294967295U)", "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn223349/tools/lldb/source/Host/common/Host.cpp" , 157, __PRETTY_FUNCTION__)); |
158 | const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid; |
159 | |
160 | delete info; |
161 | |
162 | int status = -1; |
163 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
164 | #define __WALL0x40000000 0 |
165 | #endif |
166 | const int options = __WALL0x40000000; |
167 | |
168 | while (1) |
169 | { |
170 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS(1u << 1)); |
171 | if (log) |
172 | log->Printf("%s ::wait_pid (pid = %" PRIi32"i" ", &status, options = %i)...", function, pid, options); |
173 | |
174 | // Wait for all child processes |
175 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
176 | ::pthread_testcancel (); |
177 | #endif |
178 | // Get signals from all children with same process group of pid |
179 | const ::pid_t wait_pid = ::waitpid (pid, &status, options); |
180 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
181 | ::pthread_testcancel (); |
182 | #endif |
183 | if (wait_pid == -1) |
184 | { |
185 | if (errno(*__errno_location ()) == EINTR4) |
186 | continue; |
187 | else |
188 | { |
189 | if (log) |
190 | log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno(*__errno_location ()))); |
191 | break; |
192 | } |
193 | } |
194 | else if (wait_pid > 0) |
195 | { |
196 | bool exited = false; |
197 | int signal = 0; |
198 | int exit_status = 0; |
199 | const char *status_cstr = NULL__null; |
200 | if (WIFSTOPPED(status)((((*(int *) &(status))) & 0xff) == 0x7f)) |
201 | { |
202 | signal = WSTOPSIG(status)((((*(int *) &(status))) & 0xff00) >> 8); |
203 | status_cstr = "STOPPED"; |
204 | } |
205 | else if (WIFEXITED(status)((((*(int *) &(status))) & 0x7f) == 0)) |
206 | { |
207 | exit_status = WEXITSTATUS(status)((((*(int *) &(status))) & 0xff00) >> 8); |
208 | status_cstr = "EXITED"; |
209 | exited = true; |
210 | } |
211 | else if (WIFSIGNALED(status)(((signed char) ((((*(int *) &(status))) & 0x7f) + 1) >> 1) > 0)) |
212 | { |
213 | signal = WTERMSIG(status)(((*(int *) &(status))) & 0x7f); |
214 | status_cstr = "SIGNALED"; |
215 | if (wait_pid == abs(pid)) { |
216 | exited = true; |
217 | exit_status = -1; |
218 | } |
219 | } |
220 | else |
221 | { |
222 | status_cstr = "(\?\?\?)"; |
223 | } |
224 | |
225 | // Scope for pthread_cancel_disabler |
226 | { |
227 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
228 | ScopedPThreadCancelDisabler pthread_cancel_disabler; |
229 | #endif |
230 | |
231 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS(1u << 1)); |
232 | if (log) |
233 | log->Printf ("%s ::waitpid (pid = %" PRIi32"i" ", &status, options = %i) => pid = %" PRIi32"i" ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
234 | function, |
235 | wait_pid, |
236 | options, |
237 | pid, |
238 | status, |
239 | status_cstr, |
240 | signal, |
241 | exit_status); |
242 | |
243 | if (exited || (signal != 0 && monitor_signals)) |
244 | { |
245 | bool callback_return = false; |
246 | if (callback) |
247 | callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status); |
248 | |
249 | // If our process exited, then this thread should exit |
250 | if (exited && wait_pid == abs(pid)) |
251 | { |
252 | if (log) |
253 | log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg); |
254 | break; |
255 | } |
256 | // If the callback returns true, it means this process should |
257 | // exit |
258 | if (callback_return) |
259 | { |
260 | if (log) |
261 | log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg); |
262 | break; |
263 | } |
264 | } |
265 | } |
266 | } |
267 | } |
268 | |
269 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS(1u << 1)); |
270 | if (log) |
271 | log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg); |
272 | |
273 | return NULL__null; |
274 | } |
275 | |
276 | #endif // #if !defined (__APPLE__) && !defined (_WIN32) |
277 | |
278 | #if !defined (__APPLE__) |
279 | |
280 | void |
281 | Host::SystemLog (SystemLogType type, const char *format, va_list args) |
282 | { |
283 | vfprintf (stderrstderr, format, args); |
284 | } |
285 | |
286 | #endif |
287 | |
288 | void |
289 | Host::SystemLog (SystemLogType type, const char *format, ...) |
290 | { |
291 | va_list args; |
292 | va_start (args, format)__builtin_va_start(args, format); |
293 | SystemLog (type, format, args); |
294 | va_end (args)__builtin_va_end(args); |
295 | } |
296 | |
297 | lldb::pid_t |
298 | Host::GetCurrentProcessID() |
299 | { |
300 | return ::getpid(); |
301 | } |
302 | |
303 | #ifndef _WIN32 |
304 | |
305 | lldb::tid_t |
306 | Host::GetCurrentThreadID() |
307 | { |
308 | #if defined (__APPLE__) |
309 | // Calling "mach_thread_self()" bumps the reference count on the thread |
310 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
311 | // count. |
312 | thread_port_t thread_self = mach_thread_self(); |
313 | mach_port_deallocate(mach_task_self(), thread_self); |
314 | return thread_self; |
315 | #elif defined(__FreeBSD__) |
316 | return lldb::tid_t(pthread_getthreadid_np()); |
317 | #elif defined(__ANDROID_NDK__) |
318 | return lldb::tid_t(gettid()); |
319 | #elif defined(__linux__1) |
320 | return lldb::tid_t(syscall(SYS_gettid186)); |
321 | #else |
322 | return lldb::tid_t(pthread_self()); |
323 | #endif |
324 | } |
325 | |
326 | lldb::thread_t |
327 | Host::GetCurrentThread () |
328 | { |
329 | return lldb::thread_t(pthread_self()); |
330 | } |
331 | |
332 | const char * |
333 | Host::GetSignalAsCString (int signo) |
334 | { |
335 | switch (signo) |
336 | { |
337 | case SIGHUP1: return "SIGHUP"; // 1 hangup |
338 | case SIGINT2: return "SIGINT"; // 2 interrupt |
339 | case SIGQUIT3: return "SIGQUIT"; // 3 quit |
340 | case SIGILL4: return "SIGILL"; // 4 illegal instruction (not reset when caught) |
341 | case SIGTRAP5: return "SIGTRAP"; // 5 trace trap (not reset when caught) |
342 | case SIGABRT6: return "SIGABRT"; // 6 abort() |
343 | #if defined(SIGPOLL29) |
344 | #if !defined(SIGIO29) || (SIGPOLL29 != SIGIO29) |
345 | // Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to |
346 | // fail with 'multiple define cases with same value' |
347 | case SIGPOLL29: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported) |
348 | #endif |
349 | #endif |
350 | #if defined(SIGEMT) |
351 | case SIGEMT: return "SIGEMT"; // 7 EMT instruction |
352 | #endif |
353 | case SIGFPE8: return "SIGFPE"; // 8 floating point exception |
354 | case SIGKILL9: return "SIGKILL"; // 9 kill (cannot be caught or ignored) |
355 | case SIGBUS7: return "SIGBUS"; // 10 bus error |
356 | case SIGSEGV11: return "SIGSEGV"; // 11 segmentation violation |
357 | case SIGSYS31: return "SIGSYS"; // 12 bad argument to system call |
358 | case SIGPIPE13: return "SIGPIPE"; // 13 write on a pipe with no one to read it |
359 | case SIGALRM14: return "SIGALRM"; // 14 alarm clock |
360 | case SIGTERM15: return "SIGTERM"; // 15 software termination signal from kill |
361 | case SIGURG23: return "SIGURG"; // 16 urgent condition on IO channel |
362 | case SIGSTOP19: return "SIGSTOP"; // 17 sendable stop signal not from tty |
363 | case SIGTSTP20: return "SIGTSTP"; // 18 stop signal from tty |
364 | case SIGCONT18: return "SIGCONT"; // 19 continue a stopped process |
365 | case SIGCHLD17: return "SIGCHLD"; // 20 to parent on child stop or exit |
366 | case SIGTTIN21: return "SIGTTIN"; // 21 to readers pgrp upon background tty read |
367 | case SIGTTOU22: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP) |
368 | #if defined(SIGIO29) |
369 | case SIGIO29: return "SIGIO"; // 23 input/output possible signal |
370 | #endif |
371 | case SIGXCPU24: return "SIGXCPU"; // 24 exceeded CPU time limit |
372 | case SIGXFSZ25: return "SIGXFSZ"; // 25 exceeded file size limit |
373 | case SIGVTALRM26: return "SIGVTALRM"; // 26 virtual time alarm |
374 | case SIGPROF27: return "SIGPROF"; // 27 profiling time alarm |
375 | #if defined(SIGWINCH28) |
376 | case SIGWINCH28: return "SIGWINCH"; // 28 window size changes |
377 | #endif |
378 | #if defined(SIGINFO) |
379 | case SIGINFO: return "SIGINFO"; // 29 information request |
380 | #endif |
381 | case SIGUSR110: return "SIGUSR1"; // 30 user defined signal 1 |
382 | case SIGUSR212: return "SIGUSR2"; // 31 user defined signal 2 |
383 | default: |
384 | break; |
385 | } |
386 | return NULL__null; |
387 | } |
388 | |
389 | #endif |
390 | |
391 | void |
392 | Host::WillTerminate () |
393 | { |
394 | } |
395 | |
396 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__1) // see macosx/Host.mm |
397 | |
398 | void |
399 | Host::Backtrace (Stream &strm, uint32_t max_frames) |
400 | { |
401 | // TODO: Is there a way to backtrace the current process on other systems? |
402 | } |
403 | |
404 | size_t |
405 | Host::GetEnvironment (StringList &env) |
406 | { |
407 | // TODO: Is there a way to the host environment for this process on other systems? |
408 | return 0; |
409 | } |
410 | |
411 | #endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) |
412 | |
413 | #ifndef _WIN32 |
414 | |
415 | lldb::thread_key_t |
416 | Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) |
417 | { |
418 | pthread_key_t key; |
419 | ::pthread_key_create (&key, callback); |
420 | return key; |
421 | } |
422 | |
423 | void* |
424 | Host::ThreadLocalStorageGet(lldb::thread_key_t key) |
425 | { |
426 | return ::pthread_getspecific (key); |
427 | } |
428 | |
429 | void |
430 | Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) |
431 | { |
432 | ::pthread_setspecific (key, value); |
433 | } |
434 | |
435 | #endif |
436 | |
437 | #if !defined (__APPLE__) // see Host.mm |
438 | |
439 | bool |
440 | Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle) |
441 | { |
442 | bundle.Clear(); |
443 | return false; |
444 | } |
445 | |
446 | bool |
447 | Host::ResolveExecutableInBundle (FileSpec &file) |
448 | { |
449 | return false; |
450 | } |
451 | #endif |
452 | |
453 | #ifndef _WIN32 |
454 | |
455 | FileSpec |
456 | Host::GetModuleFileSpecForHostAddress (const void *host_addr) |
457 | { |
458 | FileSpec module_filespec; |
459 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
460 | Dl_info info; |
461 | if (::dladdr (host_addr, &info)) |
462 | { |
463 | if (info.dli_fname) |
464 | module_filespec.SetFile(info.dli_fname, true); |
465 | } |
466 | #else |
467 | assert(false && "dladdr() not supported on Android")((false && "dladdr() not supported on Android") ? static_cast <void> (0) : __assert_fail ("false && \"dladdr() not supported on Android\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn223349/tools/lldb/source/Host/common/Host.cpp" , 467, __PRETTY_FUNCTION__)); |
468 | #endif |
469 | return module_filespec; |
470 | } |
471 | |
472 | #endif |
473 | |
474 | #if !defined(__linux__1) |
475 | bool |
476 | Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) |
477 | { |
478 | return false; |
479 | } |
480 | #endif |
481 | |
482 | struct ShellInfo |
483 | { |
484 | ShellInfo () : |
485 | process_reaped (false), |
486 | can_delete (false), |
487 | pid (LLDB_INVALID_PROCESS_ID0), |
488 | signo(-1), |
489 | status(-1) |
490 | { |
491 | } |
492 | |
493 | lldb_private::Predicate<bool> process_reaped; |
494 | lldb_private::Predicate<bool> can_delete; |
495 | lldb::pid_t pid; |
496 | int signo; |
497 | int status; |
498 | }; |
499 | |
500 | static bool |
501 | MonitorShellCommand (void *callback_baton, |
502 | lldb::pid_t pid, |
503 | bool exited, // True if the process did exit |
504 | int signo, // Zero for no signal |
505 | int status) // Exit value of process if signal is zero |
506 | { |
507 | ShellInfo *shell_info = (ShellInfo *)callback_baton; |
508 | shell_info->pid = pid; |
509 | shell_info->signo = signo; |
510 | shell_info->status = status; |
511 | // Let the thread running Host::RunShellCommand() know that the process |
512 | // exited and that ShellInfo has been filled in by broadcasting to it |
513 | shell_info->process_reaped.SetValue(1, eBroadcastAlways); |
514 | // Now wait for a handshake back from that thread running Host::RunShellCommand |
515 | // so we know that we can delete shell_info_ptr |
516 | shell_info->can_delete.WaitForValueEqualTo(true); |
517 | // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete... |
518 | usleep(1000); |
519 | // Now delete the shell info that was passed into this function |
520 | delete shell_info; |
521 | return true; |
522 | } |
523 | |
524 | Error |
525 | Host::RunShellCommand (const char *command, |
526 | const char *working_dir, |
527 | int *status_ptr, |
528 | int *signo_ptr, |
529 | std::string *command_output_ptr, |
530 | uint32_t timeout_sec, |
531 | bool run_in_default_shell) |
532 | { |
533 | Error error; |
534 | ProcessLaunchInfo launch_info; |
535 | if (run_in_default_shell) |
536 | { |
537 | // Run the command in a shell |
538 | launch_info.SetShell(HostInfo::GetDefaultShell()); |
539 | launch_info.GetArguments().AppendArgument(command); |
540 | const bool localhost = true; |
541 | const bool will_debug = false; |
542 | const bool first_arg_is_full_shell_command = true; |
543 | launch_info.ConvertArgumentsForLaunchingInShell (error, |
544 | localhost, |
545 | will_debug, |
546 | first_arg_is_full_shell_command, |
547 | 0); |
548 | } |
549 | else |
550 | { |
551 | // No shell, just run it |
552 | Args args (command); |
553 | const bool first_arg_is_executable = true; |
554 | launch_info.SetArguments(args, first_arg_is_executable); |
555 | } |
556 | |
557 | if (working_dir) |
558 | launch_info.SetWorkingDirectory(working_dir); |
559 | char output_file_path_buffer[PATH_MAX4096]; |
560 | const char *output_file_path = NULL__null; |
561 | |
562 | if (command_output_ptr) |
563 | { |
564 | // Create a temporary file to get the stdout/stderr and redirect the |
565 | // output of the command into this file. We will later read this file |
566 | // if all goes well and fill the data into "command_output_ptr" |
567 | FileSpec tmpdir_file_spec; |
568 | if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
569 | { |
570 | tmpdir_file_spec.AppendPathComponent("lldb-shell-output.XXXXXX"); |
571 | strncpy(output_file_path_buffer, tmpdir_file_spec.GetPath().c_str(), sizeof(output_file_path_buffer)); |
572 | } |
573 | else |
574 | { |
575 | strncpy(output_file_path_buffer, "/tmp/lldb-shell-output.XXXXXX", sizeof(output_file_path_buffer)); |
576 | } |
577 | |
578 | output_file_path = ::mktemp(output_file_path_buffer); |
Call to function 'mktemp' is insecure as it always creates or uses insecure temporary file. Use 'mkstemp' instead | |
579 | } |
580 | |
581 | launch_info.AppendSuppressFileAction (STDIN_FILENO0, true, false); |
582 | if (output_file_path) |
583 | { |
584 | launch_info.AppendOpenFileAction(STDOUT_FILENO1, output_file_path, false, true); |
585 | launch_info.AppendDuplicateFileAction(STDOUT_FILENO1, STDERR_FILENO2); |
586 | } |
587 | else |
588 | { |
589 | launch_info.AppendSuppressFileAction (STDOUT_FILENO1, false, true); |
590 | launch_info.AppendSuppressFileAction (STDERR_FILENO2, false, true); |
591 | } |
592 | |
593 | // The process monitor callback will delete the 'shell_info_ptr' below... |
594 | std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo()); |
595 | |
596 | const bool monitor_signals = false; |
597 | launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals); |
598 | |
599 | error = LaunchProcess (launch_info); |
600 | const lldb::pid_t pid = launch_info.GetProcessID(); |
601 | |
602 | if (error.Success() && pid == LLDB_INVALID_PROCESS_ID0) |
603 | error.SetErrorString("failed to get process ID"); |
604 | |
605 | if (error.Success()) |
606 | { |
607 | // The process successfully launched, so we can defer ownership of |
608 | // "shell_info" to the MonitorShellCommand callback function that will |
609 | // get called when the process dies. We release the unique pointer as it |
610 | // doesn't need to delete the ShellInfo anymore. |
611 | ShellInfo *shell_info = shell_info_ap.release(); |
612 | TimeValue *timeout_ptr = nullptr; |
613 | TimeValue timeout_time(TimeValue::Now()); |
614 | if (timeout_sec > 0) { |
615 | timeout_time.OffsetWithSeconds(timeout_sec); |
616 | timeout_ptr = &timeout_time; |
617 | } |
618 | bool timed_out = false; |
619 | shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out); |
620 | if (timed_out) |
621 | { |
622 | error.SetErrorString("timed out waiting for shell command to complete"); |
623 | |
624 | // Kill the process since it didn't complete within the timeout specified |
625 | Kill (pid, SIGKILL9); |
626 | // Wait for the monitor callback to get the message |
627 | timeout_time = TimeValue::Now(); |
628 | timeout_time.OffsetWithSeconds(1); |
629 | timed_out = false; |
630 | shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out); |
631 | } |
632 | else |
633 | { |
634 | if (status_ptr) |
635 | *status_ptr = shell_info->status; |
636 | |
637 | if (signo_ptr) |
638 | *signo_ptr = shell_info->signo; |
639 | |
640 | if (command_output_ptr) |
641 | { |
642 | command_output_ptr->clear(); |
643 | FileSpec file_spec(output_file_path, File::eOpenOptionRead); |
644 | uint64_t file_size = file_spec.GetByteSize(); |
645 | if (file_size > 0) |
646 | { |
647 | if (file_size > command_output_ptr->max_size()) |
648 | { |
649 | error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string"); |
650 | } |
651 | else |
652 | { |
653 | command_output_ptr->resize(file_size); |
654 | file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error); |
655 | } |
656 | } |
657 | } |
658 | } |
659 | shell_info->can_delete.SetValue(true, eBroadcastAlways); |
660 | } |
661 | |
662 | if (output_file_path) |
663 | ::unlink (output_file_path); |
664 | // Handshake with the monitor thread, or just let it know in advance that |
665 | // it can delete "shell_info" in case we timed out and were not able to kill |
666 | // the process... |
667 | return error; |
668 | } |
669 | |
670 | |
671 | // LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC |
672 | // systems |
673 | |
674 | #if defined (__APPLE__) || defined (__linux__1) || defined (__FreeBSD__) || defined (__GLIBC__2) || defined(__NetBSD__) |
675 | // this method needs to be visible to macosx/Host.cpp and |
676 | // common/Host.cpp. |
677 | |
678 | short |
679 | Host::GetPosixspawnFlags(const ProcessLaunchInfo &launch_info) |
680 | { |
681 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
682 | short flags = POSIX_SPAWN_SETSIGDEF0x04 | POSIX_SPAWN_SETSIGMASK0x08; |
683 | |
684 | #if defined (__APPLE__) |
685 | if (launch_info.GetFlags().Test (eLaunchFlagExec)) |
686 | flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag |
687 | |
688 | if (launch_info.GetFlags().Test (eLaunchFlagDebug)) |
689 | flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag |
690 | |
691 | if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR)) |
692 | flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag |
693 | |
694 | if (launch_info.GetLaunchInSeparateProcessGroup()) |
695 | flags |= POSIX_SPAWN_SETPGROUP0x02; |
696 | |
697 | #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT |
698 | #if defined (__APPLE__) && (defined (__x86_64__1) || defined (__i386__)) |
699 | static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate; |
700 | if (g_use_close_on_exec_flag == eLazyBoolCalculate) |
701 | { |
702 | g_use_close_on_exec_flag = eLazyBoolNo; |
703 | |
704 | uint32_t major, minor, update; |
705 | if (HostInfo::GetOSVersion(major, minor, update)) |
706 | { |
707 | // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier |
708 | if (major > 10 || (major == 10 && minor > 7)) |
709 | { |
710 | // Only enable for 10.8 and later OS versions |
711 | g_use_close_on_exec_flag = eLazyBoolYes; |
712 | } |
713 | } |
714 | } |
715 | #else |
716 | static LazyBool g_use_close_on_exec_flag = eLazyBoolYes; |
717 | #endif |
718 | // Close all files exception those with file actions if this is supported. |
719 | if (g_use_close_on_exec_flag == eLazyBoolYes) |
720 | flags |= POSIX_SPAWN_CLOEXEC_DEFAULT; |
721 | #endif |
722 | #endif // #if defined (__APPLE__) |
723 | return flags; |
724 | #else |
725 | assert(false && "Host::GetPosixspawnFlags() not supported on Android")((false && "Host::GetPosixspawnFlags() not supported on Android" ) ? static_cast<void> (0) : __assert_fail ("false && \"Host::GetPosixspawnFlags() not supported on Android\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn223349/tools/lldb/source/Host/common/Host.cpp" , 725, __PRETTY_FUNCTION__)); |
726 | return 0; |
727 | #endif |
728 | } |
729 | |
730 | Error |
731 | Host::LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &launch_info, lldb::pid_t &pid) |
732 | { |
733 | Error error; |
734 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
735 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST(1u << 14) | LIBLLDB_LOG_PROCESS(1u << 1))); |
736 | |
737 | posix_spawnattr_t attr; |
738 | error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX); |
739 | |
740 | if (error.Fail() || log) |
741 | error.PutToLog(log, "::posix_spawnattr_init ( &attr )"); |
742 | if (error.Fail()) |
743 | return error; |
744 | |
745 | // Make a quick class that will cleanup the posix spawn attributes in case |
746 | // we return in the middle of this function. |
747 | lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy); |
748 | |
749 | sigset_t no_signals; |
750 | sigset_t all_signals; |
751 | sigemptyset (&no_signals); |
752 | sigfillset (&all_signals); |
753 | ::posix_spawnattr_setsigmask(&attr, &no_signals); |
754 | #if defined (__linux__1) || defined (__FreeBSD__) |
755 | ::posix_spawnattr_setsigdefault(&attr, &no_signals); |
756 | #else |
757 | ::posix_spawnattr_setsigdefault(&attr, &all_signals); |
758 | #endif |
759 | |
760 | short flags = GetPosixspawnFlags(launch_info); |
761 | |
762 | error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX); |
763 | if (error.Fail() || log) |
764 | error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags); |
765 | if (error.Fail()) |
766 | return error; |
767 | |
768 | // posix_spawnattr_setbinpref_np appears to be an Apple extension per: |
769 | // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/ |
770 | #if defined (__APPLE__) && !defined (__arm__) |
771 | |
772 | // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell |
773 | // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation |
774 | // to do that job on OSX. |
775 | |
776 | if (launch_info.GetShell() == nullptr) |
777 | { |
778 | // We don't need to do this for ARM, and we really shouldn't now that we |
779 | // have multiple CPU subtypes and no posix_spawnattr call that allows us |
780 | // to set which CPU subtype to launch... |
781 | const ArchSpec &arch_spec = launch_info.GetArchitecture(); |
782 | cpu_type_t cpu = arch_spec.GetMachOCPUType(); |
783 | cpu_type_t sub = arch_spec.GetMachOCPUSubType(); |
784 | if (cpu != 0 && |
785 | cpu != static_cast<cpu_type_t>(UINT32_MAX(4294967295U)) && |
786 | cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE(0xFFFFFFFEu)) && |
787 | !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail |
788 | { |
789 | size_t ocount = 0; |
790 | error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX); |
791 | if (error.Fail() || log) |
792 | error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %llu )", cpu, (uint64_t)ocount); |
793 | |
794 | if (error.Fail() || ocount != 1) |
795 | return error; |
796 | } |
797 | } |
798 | |
799 | #endif |
800 | |
801 | const char *tmp_argv[2]; |
802 | char * const *argv = (char * const*)launch_info.GetArguments().GetConstArgumentVector(); |
803 | char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector(); |
804 | if (argv == NULL__null) |
805 | { |
806 | // posix_spawn gets very unhappy if it doesn't have at least the program |
807 | // name in argv[0]. One of the side affects I have noticed is the environment |
808 | // variables don't make it into the child process if "argv == NULL"!!! |
809 | tmp_argv[0] = exe_path; |
810 | tmp_argv[1] = NULL__null; |
811 | argv = (char * const*)tmp_argv; |
812 | } |
813 | |
814 | #if !defined (__APPLE__) |
815 | // manage the working directory |
816 | char current_dir[PATH_MAX4096]; |
817 | current_dir[0] = '\0'; |
818 | #endif |
819 | |
820 | const char *working_dir = launch_info.GetWorkingDirectory(); |
821 | if (working_dir) |
822 | { |
823 | #if defined (__APPLE__) |
824 | // Set the working directory on this thread only |
825 | if (__pthread_chdir (working_dir) < 0) { |
826 | if (errno(*__errno_location ()) == ENOENT2) { |
827 | error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); |
828 | } else if (errno(*__errno_location ()) == ENOTDIR20) { |
829 | error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir); |
830 | } else { |
831 | error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution."); |
832 | } |
833 | return error; |
834 | } |
835 | #else |
836 | if (::getcwd(current_dir, sizeof(current_dir)) == NULL__null) |
837 | { |
838 | error.SetError(errno(*__errno_location ()), eErrorTypePOSIX); |
839 | error.LogIfError(log, "unable to save the current directory"); |
840 | return error; |
841 | } |
842 | |
843 | if (::chdir(working_dir) == -1) |
844 | { |
845 | error.SetError(errno(*__errno_location ()), eErrorTypePOSIX); |
846 | error.LogIfError(log, "unable to change working directory to %s", working_dir); |
847 | return error; |
848 | } |
849 | #endif |
850 | } |
851 | |
852 | ::pid_t result_pid = LLDB_INVALID_PROCESS_ID0; |
853 | const size_t num_file_actions = launch_info.GetNumFileActions (); |
854 | if (num_file_actions > 0) |
855 | { |
856 | posix_spawn_file_actions_t file_actions; |
857 | error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX); |
858 | if (error.Fail() || log) |
859 | error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )"); |
860 | if (error.Fail()) |
861 | return error; |
862 | |
863 | // Make a quick class that will cleanup the posix spawn attributes in case |
864 | // we return in the middle of this function. |
865 | lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy); |
866 | |
867 | for (size_t i=0; i<num_file_actions; ++i) |
868 | { |
869 | const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i); |
870 | if (launch_file_action) |
871 | { |
872 | if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log, error)) |
873 | return error; |
874 | } |
875 | } |
876 | |
877 | error.SetError(::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp), eErrorTypePOSIX); |
878 | |
879 | if (error.Fail() || log) |
880 | { |
881 | error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", result_pid, |
882 | exe_path, static_cast<void *>(&file_actions), static_cast<void *>(&attr), reinterpret_cast<const void *>(argv), |
883 | reinterpret_cast<const void *>(envp)); |
884 | if (log) |
885 | { |
886 | for (int ii=0; argv[ii]; ++ii) |
887 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
888 | } |
889 | } |
890 | |
891 | } |
892 | else |
893 | { |
894 | error.SetError(::posix_spawnp(&result_pid, exe_path, NULL__null, &attr, argv, envp), eErrorTypePOSIX); |
895 | |
896 | if (error.Fail() || log) |
897 | { |
898 | error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = NULL, attr = %p, argv = %p, envp = %p )", |
899 | result_pid, exe_path, static_cast<void *>(&attr), reinterpret_cast<const void *>(argv), |
900 | reinterpret_cast<const void *>(envp)); |
901 | if (log) |
902 | { |
903 | for (int ii=0; argv[ii]; ++ii) |
904 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
905 | } |
906 | } |
907 | } |
908 | pid = result_pid; |
909 | |
910 | if (working_dir) |
911 | { |
912 | #if defined (__APPLE__) |
913 | // No more thread specific current working directory |
914 | __pthread_fchdir (-1); |
915 | #else |
916 | if (::chdir(current_dir) == -1 && error.Success()) |
917 | { |
918 | error.SetError(errno(*__errno_location ()), eErrorTypePOSIX); |
919 | error.LogIfError(log, "unable to change current directory back to %s", |
920 | current_dir); |
921 | } |
922 | #endif |
923 | } |
924 | #else |
925 | error.SetErrorString("Host::LaunchProcessPosixSpawn() not supported on Android"); |
926 | #endif |
927 | |
928 | return error; |
929 | } |
930 | |
931 | bool |
932 | Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error) |
933 | { |
934 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
935 | if (info == NULL__null) |
936 | return false; |
937 | |
938 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
939 | |
940 | switch (info->GetAction()) |
941 | { |
942 | case FileAction::eFileActionNone: |
943 | error.Clear(); |
944 | break; |
945 | |
946 | case FileAction::eFileActionClose: |
947 | if (info->GetFD() == -1) |
948 | error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)"); |
949 | else |
950 | { |
951 | error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX); |
952 | if (log && (error.Fail() || log)) |
953 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
954 | static_cast<void *>(file_actions), info->GetFD()); |
955 | } |
956 | break; |
957 | |
958 | case FileAction::eFileActionDuplicate: |
959 | if (info->GetFD() == -1) |
960 | error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
961 | else if (info->GetActionArgument() == -1) |
962 | error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
963 | else |
964 | { |
965 | error.SetError( |
966 | ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()), |
967 | eErrorTypePOSIX); |
968 | if (log && (error.Fail() || log)) |
969 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
970 | static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument()); |
971 | } |
972 | break; |
973 | |
974 | case FileAction::eFileActionOpen: |
975 | if (info->GetFD() == -1) |
976 | error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)"); |
977 | else |
978 | { |
979 | int oflag = info->GetActionArgument(); |
980 | |
981 | mode_t mode = 0; |
982 | |
983 | if (oflag & O_CREAT0100) |
984 | mode = 0640; |
985 | |
986 | error.SetError( |
987 | ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode), |
988 | eErrorTypePOSIX); |
989 | if (error.Fail() || log) |
990 | error.PutToLog(log, |
991 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
992 | static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode); |
993 | } |
994 | break; |
995 | } |
996 | return error.Success(); |
997 | #else |
998 | error.SetErrorString("Host::AddPosixSpawnFileAction() not supported on Android"); |
999 | return false; |
1000 | #endif |
1001 | } |
1002 | #endif // LaunchProcedssPosixSpawn: Apple, Linux, FreeBSD and other GLIBC systems |
1003 | |
1004 | #if defined(__linux__1) || defined(__FreeBSD__) || defined(__GLIBC__2) || defined(__NetBSD__) || defined(_WIN32) |
1005 | // The functions below implement process launching via posix_spawn() for Linux, |
1006 | // FreeBSD and NetBSD. |
1007 | |
1008 | Error |
1009 | Host::LaunchProcess (ProcessLaunchInfo &launch_info) |
1010 | { |
1011 | std::unique_ptr<ProcessLauncher> delegate_launcher; |
1012 | #if defined(_WIN32) |
1013 | delegate_launcher.reset(new ProcessLauncherWindows()); |
1014 | #else |
1015 | delegate_launcher.reset(new ProcessLauncherPosix()); |
1016 | #endif |
1017 | MonitoringProcessLauncher launcher(std::move(delegate_launcher)); |
1018 | |
1019 | Error error; |
1020 | HostProcess process = launcher.LaunchProcess(launch_info, error); |
1021 | |
1022 | // TODO(zturner): It would be better if the entire HostProcess were returned instead of writing |
1023 | // it into this structure. |
1024 | launch_info.SetProcessID(process.GetProcessId()); |
1025 | |
1026 | return error; |
1027 | } |
1028 | #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
1029 | |
1030 | #ifndef _WIN32 |
1031 | void |
1032 | Host::Kill(lldb::pid_t pid, int signo) |
1033 | { |
1034 | ::kill(pid, signo); |
1035 | } |
1036 | |
1037 | #endif |
1038 | |
1039 | #if !defined (__APPLE__) |
1040 | bool |
1041 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
1042 | { |
1043 | return false; |
1044 | } |
1045 | |
1046 | void |
1047 | Host::SetCrashDescriptionWithFormat (const char *format, ...) |
1048 | { |
1049 | } |
1050 | |
1051 | void |
1052 | Host::SetCrashDescription (const char *description) |
1053 | { |
1054 | } |
1055 | |
1056 | lldb::pid_t |
1057 | Host::LaunchApplication (const FileSpec &app_file_spec) |
1058 | { |
1059 | return LLDB_INVALID_PROCESS_ID0; |
1060 | } |
1061 | |
1062 | #endif |
1063 | |
1064 | #if !defined (__linux__1) && !defined (__FreeBSD__) && !defined (__NetBSD__) |
1065 | |
1066 | const lldb_private::UnixSignalsSP& |
1067 | Host::GetUnixSignals () |
1068 | { |
1069 | static UnixSignalsSP s_unix_signals_sp (new UnixSignals ()); |
1070 | return s_unix_signals_sp; |
1071 | } |
1072 | |
1073 | #endif |