summaryrefslogtreecommitdiffstats
path: root/packages/Python/lldbsuite/test/functionalities/process_attach
diff options
context:
space:
mode:
authordim <dim@FreeBSD.org>2016-01-06 20:12:03 +0000
committerdim <dim@FreeBSD.org>2016-01-06 20:12:03 +0000
commit78b9749c0a4ea980a8b934645da6ae98fcc665e8 (patch)
treedd2a1ddf0476664c2b823409c36cbccd52662ca7 /packages/Python/lldbsuite/test/functionalities/process_attach
parent60cb593f9d55fa5ca7a5372b731f2330345b4b9a (diff)
downloadFreeBSD-src-78b9749c0a4ea980a8b934645da6ae98fcc665e8.zip
FreeBSD-src-78b9749c0a4ea980a8b934645da6ae98fcc665e8.tar.gz
Vendor import of lldb trunk r256945:
https://llvm.org/svn/llvm-project/lldb/trunk@256945
Diffstat (limited to 'packages/Python/lldbsuite/test/functionalities/process_attach')
-rw-r--r--packages/Python/lldbsuite/test/functionalities/process_attach/Makefile7
-rw-r--r--packages/Python/lldbsuite/test/functionalities/process_attach/TestProcessAttach.py58
-rw-r--r--packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/Makefile7
-rw-r--r--packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/TestAttachDenied.py60
-rw-r--r--packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/main.cpp108
-rw-r--r--packages/Python/lldbsuite/test/functionalities/process_attach/main.cpp37
6 files changed, 277 insertions, 0 deletions
diff --git a/packages/Python/lldbsuite/test/functionalities/process_attach/Makefile b/packages/Python/lldbsuite/test/functionalities/process_attach/Makefile
new file mode 100644
index 0000000..a964853
--- /dev/null
+++ b/packages/Python/lldbsuite/test/functionalities/process_attach/Makefile
@@ -0,0 +1,7 @@
+LEVEL = ../../make
+
+CXX_SOURCES := main.cpp
+
+EXE := ProcessAttach
+
+include $(LEVEL)/Makefile.rules
diff --git a/packages/Python/lldbsuite/test/functionalities/process_attach/TestProcessAttach.py b/packages/Python/lldbsuite/test/functionalities/process_attach/TestProcessAttach.py
new file mode 100644
index 0000000..83906b5
--- /dev/null
+++ b/packages/Python/lldbsuite/test/functionalities/process_attach/TestProcessAttach.py
@@ -0,0 +1,58 @@
+"""
+Test process attach.
+"""
+
+from __future__ import print_function
+
+
+
+import os, time
+import lldb
+from lldbsuite.test.lldbtest import *
+import lldbsuite.test.lldbutil as lldbutil
+
+exe_name = "ProcessAttach" # Must match Makefile
+
+class ProcessAttachTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @skipIfiOSSimulator
+ def test_attach_to_process_by_id(self):
+ """Test attach by process id"""
+ self.build()
+ exe = os.path.join(os.getcwd(), exe_name)
+
+ # Spawn a new process
+ popen = self.spawnSubprocess(exe)
+ self.addTearDownHook(self.cleanupSubprocesses)
+
+ self.runCmd("process attach -p " + str(popen.pid))
+
+ target = self.dbg.GetSelectedTarget()
+
+ process = target.GetProcess()
+ self.assertTrue(process, PROCESS_IS_VALID)
+
+ def test_attach_to_process_by_name(self):
+ """Test attach by process name"""
+ self.build()
+ exe = os.path.join(os.getcwd(), exe_name)
+
+ # Spawn a new process
+ popen = self.spawnSubprocess(exe)
+ self.addTearDownHook(self.cleanupSubprocesses)
+
+ self.runCmd("process attach -n " + exe_name)
+
+ target = self.dbg.GetSelectedTarget()
+
+ process = target.GetProcess()
+ self.assertTrue(process, PROCESS_IS_VALID)
+
+ def tearDown(self):
+ # Destroy process before TestBase.tearDown()
+ self.dbg.GetSelectedTarget().GetProcess().Destroy()
+
+ # Call super's tearDown().
+ TestBase.tearDown(self)
diff --git a/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/Makefile b/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/Makefile
new file mode 100644
index 0000000..87600bb
--- /dev/null
+++ b/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/Makefile
@@ -0,0 +1,7 @@
+LEVEL = ../../../make
+
+CXX_SOURCES := main.cpp
+
+EXE := AttachDenied
+
+include $(LEVEL)/Makefile.rules
diff --git a/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/TestAttachDenied.py b/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/TestAttachDenied.py
new file mode 100644
index 0000000..ed9d58f
--- /dev/null
+++ b/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/TestAttachDenied.py
@@ -0,0 +1,60 @@
+"""
+Test denied process attach.
+"""
+
+from __future__ import print_function
+
+
+
+import os
+import time
+import lldb
+from lldbsuite.test.lldbtest import *
+
+exe_name = 'AttachDenied' # Must match Makefile
+
+class AttachDeniedTestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ def run_platform_command(self, cmd):
+ platform = self.dbg.GetSelectedPlatform()
+ shell_command = lldb.SBPlatformShellCommand(cmd)
+ err = platform.Run(shell_command)
+ return (err, shell_command.GetStatus(), shell_command.GetOutput())
+
+ @skipIfWindows
+ @skipIfiOSSimulator
+ def test_attach_to_process_by_id_denied(self):
+ """Test attach by process id denied"""
+ self.build()
+ exe = os.path.join(os.getcwd(), exe_name)
+
+ # Use a file as a synchronization point between test and inferior.
+ pid_file_path = lldbutil.append_to_process_working_directory(
+ "pid_file_%d" % (int(time.time())))
+ self.addTearDownHook(lambda: self.run_platform_command("rm %s" % (pid_file_path)))
+
+ # Spawn a new process
+ popen = self.spawnSubprocess(exe, [pid_file_path])
+ self.addTearDownHook(self.cleanupSubprocesses)
+
+ max_attempts = 5
+ for i in range(max_attempts):
+ err, retcode, msg = self.run_platform_command("ls %s" % pid_file_path)
+ if err.Success() and retcode == 0:
+ break
+ else:
+ print(msg)
+ if i < max_attempts:
+ # Exponential backoff!
+ time.sleep(pow(2, i) * 0.25)
+ else:
+ self.fail("Child PID file %s not found even after %d attempts." % (pid_file_path, max_attempts))
+ err, retcode, pid = self.run_platform_command("cat %s" % (pid_file_path))
+ self.assertTrue(err.Success() and retcode == 0,
+ "Failed to read file %s: %s, retcode: %d" % (pid_file_path, err.GetCString(), retcode))
+
+ self.expect('process attach -p ' + pid,
+ startstr = 'error: attach failed:',
+ error = True)
diff --git a/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/main.cpp b/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/main.cpp
new file mode 100644
index 0000000..ff1fcca
--- /dev/null
+++ b/packages/Python/lldbsuite/test/functionalities/process_attach/attach_denied/main.cpp
@@ -0,0 +1,108 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#if defined(PTRACE_ATTACH)
+#define ATTACH_REQUEST PTRACE_ATTACH
+#define DETACH_REQUEST PTRACE_DETACH
+#elif defined(PT_ATTACH)
+#define ATTACH_REQUEST PT_ATTACH
+#define DETACH_REQUEST PT_DETACH
+#else
+#error "Unsupported platform"
+#endif
+
+bool writePid (const char* file_name, const pid_t pid)
+{
+ char *tmp_file_name = (char *)malloc(strlen(file_name) + 16);
+ strcpy(tmp_file_name, file_name);
+ strcat(tmp_file_name, "_tmp");
+ int fd = open (tmp_file_name, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
+ if (fd == -1)
+ {
+ fprintf (stderr, "open(%s) failed: %s\n", tmp_file_name, strerror (errno));
+ free(tmp_file_name);
+ return false;
+ }
+ char buffer[64];
+ snprintf (buffer, sizeof(buffer), "%ld", (long)pid);
+
+ bool res = true;
+ if (write (fd, buffer, strlen (buffer)) == -1)
+ {
+ fprintf (stderr, "write(%s) failed: %s\n", buffer, strerror (errno));
+ res = false;
+ }
+ close (fd);
+
+ if (rename (tmp_file_name, file_name) == -1)
+ {
+ fprintf (stderr, "rename(%s, %s) failed: %s\n", tmp_file_name, file_name, strerror (errno));
+ res = false;
+ }
+ free(tmp_file_name);
+
+ return res;
+}
+
+void signal_handler (int)
+{
+}
+
+int main (int argc, char const *argv[])
+{
+ if (argc < 2)
+ {
+ fprintf (stderr, "invalid number of command line arguments\n");
+ return 1;
+ }
+
+ const pid_t pid = fork ();
+ if (pid == -1)
+ {
+ fprintf (stderr, "fork failed: %s\n", strerror (errno));
+ return 1;
+ }
+
+ if (pid > 0)
+ {
+ // Make pause call to return when a signal is received. Normally this happens when the
+ // test runner tries to terminate us.
+ signal (SIGHUP, signal_handler);
+ signal (SIGTERM, signal_handler);
+ if (ptrace (ATTACH_REQUEST, pid, NULL, 0) == -1)
+ {
+ fprintf (stderr, "ptrace(ATTACH) failed: %s\n", strerror (errno));
+ }
+ else
+ {
+ if (writePid (argv[1], pid))
+ pause (); // Waiting for the debugger trying attach to the child.
+
+ if (ptrace (DETACH_REQUEST, pid, NULL, 0) != 0)
+ fprintf (stderr, "ptrace(DETACH) failed: %s\n", strerror (errno));
+ }
+
+ kill (pid, SIGTERM);
+ int status = 0;
+ if (waitpid (pid, &status, 0) == -1)
+ fprintf (stderr, "waitpid failed: %s\n", strerror (errno));
+ }
+ else
+ {
+ // child inferior.
+ pause ();
+ }
+
+ printf ("Exiting now\n");
+ return 0;
+}
diff --git a/packages/Python/lldbsuite/test/functionalities/process_attach/main.cpp b/packages/Python/lldbsuite/test/functionalities/process_attach/main.cpp
new file mode 100644
index 0000000..8021fea
--- /dev/null
+++ b/packages/Python/lldbsuite/test/functionalities/process_attach/main.cpp
@@ -0,0 +1,37 @@
+#include <stdio.h>
+
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
+
+#include <chrono>
+#include <thread>
+
+int main(int argc, char const *argv[]) {
+ int temp;
+#if defined(__linux__)
+ // Immediately enable any ptracer so that we can allow the stub attach
+ // operation to succeed. Some Linux kernels are locked down so that
+ // only an ancestor process can be a ptracer of a process. This disables that
+ // restriction. Without it, attach-related stub tests will fail.
+#if defined(PR_SET_PTRACER) && defined(PR_SET_PTRACER_ANY)
+ int prctl_result;
+
+ // For now we execute on best effort basis. If this fails for
+ // some reason, so be it.
+ prctl_result = prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
+ (void) prctl_result;
+#endif
+#endif
+
+ // Waiting to be attached by the debugger.
+ temp = 0;
+
+ while (temp < 30) // Waiting to be attached...
+ {
+ std::this_thread::sleep_for(std::chrono::seconds(2));
+ temp++;
+ }
+
+ printf("Exiting now\n");
+}
OpenPOWER on IntegriCloud