1"""
2Test basics of Minidump debugging.
3"""
4
5from six import iteritems
6
7import shutil
8
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class MiniDumpNewTestCase(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    NO_DEBUG_INFO_TESTCASE = True
20
21    _linux_x86_64_pid = 29917
22    _linux_x86_64_not_crashed_pid = 29939
23    _linux_x86_64_not_crashed_pid_offset = 0xD967
24
25    def process_from_yaml(self, yaml_file):
26        minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")
27        self.yaml2obj(yaml_file, minidump_path)
28        self.target = self.dbg.CreateTarget(None)
29        self.process = self.target.LoadCore(minidump_path)
30        return self.process
31
32    @skipIfReproducer # lldb::FileSP used in typemap cannot be instrumented.
33    def check_state(self):
34        with open(os.devnull) as devnul:
35            # sanitize test output
36            self.dbg.SetOutputFileHandle(devnul, False)
37            self.dbg.SetErrorFileHandle(devnul, False)
38
39            self.assertTrue(self.process.is_stopped)
40
41            # Process.Continue
42            error = self.process.Continue()
43            self.assertFalse(error.Success())
44            self.assertTrue(self.process.is_stopped)
45
46            # Thread.StepOut
47            thread = self.process.GetSelectedThread()
48            thread.StepOut()
49            self.assertTrue(self.process.is_stopped)
50
51            # command line
52            self.dbg.HandleCommand('s')
53            self.assertTrue(self.process.is_stopped)
54            self.dbg.HandleCommand('c')
55            self.assertTrue(self.process.is_stopped)
56
57            # restore file handles
58            self.dbg.SetOutputFileHandle(None, False)
59            self.dbg.SetErrorFileHandle(None, False)
60
61    def test_loadcore_error_status(self):
62        """Test the SBTarget.LoadCore(core, error) overload."""
63        minidump_path = self.getBuildArtifact("linux-x86_64.dmp")
64        self.yaml2obj("linux-x86_64.yaml", minidump_path)
65        self.target = self.dbg.CreateTarget(None)
66        error = lldb.SBError()
67        self.process = self.target.LoadCore(minidump_path, error)
68        self.assertTrue(self.process, PROCESS_IS_VALID)
69        self.assertTrue(error.Success())
70
71    def test_loadcore_error_status_failure(self):
72        """Test the SBTarget.LoadCore(core, error) overload."""
73        self.target = self.dbg.CreateTarget(None)
74        error = lldb.SBError()
75        self.process = self.target.LoadCore("non-existent.dmp", error)
76        self.assertFalse(self.process, PROCESS_IS_VALID)
77        self.assertTrue(error.Fail())
78
79    def test_process_info_in_minidump(self):
80        """Test that lldb can read the process information from the Minidump."""
81        self.process_from_yaml("linux-x86_64.yaml")
82        self.assertTrue(self.process, PROCESS_IS_VALID)
83        self.assertEqual(self.process.GetNumThreads(), 1)
84        self.assertEqual(self.process.GetProcessID(), self._linux_x86_64_pid)
85        self.check_state()
86
87    def test_memory_region_name(self):
88        self.process_from_yaml("regions-linux-map.yaml")
89        result = lldb.SBCommandReturnObject()
90        addr_region_name_pairs = [
91            ("0x400d9000", "/system/bin/app_process"),
92            ("0x400db000", "/system/bin/app_process"),
93            ("0x400dd000", "/system/bin/linker"),
94            ("0x400ed000", "/system/bin/linker"),
95            ("0x400ee000", "/system/bin/linker"),
96            ("0x400fb000", "/system/lib/liblog.so"),
97            ("0x400fc000", "/system/lib/liblog.so"),
98            ("0x400fd000", "/system/lib/liblog.so"),
99            ("0x400ff000", "/system/lib/liblog.so"),
100            ("0x40100000", "/system/lib/liblog.so"),
101            ("0x40101000", "/system/lib/libc.so"),
102            ("0x40122000", "/system/lib/libc.so"),
103            ("0x40123000", "/system/lib/libc.so"),
104            ("0x40167000", "/system/lib/libc.so"),
105            ("0x40169000", "/system/lib/libc.so"),
106        ]
107        ci = self.dbg.GetCommandInterpreter()
108        for (addr, region_name) in addr_region_name_pairs:
109            command = 'memory region ' + addr
110            ci.HandleCommand(command, result, False)
111            message = 'Ensure memory "%s" shows up in output for "%s"' % (
112                region_name, command)
113            self.assertIn(region_name, result.GetOutput(), message)
114
115    def test_thread_info_in_minidump(self):
116        """Test that lldb can read the thread information from the Minidump."""
117        self.process_from_yaml("linux-x86_64.yaml")
118        self.check_state()
119        # This process crashed due to a segmentation fault in its
120        # one and only thread.
121        self.assertEqual(self.process.GetNumThreads(), 1)
122        thread = self.process.GetThreadAtIndex(0)
123        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonSignal)
124        stop_description = thread.GetStopDescription(256)
125        self.assertIn("SIGSEGV", stop_description)
126
127    def test_stack_info_in_minidump(self):
128        """Test that we can see a trivial stack in a breakpad-generated Minidump."""
129        # target create linux-x86_64 -c linux-x86_64.dmp
130        self.dbg.CreateTarget("linux-x86_64")
131        self.target = self.dbg.GetSelectedTarget()
132        self.process = self.target.LoadCore("linux-x86_64.dmp")
133        self.check_state()
134        self.assertEqual(self.process.GetNumThreads(), 1)
135        self.assertEqual(self.process.GetProcessID(), self._linux_x86_64_pid)
136        thread = self.process.GetThreadAtIndex(0)
137        # frame #0: linux-x86_64`crash()
138        # frame #1: linux-x86_64`_start
139        self.assertEqual(thread.GetNumFrames(), 2)
140        frame = thread.GetFrameAtIndex(0)
141        self.assertTrue(frame.IsValid())
142        self.assertTrue(frame.GetModule().IsValid())
143        pc = frame.GetPC()
144        eip = frame.FindRegister("pc")
145        self.assertTrue(eip.IsValid())
146        self.assertEqual(pc, eip.GetValueAsUnsigned())
147
148    def test_snapshot_minidump_dump_requested(self):
149        """Test that if we load a snapshot minidump file (meaning the process
150        did not crash) with exception code "DUMP_REQUESTED" there is no stop reason."""
151        # target create -c linux-x86_64_not_crashed.dmp
152        self.dbg.CreateTarget(None)
153        self.target = self.dbg.GetSelectedTarget()
154        self.process = self.target.LoadCore("linux-x86_64_not_crashed.dmp")
155        self.check_state()
156        self.assertEqual(self.process.GetNumThreads(), 1)
157        thread = self.process.GetThreadAtIndex(0)
158        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone)
159        stop_description = thread.GetStopDescription(256)
160        self.assertEqual(stop_description, "")
161
162    def test_snapshot_minidump_null_exn_code(self):
163        """Test that if we load a snapshot minidump file (meaning the process
164        did not crash) with exception code zero there is no stop reason."""
165        self.process_from_yaml("linux-x86_64_null_signal.yaml")
166        self.check_state()
167        self.assertEqual(self.process.GetNumThreads(), 1)
168        thread = self.process.GetThreadAtIndex(0)
169        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone)
170        stop_description = thread.GetStopDescription(256)
171        self.assertEqual(stop_description, "")
172
173    def check_register_unsigned(self, set, name, expected):
174        reg_value = set.GetChildMemberWithName(name)
175        self.assertTrue(reg_value.IsValid(),
176                        'Verify we have a register named "%s"' % (name))
177        self.assertEqual(reg_value.GetValueAsUnsigned(), expected,
178                         'Verify "%s" == %i' % (name, expected))
179
180    def check_register_string_value(self, set, name, expected, format):
181        reg_value = set.GetChildMemberWithName(name)
182        self.assertTrue(reg_value.IsValid(),
183                        'Verify we have a register named "%s"' % (name))
184        if format is not None:
185            reg_value.SetFormat(format)
186        self.assertEqual(reg_value.GetValue(), expected,
187                         'Verify "%s" has string value "%s"' % (name,
188                                                                expected))
189
190    def test_arm64_registers(self):
191        """Test ARM64 registers from a breakpad created minidump."""
192        self.process_from_yaml("arm64-macos.yaml")
193        self.check_state()
194        self.assertEqual(self.process.GetNumThreads(), 1)
195        thread = self.process.GetThreadAtIndex(0)
196        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone)
197        stop_description = thread.GetStopDescription(256)
198        self.assertEqual(stop_description, "")
199        registers = thread.GetFrameAtIndex(0).GetRegisters()
200        # Verify the GPR registers are all correct
201        # Verify x0 - x31 register values
202        gpr = registers.GetValueAtIndex(0)
203        for i in range(32):
204            v = i+1 | i+2 << 32 | i+3 << 48
205            w = i+1
206            self.check_register_unsigned(gpr, 'x%i' % (i), v)
207            self.check_register_unsigned(gpr, 'w%i' % (i), w)
208        # Verify arg1 - arg8 register values
209        for i in range(1, 9):
210            v = i | i+1 << 32 | i+2 << 48
211            self.check_register_unsigned(gpr, 'arg%i' % (i), v)
212        i = 29
213        v = i+1 | i+2 << 32 | i+3 << 48
214        self.check_register_unsigned(gpr, 'fp', v)
215        i = 30
216        v = i+1 | i+2 << 32 | i+3 << 48
217        self.check_register_unsigned(gpr, 'lr', v)
218        i = 31
219        v = i+1 | i+2 << 32 | i+3 << 48
220        self.check_register_unsigned(gpr, 'sp', v)
221        self.check_register_unsigned(gpr, 'pc', 0x1000)
222        self.check_register_unsigned(gpr, 'cpsr', 0x11223344)
223        self.check_register_unsigned(gpr, 'psr', 0x11223344)
224
225        # Verify the FPR registers are all correct
226        fpr = registers.GetValueAtIndex(1)
227        for i in range(32):
228            v = "0x"
229            d = "0x"
230            s = "0x"
231            h = "0x"
232            for j in range(i+15, i-1, -1):
233                v += "%2.2x" % (j)
234            for j in range(i+7, i-1, -1):
235                d += "%2.2x" % (j)
236            for j in range(i+3, i-1, -1):
237                s += "%2.2x" % (j)
238            for j in range(i+1, i-1, -1):
239                h += "%2.2x" % (j)
240            self.check_register_string_value(fpr, "v%i" % (i), v,
241                                             lldb.eFormatHex)
242            self.check_register_string_value(fpr, "d%i" % (i), d,
243                                             lldb.eFormatHex)
244            self.check_register_string_value(fpr, "s%i" % (i), s,
245                                             lldb.eFormatHex)
246            self.check_register_string_value(fpr, "h%i" % (i), h,
247                                             lldb.eFormatHex)
248        self.check_register_unsigned(gpr, 'fpsr', 0x55667788)
249        self.check_register_unsigned(gpr, 'fpcr', 0x99aabbcc)
250
251    def verify_arm_registers(self, apple=False):
252        """
253            Verify values of all ARM registers from a breakpad created
254            minidump.
255        """
256        if apple:
257            self.process_from_yaml("arm-macos.yaml")
258        else:
259            self.process_from_yaml("arm-linux.yaml")
260        self.check_state()
261        self.assertEqual(self.process.GetNumThreads(), 1)
262        thread = self.process.GetThreadAtIndex(0)
263        self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone)
264        stop_description = thread.GetStopDescription(256)
265        self.assertEqual(stop_description, "")
266        registers = thread.GetFrameAtIndex(0).GetRegisters()
267        # Verify the GPR registers are all correct
268        # Verify x0 - x31 register values
269        gpr = registers.GetValueAtIndex(0)
270        for i in range(1, 16):
271            self.check_register_unsigned(gpr, 'r%i' % (i), i+1)
272        # Verify arg1 - arg4 register values
273        for i in range(1, 5):
274            self.check_register_unsigned(gpr, 'arg%i' % (i), i)
275        if apple:
276            self.check_register_unsigned(gpr, 'fp', 0x08)
277        else:
278            self.check_register_unsigned(gpr, 'fp', 0x0c)
279        self.check_register_unsigned(gpr, 'lr', 0x0f)
280        self.check_register_unsigned(gpr, 'sp', 0x0e)
281        self.check_register_unsigned(gpr, 'pc', 0x10)
282        self.check_register_unsigned(gpr, 'cpsr', 0x11223344)
283
284        # Verify the FPR registers are all correct
285        fpr = registers.GetValueAtIndex(1)
286        # Check d0 - d31
287        self.check_register_unsigned(gpr, 'fpscr', 0x55667788aabbccdd)
288        for i in range(32):
289            value = (i+1) | (i+1) << 8 | (i+1) << 32 | (i+1) << 48
290            self.check_register_unsigned(fpr, "d%i" % (i), value)
291        # Check s0 - s31
292        for i in range(32):
293            i_val = (i >> 1) + 1
294            if i & 1:
295                value = "%#8.8x" % (i_val | i_val << 16)
296            else:
297                value = "%#8.8x" % (i_val | i_val << 8)
298            self.check_register_string_value(fpr, "s%i" % (i), value,
299                                             lldb.eFormatHex)
300        # Check q0 - q15
301        for i in range(15):
302            a = i * 2 + 1
303            b = a + 1
304            value = ("0x00%2.2x00%2.2x0000%2.2x%2.2x"
305                     "00%2.2x00%2.2x0000%2.2x%2.2x") % (b, b, b, b, a, a, a, a)
306            self.check_register_string_value(fpr, "q%i" % (i), value,
307                                             lldb.eFormatHex)
308
309    def test_linux_arm_registers(self):
310        """Test Linux ARM registers from a breakpad created minidump.
311
312           The frame pointer is R11 for linux.
313        """
314        self.verify_arm_registers(apple=False)
315
316    def test_apple_arm_registers(self):
317        """Test Apple ARM registers from a breakpad created minidump.
318
319           The frame pointer is R7 for linux.
320        """
321        self.verify_arm_registers(apple=True)
322
323    def do_test_deeper_stack(self, binary, core, pid):
324        target = self.dbg.CreateTarget(binary)
325        process = target.LoadCore(core)
326        thread = process.GetThreadAtIndex(0)
327
328        self.assertEqual(process.GetProcessID(), pid)
329
330        expected_stack = {1: 'bar', 2: 'foo', 3: '_start'}
331        self.assertGreaterEqual(thread.GetNumFrames(), len(expected_stack))
332        for index, name in iteritems(expected_stack):
333            frame = thread.GetFrameAtIndex(index)
334            self.assertTrue(frame.IsValid())
335            function_name = frame.GetFunctionName()
336            self.assertIn(name, function_name)
337
338    @skipIfLLVMTargetMissing("X86")
339    def test_deeper_stack_in_minidump(self):
340        """Test that we can examine a more interesting stack in a Minidump."""
341        # Launch with the Minidump, and inspect the stack.
342        # target create linux-x86_64_not_crashed -c linux-x86_64_not_crashed.dmp
343        self.do_test_deeper_stack("linux-x86_64_not_crashed",
344                                  "linux-x86_64_not_crashed.dmp",
345                                  self._linux_x86_64_not_crashed_pid)
346
347    @skipIfReproducer # VFS is a snapshot.
348    def do_change_pid_in_minidump(self, core, newcore, offset, oldpid, newpid):
349        """ This assumes that the minidump is breakpad generated on Linux -
350        meaning that the PID in the file will be an ascii string part of
351        /proc/PID/status which is written in the file
352        """
353        shutil.copyfile(core, newcore)
354        with open(newcore, "rb+") as f:
355            f.seek(offset)
356            currentpid = f.read(5).decode('utf-8')
357            self.assertEqual(currentpid, oldpid)
358
359            f.seek(offset)
360            if len(newpid) < len(oldpid):
361                newpid += " " * (len(oldpid) - len(newpid))
362            newpid += "\n"
363            f.write(newpid.encode('utf-8'))
364
365    def test_deeper_stack_in_minidump_with_same_pid_running(self):
366        """Test that we read the information from the core correctly even if we
367        have a running process with the same PID"""
368        new_core = self.getBuildArtifact("linux-x86_64_not_crashed-pid.dmp")
369        self.do_change_pid_in_minidump("linux-x86_64_not_crashed.dmp",
370                                       new_core,
371                                       self._linux_x86_64_not_crashed_pid_offset,
372                                       str(self._linux_x86_64_not_crashed_pid),
373                                       str(os.getpid()))
374        self.do_test_deeper_stack("linux-x86_64_not_crashed", new_core, os.getpid())
375
376    def test_two_cores_same_pid(self):
377        """Test that we handle the situation if we have two core files with the same PID """
378        new_core = self.getBuildArtifact("linux-x86_64_not_crashed-pid.dmp")
379        self.do_change_pid_in_minidump("linux-x86_64_not_crashed.dmp",
380                                       new_core,
381                                       self._linux_x86_64_not_crashed_pid_offset,
382                                       str(self._linux_x86_64_not_crashed_pid),
383                                       str(self._linux_x86_64_pid))
384        self.do_test_deeper_stack("linux-x86_64_not_crashed",
385                                  new_core, self._linux_x86_64_pid)
386        self.test_stack_info_in_minidump()
387
388    def test_local_variables_in_minidump(self):
389        """Test that we can examine local variables in a Minidump."""
390        # Launch with the Minidump, and inspect a local variable.
391        # target create linux-x86_64_not_crashed -c linux-x86_64_not_crashed.dmp
392        self.target = self.dbg.CreateTarget("linux-x86_64_not_crashed")
393        self.process = self.target.LoadCore("linux-x86_64_not_crashed.dmp")
394        self.check_state()
395        thread = self.process.GetThreadAtIndex(0)
396        frame = thread.GetFrameAtIndex(1)
397        value = frame.EvaluateExpression('x')
398        self.assertEqual(value.GetValueAsSigned(), 3)
399
400    def test_memory_regions_in_minidump(self):
401        """Test memory regions from a Minidump"""
402        self.process_from_yaml("regions-linux-map.yaml")
403        self.check_state()
404
405        regions_count = 19
406        region_info_list = self.process.GetMemoryRegions()
407        self.assertEqual(region_info_list.GetSize(), regions_count)
408
409        def check_region(index, start, end, read, write, execute, mapped, name):
410            region_info = lldb.SBMemoryRegionInfo()
411            self.assertTrue(
412                self.process.GetMemoryRegionInfo(start, region_info).Success())
413            self.assertEqual(start, region_info.GetRegionBase())
414            self.assertEqual(end, region_info.GetRegionEnd())
415            self.assertEqual(read, region_info.IsReadable())
416            self.assertEqual(write, region_info.IsWritable())
417            self.assertEqual(execute, region_info.IsExecutable())
418            self.assertEqual(mapped, region_info.IsMapped())
419            self.assertEqual(name, region_info.GetName())
420
421            # Ensure we have the same regions as SBMemoryRegionInfoList contains.
422            if index >= 0 and index < regions_count:
423                region_info_from_list = lldb.SBMemoryRegionInfo()
424                self.assertTrue(region_info_list.GetMemoryRegionAtIndex(
425                    index, region_info_from_list))
426                self.assertEqual(region_info_from_list, region_info)
427
428        a = "/system/bin/app_process"
429        b = "/system/bin/linker"
430        c = "/system/lib/liblog.so"
431        d = "/system/lib/libc.so"
432        n = None
433        max_int = 0xffffffffffffffff
434
435        # Test address before the first entry comes back with nothing mapped up
436        # to first valid region info
437        check_region(-1, 0x00000000, 0x400d9000, False, False, False, False, n)
438        check_region( 0, 0x400d9000, 0x400db000, True,  False, True,  True,  a)
439        check_region( 1, 0x400db000, 0x400dc000, True,  False, False, True,  a)
440        check_region( 2, 0x400dc000, 0x400dd000, True,  True,  False, True,  n)
441        check_region( 3, 0x400dd000, 0x400ec000, True,  False, True,  True,  b)
442        check_region( 4, 0x400ec000, 0x400ed000, True,  False, False, True,  n)
443        check_region( 5, 0x400ed000, 0x400ee000, True,  False, False, True,  b)
444        check_region( 6, 0x400ee000, 0x400ef000, True,  True,  False, True,  b)
445        check_region( 7, 0x400ef000, 0x400fb000, True,  True,  False, True,  n)
446        check_region( 8, 0x400fb000, 0x400fc000, True,  False, True,  True,  c)
447        check_region( 9, 0x400fc000, 0x400fd000, True,  True,  True,  True,  c)
448        check_region(10, 0x400fd000, 0x400ff000, True,  False, True,  True,  c)
449        check_region(11, 0x400ff000, 0x40100000, True,  False, False, True,  c)
450        check_region(12, 0x40100000, 0x40101000, True,  True,  False, True,  c)
451        check_region(13, 0x40101000, 0x40122000, True,  False, True,  True,  d)
452        check_region(14, 0x40122000, 0x40123000, True,  True,  True,  True,  d)
453        check_region(15, 0x40123000, 0x40167000, True,  False, True,  True,  d)
454        check_region(16, 0x40167000, 0x40169000, True,  False, False, True,  d)
455        check_region(17, 0x40169000, 0x4016b000, True,  True,  False, True,  d)
456        check_region(18, 0x4016b000, 0x40176000, True,  True,  False, True,  n)
457        check_region(-1, 0x40176000, max_int,    False, False, False, False, n)
458
459    @skipIfLLVMTargetMissing("X86")
460    def test_minidump_sysroot(self):
461        """Test that lldb can find a module referenced in an i386 linux minidump using the sysroot."""
462
463        # Copy linux-x86_64 executable to tmp_sysroot/temp/test/ (since it was compiled as
464        # /tmp/test/linux-x86_64)
465        tmp_sysroot = os.path.join(
466            self.getBuildDir(), "lldb_i386_mock_sysroot")
467        executable = os.path.join(
468            tmp_sysroot, "tmp", "test", "linux-x86_64")
469        exe_dir = os.path.dirname(executable)
470        lldbutil.mkdir_p(exe_dir)
471        shutil.copyfile("linux-x86_64", executable)
472
473        # Set sysroot and load core
474        self.runCmd("platform select remote-linux --sysroot '%s'" %
475                    tmp_sysroot)
476        self.process_from_yaml("linux-x86_64.yaml")
477        self.check_state()
478
479        # Check that we loaded the module from the sysroot
480        self.assertEqual(self.target.GetNumModules(), 1)
481        module = self.target.GetModuleAtIndex(0)
482        spec_dir_norm = os.path.normcase(module.GetFileSpec().GetDirectory())
483        exe_dir_norm = os.path.normcase(exe_dir)
484        self.assertEqual(spec_dir_norm, exe_dir_norm)
485