blob: 16596aee278e6d735f5da4e204a401d0619cb9bc [file] [log] [blame]
Johnny Chenfc210782010-07-02 22:04:42 +00001"""Show global variables and check that they do indeed have global scopes."""
2
3import os, time
4import lldb
5import unittest
6
7main = False
8
9class TestClassTypes(unittest.TestCase):
10
11 def setUp(self):
12 global main
13
14 # Save old working directory.
15 self.oldcwd = os.getcwd()
16 # Change current working directory if ${LLDB_TEST} is defined.
17 if ("LLDB_TEST" in os.environ):
18 os.chdir(os.path.join(os.environ["LLDB_TEST"], "global_variables"));
19 self.dbg = lldb.SBDebugger.Create() if main else lldb.DBG
20 if not self.dbg.IsValid():
21 raise Exception('Invalid debugger instance')
22 self.dbg.SetAsync(False)
23 self.ci = self.dbg.GetCommandInterpreter()
24 if not self.ci:
25 raise Exception('Could not get the command interpreter')
26
27 def tearDown(self):
28 # Restore old working directory.
29 os.chdir(self.oldcwd)
30 del self.dbg
31
32 def test_global_variables(self):
33 """Test 'variable list -s -a' which omits args and shows scopes."""
34 res = lldb.SBCommandReturnObject()
35 exe = os.path.join(os.getcwd(), "a.out")
36 self.ci.HandleCommand("file " + exe, res)
37 self.assertTrue(res.Succeeded())
38
39 # Break inside the main.
40 self.ci.HandleCommand("breakpoint set -f main.c -l 20", res)
41 self.assertTrue(res.Succeeded())
42 self.assertTrue(res.GetOutput().startswith(
43 "Breakpoint created: 1: file ='main.c', line = 20, locations = 1"))
44
45 self.ci.HandleCommand("run", res)
46 time.sleep(0.1)
47 self.assertTrue(res.Succeeded())
48
49 # The stop reason of the thread should be breakpoint.
50 self.ci.HandleCommand("thread list", res)
51 print "thread list ->", res.GetOutput()
52 self.assertTrue(res.Succeeded())
53 self.assertTrue(res.GetOutput().find('state is Stopped') and
54 res.GetOutput().find('stop reason = breakpoint'))
55
56 # The breakpoint should have a hit count of 1.
57 self.ci.HandleCommand("breakpoint list", res)
58 self.assertTrue(res.Succeeded())
59 self.assertTrue(res.GetOutput().find(' resolved, hit count = 1'))
60
61 # Check that GLOBAL scopes are indicated for the variables.
62 self.ci.HandleCommand("variable list -s -a", res);
63 self.assertTrue(res.Succeeded())
64 self.assertTrue(res.GetOutput().find('GLOBAL: g_file_static_cstr') and
65 res.GetOutput().find('GLOBAL: g_file_global_int') and
66 res.GetOutput().find('GLOBAL: g_file_global_cstr'))
67
68 self.ci.HandleCommand("continue", res)
69 self.assertTrue(res.Succeeded())
70
71
72if __name__ == '__main__':
73 lldb.SBDebugger.Initialize()
74 main = True
75 unittest.main()
76 lldb.SBDebugger.Terminate()