|
| 1 | +""" |
| 2 | +Test lldb-dap stack trace when some of the source paths are missing |
| 3 | +""" |
| 4 | + |
| 5 | +from lldbsuite.test.decorators import skipIfWindows |
| 6 | +from lldbsuite.test.lldbtest import line_number |
| 7 | +import lldbdap_testcase |
| 8 | +from contextlib import contextmanager |
| 9 | +import os |
| 10 | + |
| 11 | + |
| 12 | +OTHER_C_SOURCE_CODE = """ |
| 13 | +int fibonacci(int n) { |
| 14 | + if (n < 0) return -1; |
| 15 | + if (n == 0) return 0; |
| 16 | + if (n == 1) return 1; |
| 17 | + int a = 0, b = 1, c; |
| 18 | + for (int i = 2; i <= n; ++i) { |
| 19 | + c = a + b; |
| 20 | + a = b; |
| 21 | + b = c; |
| 22 | + } |
| 23 | +
|
| 24 | + return b; // Break here |
| 25 | +} |
| 26 | +""" |
| 27 | + |
| 28 | + |
| 29 | +@contextmanager |
| 30 | +def delete_file_on_exit(path): |
| 31 | + try: |
| 32 | + yield path |
| 33 | + finally: |
| 34 | + if os.path.exists(path): |
| 35 | + os.remove(path) |
| 36 | + |
| 37 | + |
| 38 | +class TestDAP_stackTraceMissingSourcePath(lldbdap_testcase.DAPTestCaseBase): |
| 39 | + def build_and_run_until_breakpoint(self, stop_disassembly_display: str): |
| 40 | + """ |
| 41 | + Build the program and run until the breakpoint is hit, and return the stack frames. |
| 42 | + """ |
| 43 | + other_source_file = "other.c" |
| 44 | + with delete_file_on_exit(other_source_file): |
| 45 | + with open(other_source_file, "w") as f: |
| 46 | + f.write(OTHER_C_SOURCE_CODE) |
| 47 | + |
| 48 | + breakpoint_line = line_number(other_source_file, "// Break here") |
| 49 | + |
| 50 | + program = self.getBuildArtifact("a.out") |
| 51 | + init_commands = [ |
| 52 | + f"settings set stop-disassembly-display {stop_disassembly_display}" |
| 53 | + ] |
| 54 | + self.build_and_launch(program, initCommands=init_commands) |
| 55 | + |
| 56 | + breakpoint_ids = self.set_source_breakpoints( |
| 57 | + other_source_file, [breakpoint_line] |
| 58 | + ) |
| 59 | + self.assertEqual( |
| 60 | + len(breakpoint_ids), 1, "expect correct number of breakpoints" |
| 61 | + ) |
| 62 | + |
| 63 | + self.continue_to_breakpoints(breakpoint_ids) |
| 64 | + |
| 65 | + frames = self.get_stackFrames() |
| 66 | + self.assertLessEqual(2, len(frames), "expect at least 2 frames") |
| 67 | + |
| 68 | + self.assertIn( |
| 69 | + "path", |
| 70 | + frames[0]["source"], |
| 71 | + "Expect source path to always be in frame (other.c)", |
| 72 | + ) |
| 73 | + self.assertIn( |
| 74 | + "path", |
| 75 | + frames[1]["source"], |
| 76 | + "Expect source path in always be in frame (main.c)", |
| 77 | + ) |
| 78 | + |
| 79 | + return frames |
| 80 | + |
| 81 | + @skipIfWindows |
| 82 | + def test_stopDisassemblyDispay_noSource(self): |
| 83 | + """ |
| 84 | + Test that with with stop-disassembly-display = no-source - frames without source available give assembly code. |
| 85 | + """ |
| 86 | + frames = self.build_and_run_until_breakpoint("no-source") |
| 87 | + |
| 88 | + self.assertNotIn( |
| 89 | + "other.c", |
| 90 | + frames[0]["source"]["path"], |
| 91 | + "Expect original source path to not be in unavailable source frame (other.c)", |
| 92 | + ) |
| 93 | + self.assertIn( |
| 94 | + "sourceReference", |
| 95 | + frames[0]["source"], |
| 96 | + "Expect sourceReference source path in to be in unavailable source frame (other.c)", |
| 97 | + ) |
| 98 | + |
| 99 | + self.assertIn( |
| 100 | + "main.c", |
| 101 | + frames[1]["source"]["path"], |
| 102 | + "Expect original source path to be in source code frame (main.c)", |
| 103 | + ) |
| 104 | + self.assertNotIn( |
| 105 | + "sourceReference", |
| 106 | + frames[1]["source"], |
| 107 | + "Expect no sourceReference in source code frame (main.c)", |
| 108 | + ) |
| 109 | + |
| 110 | + @skipIfWindows |
| 111 | + def test_stopDisassemblyDispay_noDebuginfo(self): |
| 112 | + """ |
| 113 | + Test that with with stop-disassembly-display = no-debuginfo - all frames give source code even when source not available. |
| 114 | + """ |
| 115 | + frames = self.build_and_run_until_breakpoint("no-debuginfo") |
| 116 | + |
| 117 | + self.assertIn( |
| 118 | + "other.c", |
| 119 | + frames[0]["source"]["path"], |
| 120 | + "Expect original source path to be in unavailable source frame (other.c)", |
| 121 | + ) |
| 122 | + self.assertNotIn( |
| 123 | + "sourceReference", |
| 124 | + frames[0]["source"], |
| 125 | + "Expect sourceReference source path in to be in unavailable source frame (other.c)", |
| 126 | + ) |
| 127 | + |
| 128 | + self.assertIn( |
| 129 | + "main.c", |
| 130 | + frames[1]["source"]["path"], |
| 131 | + "Expect original source path to be in source code frame (main.c)", |
| 132 | + ) |
| 133 | + self.assertNotIn( |
| 134 | + "sourceReference", |
| 135 | + frames[1]["source"], |
| 136 | + "Expect no sourceReference in source code frame (main.c)", |
| 137 | + ) |
| 138 | + |
| 139 | + @skipIfWindows |
| 140 | + def test_stopDisassemblyDispay_never(self): |
| 141 | + """ |
| 142 | + Test that with with stop-disassembly-display = never - all frames don't give assembly code. |
| 143 | + """ |
| 144 | + frames = self.build_and_run_until_breakpoint("never") |
| 145 | + |
| 146 | + self.assertIn( |
| 147 | + "other.c", |
| 148 | + frames[0]["source"]["path"], |
| 149 | + "Expect original source path to be in unavailable source frame (other.c)", |
| 150 | + ) |
| 151 | + self.assertNotIn( |
| 152 | + "sourceReference", |
| 153 | + frames[0]["source"], |
| 154 | + "Expect sourceReference source path in to be in unavailable source frame (other.c)", |
| 155 | + ) |
| 156 | + |
| 157 | + self.assertIn( |
| 158 | + "main.c", |
| 159 | + frames[1]["source"]["path"], |
| 160 | + "Expect original source path to be in source code frame (main.c)", |
| 161 | + ) |
| 162 | + self.assertNotIn( |
| 163 | + "sourceReference", |
| 164 | + frames[1]["source"], |
| 165 | + "Expect no sourceReference in source code frame (main.c)", |
| 166 | + ) |
| 167 | + |
| 168 | + @skipIfWindows |
| 169 | + def test_stopDisassemblyDispay_always(self): |
| 170 | + """ |
| 171 | + Test that with with stop-disassembly-display = always - all frames give source code. |
| 172 | + """ |
| 173 | + frames = self.build_and_run_until_breakpoint("always") |
| 174 | + |
| 175 | + self.assertNotIn( |
| 176 | + "other.c", |
| 177 | + frames[0]["source"]["path"], |
| 178 | + "Expect original source path to not be in unavailable source frame (other.c)", |
| 179 | + ) |
| 180 | + self.assertIn( |
| 181 | + "sourceReference", |
| 182 | + frames[0]["source"], |
| 183 | + "Expect sourceReference source path in to be in unavailable source frame (other.c)", |
| 184 | + ) |
| 185 | + |
| 186 | + self.assertNotIn( |
| 187 | + "main.c", |
| 188 | + frames[1]["source"]["path"], |
| 189 | + "Expect original source path to not be in source code frame (main.c)", |
| 190 | + ) |
| 191 | + self.assertIn( |
| 192 | + "sourceReference", |
| 193 | + frames[1]["source"], |
| 194 | + "Expect sourceReference in source code frame (main.c)", |
| 195 | + ) |
0 commit comments