authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-10-13 12:13:41+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-10-13 12:13:41+02:00
logb1f3f59d660bbe163502daa6f0acd560782f49d8
tree67148819b3445ecfc9dbaf3d87229bfcea8abe39
parentba711f1115b76d85b13fc64636c4a4af8405cc1e

Fix fp-based backtracing on RISC-V

The fp points to the top of the frame instead of pointing to the old fp, compensate this difference with an offset.

1 files changed, 15 insertions(+), 6 deletions(-)

lib/std/debug.zig+15-6
......@@ -280,14 +280,23 @@ pub const StackIterator = struct {
280280 };
281281 }
282282
283 // On some architectures such as x86 the frame pointer is the address where
284 // the previous fp is stored, while on some other architectures such as
285 // RISC-V it points to the "top" of the frame, just above where the previous
286 // fp and the return address are stored.
287 const fp_adjust_factor = if (builtin.arch == .riscv32 or builtin.arch == .riscv64)
288 2 * @sizeOf(usize)
289 else
290 0;
291
283292 fn next(self: *StackIterator) ?usize {
284 if (self.fp == 0) return null;
285 self.fp = @intToPtr(*const usize, self.fp).*;
286 if (self.fp == 0) return null;
293 if (self.fp < fp_adjust_factor) return null;
294 self.fp = @intToPtr(*const usize, self.fp - fp_adjust_factor).*;
295 if (self.fp < fp_adjust_factor) return null;
287296
288297 if (self.first_addr) |addr| {
289 while (self.fp != 0) : (self.fp = @intToPtr(*const usize, self.fp).*) {
290 const return_address = @intToPtr(*const usize, self.fp + @sizeOf(usize)).*;
298 while (self.fp >= fp_adjust_factor) : (self.fp = @intToPtr(*const usize, self.fp - fp_adjust_factor).*) {
299 const return_address = @intToPtr(*const usize, self.fp - fp_adjust_factor + @sizeOf(usize)).*;
291300 if (addr == return_address) {
292301 self.first_addr = null;
293302 return return_address;
......@@ -295,7 +304,7 @@ pub const StackIterator = struct {
295304 }
296305 }
297306
298 const return_address = @intToPtr(*const usize, self.fp + @sizeOf(usize)).*;
307 const return_address = @intToPtr(*const usize, self.fp - fp_adjust_factor + @sizeOf(usize)).*;
299308 return return_address;
300309 }
301310};