File: TestObjCExceptions.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (258 lines) | stat: -rw-r--r-- 9,085 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# encoding: utf-8
"""
Test lldb Obj-C exception support.
"""


import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class ObjCExceptionsTestCase(TestBase):
    @skipIf(compiler="clang", compiler_version=["<", "13.0"])
    def test_objc_exceptions_at_throw(self):
        self.build()

        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
        self.assertTrue(target, VALID_TARGET)

        launch_info = lldb.SBLaunchInfo(["a.out", "0"])
        launch_info.SetLaunchFlags(lldb.eLaunchFlagInheritTCCFromParent)
        lldbutil.run_to_name_breakpoint(
            self, "objc_exception_throw", launch_info=launch_info
        )

        self.expect(
            "thread list",
            substrs=["stopped", "stop reason = hit Objective-C exception"],
        )

        self.expect(
            "thread exception",
            substrs=[
                "(NSException *) exception = ",
                '"SomeReason"',
            ],
        )

        target = self.dbg.GetSelectedTarget()
        thread = target.GetProcess().GetSelectedThread()
        frame = thread.GetSelectedFrame()

        opts = lldb.SBVariablesOptions()
        opts.SetIncludeRecognizedArguments(True)
        variables = frame.GetVariables(opts)

        self.assertEqual(variables.GetSize(), 1)
        self.assertEqual(variables.GetValueAtIndex(0).name, "exception")
        self.assertEqual(
            variables.GetValueAtIndex(0).GetValueType(), lldb.eValueTypeVariableArgument
        )

        lldbutil.run_to_source_breakpoint(
            self,
            "// Set break point at this line.",
            lldb.SBFileSpec("main.mm"),
            launch_info=launch_info,
        )

        self.expect(
            "thread list",
            STOPPED_DUE_TO_BREAKPOINT,
            substrs=["stopped", "stop reason = breakpoint"],
        )

        target = self.dbg.GetSelectedTarget()
        thread = target.GetProcess().GetSelectedThread()
        frame = thread.GetSelectedFrame()

        # No exception being currently thrown/caught at this point
        self.assertFalse(thread.GetCurrentException().IsValid())
        self.assertFalse(thread.GetCurrentExceptionBacktrace().IsValid())

        self.expect(
            "frame variable e1", substrs=["(NSException *) e1 = ", '"SomeReason"']
        )

        self.expect(
            "frame variable *e1",
            substrs=[
                "(NSException) *e1 = ",
                "name = ",
                '"ExceptionName"',
                "reason = ",
                '"SomeReason"',
                "userInfo = ",
                "1 key/value pair",
                "reserved = ",
            ],
        )

        e1 = frame.FindVariable("e1")
        self.assertTrue(e1)
        self.assertEqual(e1.type.name, "NSException *")
        self.assertEqual(e1.GetSummary(), '"SomeReason"')
        self.assertEqual(e1.GetChildMemberWithName("name").description, "ExceptionName")
        self.assertEqual(e1.GetChildMemberWithName("reason").description, "SomeReason")
        userInfo = e1.GetChildMemberWithName("userInfo").dynamic
        self.assertEqual(userInfo.summary, "1 key/value pair")
        self.assertEqual(
            userInfo.GetChildAtIndex(0).GetChildAtIndex(0).description, "some_key"
        )
        self.assertEqual(
            userInfo.GetChildAtIndex(0).GetChildAtIndex(1).description, "some_value"
        )

        self.expect(
            "frame variable e2", substrs=["(NSException *) e2 = ", '"SomeReason"']
        )

        self.expect(
            "frame variable *e2",
            substrs=[
                "(NSException) *e2 = ",
                "name = ",
                '"ThrownException"',
                "reason = ",
                '"SomeReason"',
                "userInfo = ",
                "1 key/value pair",
                "reserved = ",
            ],
        )

        e2 = frame.FindVariable("e2")
        self.assertTrue(e2)
        self.assertEqual(e2.type.name, "NSException *")
        self.assertEqual(e2.GetSummary(), '"SomeReason"')
        self.assertEqual(
            e2.GetChildMemberWithName("name").description, "ThrownException"
        )
        self.assertEqual(e2.GetChildMemberWithName("reason").description, "SomeReason")
        userInfo = e2.GetChildMemberWithName("userInfo").dynamic
        self.assertEqual(userInfo.summary, "1 key/value pair")
        self.assertEqual(
            userInfo.GetChildAtIndex(0).GetChildAtIndex(0).description, "some_key"
        )
        self.assertEqual(
            userInfo.GetChildAtIndex(0).GetChildAtIndex(1).description, "some_value"
        )
        reserved = e2.GetChildMemberWithName("reserved").dynamic
        self.assertGreater(reserved.num_children, 0)
        callStackReturnAddresses = [
            reserved.GetChildAtIndex(i).GetChildAtIndex(1)
            for i in range(0, reserved.GetNumChildren())
            if reserved.GetChildAtIndex(i).GetChildAtIndex(0).description
            == "callStackReturnAddresses"
        ][0].dynamic
        children = [
            callStackReturnAddresses.GetChildAtIndex(i)
            for i in range(0, callStackReturnAddresses.num_children)
        ]

        pcs = [i.unsigned for i in children]
        names = [
            target.ResolveSymbolContextForAddress(
                lldb.SBAddress(pc, target), lldb.eSymbolContextSymbol
            )
            .GetSymbol()
            .name
            for pc in pcs
        ]
        for n in ["objc_exception_throw", "foo(int)", "main"]:
            self.assertIn(
                n, names, "%s is in the exception backtrace (%s)" % (n, names)
            )

    @skipIf(compiler="clang", compiler_version=["<", "13.0"])
    def test_objc_exceptions_at_abort(self):
        self.build()

        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
        self.assertTrue(target, VALID_TARGET)

        self.runCmd("run 0")

        # We should be stopped at pthread_kill because of an unhandled exception
        self.expect("thread list", substrs=["stopped", "stop reason = signal SIGABRT"])

        self.expect(
            "thread exception",
            substrs=[
                "(NSException *) exception = ",
                '"SomeReason"',
                "libobjc.A.dylib`objc_exception_throw",
                "a.out`foo",
                "at main.mm:16",
                "a.out`rethrow",
                "at main.mm:27",
                "a.out`main",
            ],
        )

        process = self.dbg.GetSelectedTarget().process
        thread = process.GetSelectedThread()

        # There is an exception being currently processed at this point
        self.assertTrue(thread.GetCurrentException().IsValid())
        self.assertTrue(thread.GetCurrentExceptionBacktrace().IsValid())

        history_thread = thread.GetCurrentExceptionBacktrace()
        self.assertGreaterEqual(history_thread.num_frames, 4)
        for n in ["objc_exception_throw", "foo(int)", "rethrow(int)", "main"]:
            self.assertEqual(
                len([f for f in history_thread.frames if f.GetFunctionName() == n]), 1
            )

        self.runCmd("kill")

        self.runCmd("run 1")
        # We should be stopped at pthread_kill because of an unhandled exception
        self.expect("thread list", substrs=["stopped", "stop reason = signal SIGABRT"])

        self.expect(
            "thread exception",
            substrs=[
                "(MyCustomException *) exception = ",
                "libobjc.A.dylib`objc_exception_throw",
                "a.out`foo",
                "at main.mm:18",
                "a.out`rethrow",
                "at main.mm:27",
                "a.out`main",
            ],
        )

        process = self.dbg.GetSelectedTarget().process
        thread = process.GetSelectedThread()

        history_thread = thread.GetCurrentExceptionBacktrace()
        self.assertGreaterEqual(history_thread.num_frames, 4)
        for n in ["objc_exception_throw", "foo(int)", "rethrow(int)", "main"]:
            self.assertEqual(
                len([f for f in history_thread.frames if f.GetFunctionName() == n]), 1
            )

    @skipIf(compiler="clang", compiler_version=["<", "13.0"])
    def test_cxx_exceptions_at_abort(self):
        self.build()

        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
        self.assertTrue(target, VALID_TARGET)

        self.runCmd("run 2")

        # We should be stopped at pthread_kill because of an unhandled exception
        self.expect("thread list", substrs=["stopped", "stop reason = signal SIGABRT"])

        self.expect("thread exception", substrs=["exception ="])

        process = self.dbg.GetSelectedTarget().process
        thread = process.GetSelectedThread()

        self.assertTrue(thread.GetCurrentException().IsValid())

        # C++ exception backtraces are not exposed in the API (yet).
        self.assertFalse(thread.GetCurrentExceptionBacktrace().IsValid())