137cbd817SMichał Górnyimport gdbremote_testcase
237cbd817SMichał Górnyfrom lldbsuite.test.decorators import *
337cbd817SMichał Górnyfrom lldbsuite.test.lldbtest import *
437cbd817SMichał Górnyfrom lldbsuite.test import lldbutil
537cbd817SMichał Górny
637cbd817SMichał Górnyimport binascii
737cbd817SMichał Górnyimport os
837cbd817SMichał Górny
937cbd817SMichał Górnyclass TestGdbSaveCore(gdbremote_testcase.GdbRemoteTestCaseBase):
1037cbd817SMichał Górny
1137cbd817SMichał Górny    def coredump_test(self, core_path=None, expect_path=None):
1237cbd817SMichał Górny        self.build()
1337cbd817SMichał Górny        self.set_inferior_startup_attach()
1437cbd817SMichał Górny        procs = self.prep_debug_monitor_and_inferior()
1537cbd817SMichał Górny        self.add_qSupported_packets()
1637cbd817SMichał Górny        ret = self.expect_gdbremote_sequence()
17*02e690baSMichał Górny        if "qSaveCore+" not in ret["qSupported_response"]:
18*02e690baSMichał Górny            self.skipTest("qSaveCore not supported by lldb-server")
1937cbd817SMichał Górny        self.reset_test_sequence()
2037cbd817SMichał Górny
2137cbd817SMichał Górny        packet = "$qSaveCore"
2237cbd817SMichał Górny        if core_path is not None:
2337cbd817SMichał Górny            packet += ";path-hint:{}".format(
2437cbd817SMichał Górny                binascii.b2a_hex(core_path.encode()).decode())
2537cbd817SMichał Górny
2637cbd817SMichał Górny        self.test_sequence.add_log_lines([
2737cbd817SMichał Górny            "read packet: {}#00".format(packet),
2837cbd817SMichał Górny            {"direction": "send", "regex": "[$]core-path:([0-9a-f]+)#.*",
2937cbd817SMichał Górny             "capture": {1: "path"}},
3037cbd817SMichał Górny        ], True)
3137cbd817SMichał Górny        ret = self.expect_gdbremote_sequence()
3237cbd817SMichał Górny        out_path = binascii.a2b_hex(ret["path"].encode()).decode()
3337cbd817SMichał Górny        if expect_path is not None:
3437cbd817SMichał Górny            self.assertEqual(out_path, expect_path)
3537cbd817SMichał Górny
3637cbd817SMichał Górny        target = self.dbg.CreateTarget(None)
3737cbd817SMichał Górny        process = target.LoadCore(out_path)
3837cbd817SMichał Górny        self.assertTrue(process, PROCESS_IS_VALID)
3937cbd817SMichał Górny        self.assertEqual(process.GetProcessID(), procs["inferior"].pid)
4037cbd817SMichał Górny
41b07803eeSMichał Górny    @skipUnlessPlatform(oslist=["freebsd", "netbsd"])
4237cbd817SMichał Górny    def test_netbsd_path(self):
4337cbd817SMichał Górny        core = lldbutil.append_to_process_working_directory(self, "core")
4437cbd817SMichał Górny        self.coredump_test(core, core)
4537cbd817SMichał Górny
46b07803eeSMichał Górny    @skipUnlessPlatform(oslist=["freebsd", "netbsd"])
4737cbd817SMichał Górny    def test_netbsd_no_path(self):
4837cbd817SMichał Górny        self.coredump_test()
4937cbd817SMichał Górny
50b07803eeSMichał Górny    @skipUnlessPlatform(oslist=["freebsd", "netbsd"])
5137cbd817SMichał Górny    def test_netbsd_bad_path(self):
5237cbd817SMichał Górny        self.coredump_test("/dev/null/cantwritehere")
53