aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJohnny Chen <johnny.chen@apple.com>2011-10-01 01:19:45 +0000
committerJohnny Chen <johnny.chen@apple.com>2011-10-01 01:19:45 +0000
commit5cac6a54b36dd4f9b0db570720b8d47a487343f9 (patch)
tree95e0b4132d56a27d380294ce05ca515e33f80777 /test
parent2f57db09a49f2a05a620b8163bbe1e748a46ec73 (diff)
downloadlldb-5cac6a54b36dd4f9b0db570720b8d47a487343f9.tar.gz
Add SBFrame.WatchLocation() to find and watch the location pointed to by
a variable usng the frame as the scope. Add TestSetWatchpoint.py to exercise this API. Also fix some SWIG Python docstrings. git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@140914 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r--test/python_api/watchpoint/TestSetWatchpoint.py2
-rw-r--r--test/python_api/watchpoint/watchlocation/Makefile5
-rw-r--r--test/python_api/watchpoint/watchlocation/TestSetWatchlocation.py89
-rw-r--r--test/python_api/watchpoint/watchlocation/main.cpp98
4 files changed, 193 insertions, 1 deletions
diff --git a/test/python_api/watchpoint/TestSetWatchpoint.py b/test/python_api/watchpoint/TestSetWatchpoint.py
index 8618962b2..61526ab0f 100644
--- a/test/python_api/watchpoint/TestSetWatchpoint.py
+++ b/test/python_api/watchpoint/TestSetWatchpoint.py
@@ -1,5 +1,5 @@
"""
-Use lldb Python SBFrame API to create a watchpoint for read_write of 'globl' var
+Use lldb Python SBFrame API to create a watchpoint for read_write of 'globl' var.
"""
import os, time
diff --git a/test/python_api/watchpoint/watchlocation/Makefile b/test/python_api/watchpoint/watchlocation/Makefile
new file mode 100644
index 000000000..314f1cb2f
--- /dev/null
+++ b/test/python_api/watchpoint/watchlocation/Makefile
@@ -0,0 +1,5 @@
+LEVEL = ../../../make
+
+CXX_SOURCES := main.cpp
+
+include $(LEVEL)/Makefile.rules
diff --git a/test/python_api/watchpoint/watchlocation/TestSetWatchlocation.py b/test/python_api/watchpoint/watchlocation/TestSetWatchlocation.py
new file mode 100644
index 000000000..7a78b1533
--- /dev/null
+++ b/test/python_api/watchpoint/watchlocation/TestSetWatchlocation.py
@@ -0,0 +1,89 @@
+"""
+Use lldb Python SBFrame.WatchLocation() API to create a watchpoint for write of '*g_char_ptr'.
+"""
+
+import os, time
+import re
+import unittest2
+import lldb, lldbutil
+from lldbtest import *
+
+class SetWatchlocationAPITestCase(TestBase):
+
+ mydir = os.path.join("python_api", "watchpoint", "watchlocation")
+
+ def setUp(self):
+ # Call super's setUp().
+ TestBase.setUp(self)
+ # Our simple source filename.
+ self.source = 'main.cpp'
+ # Find the line number to break inside main().
+ self.line = line_number(self.source, '// Set break point at this line.')
+ # This is for verifying that watch location works.
+ self.violating_func = "do_bad_thing_with_location";
+
+ @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+ @python_api_test
+ def test_watch_val_with_dsym(self):
+ """Exercise SBFrame.WatchLocation() API to set a watchpoint."""
+ self.buildDsym()
+ self.do_set_watchlocation()
+
+ @python_api_test
+ def test_watch_val_with_dwarf(self):
+ """Exercise SBFrame.WatchLocation() API to set a watchpoint."""
+ self.buildDwarf()
+ self.do_set_watchlocation()
+
+ def do_set_watchlocation(self):
+ """Use SBFrame.WatchLocation() to set a watchpoint and verify that the program stops later due to the watchpoint."""
+ exe = os.path.join(os.getcwd(), "a.out")
+
+ # Create a target by the debugger.
+ target = self.dbg.CreateTarget(exe)
+ self.assertTrue(target, VALID_TARGET)
+
+ # Now create a breakpoint on main.c.
+ breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
+ self.assertTrue(breakpoint and
+ breakpoint.GetNumLocations() == 1,
+ VALID_BREAKPOINT)
+
+ # Now launch the process, and do not stop at the entry point.
+ process = target.LaunchSimple(None, None, os.getcwd())
+
+ # We should be stopped due to the breakpoint. Get frame #0.
+ process = target.GetProcess()
+ self.assertTrue(process.GetState() == lldb.eStateStopped,
+ PROCESS_STOPPED)
+ thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
+ frame0 = thread.GetFrameAtIndex(0)
+
+ value = frame0.WatchLocation('g_char_ptr',
+ lldb.eValueTypeVariableGlobal,
+ lldb.LLDB_WATCH_TYPE_WRITE,
+ 1)
+ self.assertTrue(value, "Successfully found the location and set a watchpoint")
+ self.DebugSBValue(value)
+
+ # Continue. Expect the program to stop due to the variable being written to.
+ process.Continue()
+
+ if (self.TraceOn()):
+ lldbutil.print_stacktraces(process)
+
+ thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint)
+ self.assertTrue(thread, "The thread stopped due to watchpoint")
+ self.DebugSBValue(value)
+
+ self.expect(lldbutil.print_stacktrace(thread, string_buffer=True), exe=False,
+ substrs = [self.violating_func])
+
+ # This finishes our test.
+
+
+if __name__ == '__main__':
+ import atexit
+ lldb.SBDebugger.Initialize()
+ atexit.register(lambda: lldb.SBDebugger.Terminate())
+ unittest2.main()
diff --git a/test/python_api/watchpoint/watchlocation/main.cpp b/test/python_api/watchpoint/watchlocation/main.cpp
new file mode 100644
index 000000000..8515ca0cc
--- /dev/null
+++ b/test/python_api/watchpoint/watchlocation/main.cpp
@@ -0,0 +1,98 @@
+//===-- main.cpp ------------------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// C includes
+#include <pthread.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+pthread_t g_thread_1 = NULL;
+pthread_t g_thread_2 = NULL;
+pthread_t g_thread_3 = NULL;
+
+char *g_char_ptr = NULL;
+
+void
+do_bad_thing_with_location(char *char_ptr, char new_val)
+{
+ *char_ptr = new_val;
+}
+
+uint32_t access_pool (uint32_t flag = 0);
+
+uint32_t
+access_pool (uint32_t flag)
+{
+ static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER;
+ if (flag == 0)
+ ::pthread_mutex_lock (&g_access_mutex);
+
+ char old_val = *g_char_ptr;
+ if (flag != 0)
+ do_bad_thing_with_location(g_char_ptr, old_val + 1);
+
+ if (flag == 0)
+ ::pthread_mutex_unlock (&g_access_mutex);
+ return *g_char_ptr;
+}
+
+void *
+thread_func (void *arg)
+{
+ uint32_t thread_index = *((uint32_t *)arg);
+ printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
+
+ uint32_t count = 0;
+ uint32_t val;
+ while (count++ < 15)
+ {
+ // random micro second sleep from zero to 3 seconds
+ int usec = ::rand() % 3000000;
+ printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
+ ::usleep (usec);
+
+ if (count < 7)
+ val = access_pool ();
+ else
+ val = access_pool (1);
+
+ printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count);
+ }
+ printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
+ return NULL;
+}
+
+
+int main (int argc, char const *argv[])
+{
+ int err;
+ void *thread_result = NULL;
+ uint32_t thread_index_1 = 1;
+ uint32_t thread_index_2 = 2;
+ uint32_t thread_index_3 = 3;
+
+ g_char_ptr = (char *)malloc (1);
+ *g_char_ptr = 0;
+
+ // Create 3 threads
+ err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1);
+ err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2);
+ err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3);
+
+ printf ("Before turning all three threads loose...\n"); // Set break point at this line.
+
+ // Join all of our threads
+ err = ::pthread_join (g_thread_1, &thread_result);
+ err = ::pthread_join (g_thread_2, &thread_result);
+ err = ::pthread_join (g_thread_3, &thread_result);
+
+ return 0;
+}