|
| 1 | +""" |
| 2 | +Test saving a mini dump, from yamilized examples. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import lldb |
| 7 | +from lldbsuite.test.decorators import * |
| 8 | +from lldbsuite.test.lldbtest import * |
| 9 | +from lldbsuite.test import lldbutil |
| 10 | + |
| 11 | + |
| 12 | +class AddressRange: |
| 13 | + begin: int |
| 14 | + end: int |
| 15 | + |
| 16 | + def __init__(self, begin, end): |
| 17 | + self.begin = begin |
| 18 | + self.end = end |
| 19 | + |
| 20 | + |
| 21 | +# We skip all these tests on Windows because on Windows Minidumps |
| 22 | +# are not generated by LLDB. |
| 23 | +class ProcessSaveCoreMinidumpTestCaseYaml(TestBase): |
| 24 | + def process_from_yaml(self, yaml_file): |
| 25 | + minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp") |
| 26 | + self.yaml2obj(yaml_file, minidump_path) |
| 27 | + self.target = self.dbg.CreateTarget(None) |
| 28 | + self.process = self.target.LoadCore(minidump_path) |
| 29 | + return self.process |
| 30 | + |
| 31 | + @skipIfWindows |
| 32 | + def validate_regions_saved_correctly( |
| 33 | + self, core_process, expected_region, expected_invalid_region=None |
| 34 | + ): |
| 35 | + """Validate that the expected_region is saved in the core_proc, and that the expected invalid region is not saved, if not not none.""" |
| 36 | + |
| 37 | + # Validate we can read the entire expected_region |
| 38 | + error = lldb.SBError() |
| 39 | + core_process.ReadMemory( |
| 40 | + expected_region.begin, expected_region.end - expected_region.begin, error |
| 41 | + ) |
| 42 | + self.assertTrue(error.Success(), error.GetCString()) |
| 43 | + |
| 44 | + # Validate we can't read before and after the expected_region |
| 45 | + core_process.ReadMemory(expected_region.begin - 1, 1, error) |
| 46 | + self.assertTrue(error.Fail(), error.GetCString()) |
| 47 | + |
| 48 | + core_process.ReadMemory(expected_region.end + 1, 1, error) |
| 49 | + self.assertTrue(error.Fail(), error.GetCString()) |
| 50 | + |
| 51 | + if expected_invalid_region is None: |
| 52 | + return |
| 53 | + |
| 54 | + # Validate we can't read the original_region |
| 55 | + core_process.ReadMemory( |
| 56 | + expected_invalid_region.begin, |
| 57 | + expected_invalid_region.end - expected_invalid_region.begin, |
| 58 | + error, |
| 59 | + ) |
| 60 | + self.assertTrue(error.Fail(), error.GetCString()) |
| 61 | + |
| 62 | + @skipIfWindows |
| 63 | + def test_saving_sub_memory_range(self): |
| 64 | + """ |
| 65 | + Validate we can save a Minidump for a subsection of a memory range. |
| 66 | + I.E. |
| 67 | + If our memory range is 0x2000-0x2020 and the user specifies 0x2000-0x2008 |
| 68 | + we should still capture 0x2000-0x2008 |
| 69 | + """ |
| 70 | + yaml = "minidump_mem64.yaml" |
| 71 | + proc = self.process_from_yaml(yaml) |
| 72 | + new_minidump_path = self.getBuildArtifact(__name__ + ".dmp") |
| 73 | + options = lldb.SBSaveCoreOptions() |
| 74 | + options.SetOutputFile(lldb.SBFileSpec(new_minidump_path)) |
| 75 | + options.SetPluginName("minidump") |
| 76 | + options.SetStyle(lldb.eSaveCoreCustomOnly) |
| 77 | + |
| 78 | + size = 8 |
| 79 | + begin = 0x2000 |
| 80 | + end = begin + size |
| 81 | + custom_range = lldb.SBMemoryRegionInfo("", begin, end, 3, True, False) |
| 82 | + options.AddMemoryRegionToSave(custom_range) |
| 83 | + |
| 84 | + error = proc.SaveCore(options) |
| 85 | + self.assertTrue(error.Success(), error.GetCString()) |
| 86 | + core_target = self.dbg.CreateTarget(None) |
| 87 | + core_process = core_target.LoadCore(new_minidump_path) |
| 88 | + |
| 89 | + expected_address_range = AddressRange(begin, end) |
| 90 | + expected_invalid_range = AddressRange(begin, 0x2020) |
| 91 | + self.validate_regions_saved_correctly( |
| 92 | + core_process, expected_address_range, expected_invalid_range |
| 93 | + ) |
| 94 | + |
| 95 | + @skipIfWindows |
| 96 | + def test_saving_super_memory_range(self): |
| 97 | + """ |
| 98 | + Validate we can save a Minidump for a subsection of a memory range. |
| 99 | + I.E. |
| 100 | + If our memory range is 0x1000-0x1100 and the user specifies 0x900-x1200 |
| 101 | + we should still capture 0x1000-0x1100 |
| 102 | + """ |
| 103 | + yaml = "minidump_mem64.yaml" |
| 104 | + proc = self.process_from_yaml(yaml) |
| 105 | + new_minidump_path = self.getBuildArtifact(__name__ + ".dmp") |
| 106 | + options = lldb.SBSaveCoreOptions() |
| 107 | + options.SetOutputFile(lldb.SBFileSpec(new_minidump_path)) |
| 108 | + options.SetPluginName("minidump") |
| 109 | + options.SetStyle(lldb.eSaveCoreCustomOnly) |
| 110 | + |
| 111 | + size = 0x100 |
| 112 | + begin = 0x1000 |
| 113 | + end = begin + size |
| 114 | + custom_range = lldb.SBMemoryRegionInfo("", begin - 16, end + 16, 3, True, False) |
| 115 | + options.AddMemoryRegionToSave(custom_range) |
| 116 | + |
| 117 | + error = proc.SaveCore(options) |
| 118 | + self.assertTrue(error.Success(), error.GetCString()) |
| 119 | + core_target = self.dbg.CreateTarget(None) |
| 120 | + core_process = core_target.LoadCore(new_minidump_path) |
| 121 | + |
| 122 | + expected_address_range = AddressRange(begin, end) |
| 123 | + expected_invalid_range = AddressRange(begin - 16, end + 16) |
| 124 | + self.validate_regions_saved_correctly( |
| 125 | + core_process, expected_address_range, expected_invalid_range |
| 126 | + ) |
| 127 | + |
| 128 | + @skipIfWindows |
| 129 | + def test_region_that_goes_out_of_bounds(self): |
| 130 | + """ |
| 131 | + Validate we can save a Minidump for a custom region |
| 132 | + that includes an end that enters an invalid (---) page. |
| 133 | + """ |
| 134 | + yaml = "minidump_mem64.yaml" |
| 135 | + proc = self.process_from_yaml(yaml) |
| 136 | + new_minidump_path = self.getBuildArtifact(__name__ + ".dmp") |
| 137 | + options = lldb.SBSaveCoreOptions() |
| 138 | + options.SetOutputFile(lldb.SBFileSpec(new_minidump_path)) |
| 139 | + options.SetPluginName("minidump") |
| 140 | + options.SetStyle(lldb.eSaveCoreCustomOnly) |
| 141 | + |
| 142 | + size = 0x120 |
| 143 | + begin = 0x1000 |
| 144 | + end = begin + size |
| 145 | + custom_range = lldb.SBMemoryRegionInfo("", begin, end, 3, True, False) |
| 146 | + options.AddMemoryRegionToSave(custom_range) |
| 147 | + |
| 148 | + error = proc.SaveCore(options) |
| 149 | + self.assertTrue(error.Success(), error.GetCString()) |
| 150 | + core_target = self.dbg.CreateTarget(None) |
| 151 | + core_process = core_target.LoadCore(new_minidump_path) |
| 152 | + |
| 153 | + expected_address_range = AddressRange(begin, end) |
| 154 | + expected_invalid_range = AddressRange(begin - 16, end + 16) |
| 155 | + self.validate_regions_saved_correctly( |
| 156 | + core_process, expected_address_range, expected_invalid_range |
| 157 | + ) |
| 158 | + |
| 159 | + @skipIfWindows |
| 160 | + def test_region_that_starts_out_of_bounds(self): |
| 161 | + """ |
| 162 | + Validate we can save a Minidump for a custom region |
| 163 | + that includes a start in a (---) page but ends in a valid page. |
| 164 | + """ |
| 165 | + yaml = "minidump_mem64.yaml" |
| 166 | + proc = self.process_from_yaml(yaml) |
| 167 | + new_minidump_path = self.getBuildArtifact(__name__ + ".dmp") |
| 168 | + options = lldb.SBSaveCoreOptions() |
| 169 | + options.SetOutputFile(lldb.SBFileSpec(new_minidump_path)) |
| 170 | + options.SetPluginName("minidump") |
| 171 | + options.SetStyle(lldb.eSaveCoreCustomOnly) |
| 172 | + |
| 173 | + size = 0x20 |
| 174 | + begin = 0x2000 |
| 175 | + end = begin + size |
| 176 | + custom_range = lldb.SBMemoryRegionInfo("", begin - 16, end, 3, True, False) |
| 177 | + options.AddMemoryRegionToSave(custom_range) |
| 178 | + |
| 179 | + error = proc.SaveCore(options) |
| 180 | + self.assertTrue(error.Success(), error.GetCString()) |
| 181 | + core_target = self.dbg.CreateTarget(None) |
| 182 | + core_process = core_target.LoadCore(new_minidump_path) |
| 183 | + |
| 184 | + expected_address_range = AddressRange(begin, end) |
| 185 | + expected_invalid_range = AddressRange(begin - 16, end) |
| 186 | + self.validate_regions_saved_correctly( |
| 187 | + core_process, expected_address_range, expected_invalid_range |
| 188 | + ) |
| 189 | + |
| 190 | + @skipIfWindows |
| 191 | + def test_region_spans_multiple_regions(self): |
| 192 | + """ |
| 193 | + Validate we can save a Minidump for a custom region |
| 194 | + that includes a start in a (---) page but ends in a valid page. |
| 195 | + """ |
| 196 | + yaml = "minidump_mem64.yaml" |
| 197 | + proc = self.process_from_yaml(yaml) |
| 198 | + new_minidump_path = self.getBuildArtifact(__name__ + ".dmp") |
| 199 | + options = lldb.SBSaveCoreOptions() |
| 200 | + options.SetOutputFile(lldb.SBFileSpec(new_minidump_path)) |
| 201 | + options.SetPluginName("minidump") |
| 202 | + options.SetStyle(lldb.eSaveCoreCustomOnly) |
| 203 | + |
| 204 | + size = 0x1000 |
| 205 | + begin = 0x5000 |
| 206 | + end = begin + size |
| 207 | + custom_range = lldb.SBMemoryRegionInfo("", begin, end, 3, True, False) |
| 208 | + options.AddMemoryRegionToSave(custom_range) |
| 209 | + |
| 210 | + error = proc.SaveCore(options) |
| 211 | + self.assertTrue(error.Success(), error.GetCString()) |
| 212 | + core_target = self.dbg.CreateTarget(None) |
| 213 | + core_process = core_target.LoadCore(new_minidump_path) |
| 214 | + |
| 215 | + expected_address_range = AddressRange(begin, end) |
| 216 | + self.validate_regions_saved_correctly(core_process, expected_address_range) |
| 217 | + |
| 218 | + @skipIfWindows |
| 219 | + def test_region_spans_multiple_regions_with_one_subrange(self): |
| 220 | + """ |
| 221 | + Validate we can save a Minidump for a custom region |
| 222 | + that includes a start in a (---) page but ends in a valid page. |
| 223 | + """ |
| 224 | + yaml = "minidump_mem64.yaml" |
| 225 | + proc = self.process_from_yaml(yaml) |
| 226 | + new_minidump_path = self.getBuildArtifact(__name__ + ".dmp") |
| 227 | + options = lldb.SBSaveCoreOptions() |
| 228 | + options.SetOutputFile(lldb.SBFileSpec(new_minidump_path)) |
| 229 | + options.SetPluginName("minidump") |
| 230 | + options.SetStyle(lldb.eSaveCoreCustomOnly) |
| 231 | + |
| 232 | + size = 0x800 |
| 233 | + begin = 0x5000 |
| 234 | + end = begin + size |
| 235 | + custom_range = lldb.SBMemoryRegionInfo("", begin, end, 3, True, False) |
| 236 | + options.AddMemoryRegionToSave(custom_range) |
| 237 | + |
| 238 | + error = proc.SaveCore(options) |
| 239 | + self.assertTrue(error.Success(), error.GetCString()) |
| 240 | + core_target = self.dbg.CreateTarget(None) |
| 241 | + core_process = core_target.LoadCore(new_minidump_path) |
| 242 | + |
| 243 | + expected_address_range = AddressRange(begin, end) |
| 244 | + expected_invalid_range = AddressRange(begin, begin + 0x1000) |
| 245 | + self.validate_regions_saved_correctly( |
| 246 | + core_process, expected_address_range, expected_invalid_range |
| 247 | + ) |
0 commit comments