xref: /llvm-project/lldb/test/API/python_api/sbsavecoreoptions/TestSBSaveCoreOptions.py (revision 06edefac10f4481bdd458c0362d9a305f6a1ce6a)
1"""Test the SBSaveCoreOptions APIs."""
2
3import lldb
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6
7
8class SBSaveCoreOptionsAPICase(TestBase):
9    basic_minidump = "basic_minidump.yaml"
10    basic_minidump_different_pid = "basic_minidump_different_pid.yaml"
11
12    def get_process_from_yaml(self, yaml_file):
13        minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")
14        print("minidump_path: " + minidump_path)
15        self.yaml2obj(yaml_file, minidump_path)
16        self.assertTrue(
17            os.path.exists(minidump_path), "yaml2obj did not emit a minidump file"
18        )
19        target = self.dbg.CreateTarget(None)
20        process = target.LoadCore(minidump_path)
21        self.assertTrue(process.IsValid(), "Process is not valid")
22        return process
23
24    def get_basic_process(self):
25        return self.get_process_from_yaml(self.basic_minidump)
26
27    def get_basic_process_different_pid(self):
28        return self.get_process_from_yaml(self.basic_minidump_different_pid)
29
30    def test_plugin_name_assignment(self):
31        """Test assignment ensuring valid plugin names only."""
32        options = lldb.SBSaveCoreOptions()
33        error = options.SetPluginName(None)
34        self.assertTrue(error.Success())
35        self.assertEqual(options.GetPluginName(), None)
36        error = options.SetPluginName("Not a real plugin")
37        self.assertTrue(error.Fail())
38        self.assertEqual(options.GetPluginName(), None)
39        error = options.SetPluginName("minidump")
40        self.assertTrue(error.Success())
41        self.assertEqual(options.GetPluginName(), "minidump")
42        error = options.SetPluginName("")
43        self.assertTrue(error.Success())
44        self.assertEqual(options.GetPluginName(), None)
45
46    def test_default_corestyle_behavior(self):
47        """Test that the default core style is unspecified."""
48        options = lldb.SBSaveCoreOptions()
49        self.assertEqual(options.GetStyle(), lldb.eSaveCoreUnspecified)
50
51    def test_adding_and_removing_thread(self):
52        """Test adding and removing a thread from save core options."""
53        self.assertTrue(self.dbg)
54        options = lldb.SBSaveCoreOptions()
55        process = self.get_basic_process()
56        self.assertTrue(process.IsValid(), "Process is not valid")
57        thread = process.GetThreadAtIndex(0)
58        error = options.AddThread(thread)
59        self.assertTrue(error.Success(), error.GetCString())
60        removed_success = options.RemoveThread(thread)
61        self.assertTrue(removed_success)
62        removed_success = options.RemoveThread(thread)
63        self.assertFalse(removed_success)
64
65    def test_adding_thread_different_process(self):
66        """Test adding and removing a thread from save core options."""
67        options = lldb.SBSaveCoreOptions()
68        process = self.get_basic_process()
69        process_2 = self.get_basic_process_different_pid()
70        thread = process.GetThreadAtIndex(0)
71        error = options.AddThread(thread)
72        self.assertTrue(error.Success())
73        thread_2 = process_2.GetThreadAtIndex(0)
74        error = options.AddThread(thread_2)
75        self.assertTrue(error.Fail())
76        options.Clear()
77        error = options.AddThread(thread_2)
78        self.assertTrue(error.Success())
79        options.SetProcess(process)
80        error = options.AddThread(thread_2)
81        self.assertTrue(error.Fail())
82        error = options.AddThread(thread)
83        self.assertTrue(error.Success())
84
85    def test_removing_and_adding_insertion_order(self):
86        """Test insertion order is maintained when removing and adding threads."""
87        options = lldb.SBSaveCoreOptions()
88        process = self.get_basic_process()
89        threads = []
90        for x in range(0, 3):
91            thread = process.GetThreadAtIndex(x)
92            threads.append(thread)
93            error = options.AddThread(thread)
94            self.assertTrue(error.Success())
95
96        # Get the middle thread, remove it, and insert it back.
97        middle_thread = threads[1]
98        self.assertTrue(options.RemoveThread(middle_thread))
99        thread_collection = options.GetThreadsToSave()
100        self.assertTrue(thread_collection is not None)
101        self.assertEqual(thread_collection.GetSize(), 2)
102        error = options.AddThread(middle_thread)
103        self.assertTrue(error.Success())
104        thread_collection = options.GetThreadsToSave()
105        self.assertEqual(thread_collection.GetSize(), 3)
106        self.assertIn(middle_thread, thread_collection)
107