-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rt] Add NativeStackWalker for applications to walk stack
- Loading branch information
Showing
12 changed files
with
144 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2019 Google Inc. All rights reserved. | ||
// See LICENSE for details of Apache 2.0 license. | ||
|
||
// Encapsulates logic relating to walking the stack to be exposed to (power user) applications. | ||
component NativeStackWalker { | ||
def iterateFrames(i: Pointer, s: Pointer, callback: (Pointer, Pointer, RiUserCode, int) -> bool) { | ||
var ip = i, sp = s; | ||
while (true) { | ||
// Try looking up a GC stackmap entry first. | ||
var stackmapEntry = RiTables.exactMatch(RiTables.searchTable(CiRuntime.GC_STACKMAP_PAGES, | ||
CiRuntime.GC_STACKMAP_TABLE, ip)); | ||
if (stackmapEntry != Pointer.NULL) { // found a (GC) stackmap entry. | ||
var stackMap = stackmapEntry.load<int>() >>> RiTables.PAGE_SHIFT; | ||
var frameSize = RiGc.stackmapSize(stackMap); | ||
callback(ip, sp, null, frameSize); | ||
var t = RiOs.callerFrame(ip, sp, frameSize / RiGc.REF_SIZE); | ||
ip = t.0; sp = t.1; | ||
continue; | ||
} | ||
|
||
// | ||
/* TODO: try using a source entry if no GC entry. | ||
var sourceEntry = RiTables.findSource(ip); | ||
if (sourceEntry != Pointer.NULL) { // found a source entry. | ||
var frameWords = printSourceEntry(ip, sourceEntry); | ||
var t = RiOs.callerFrame(ip, sp, frameWords); | ||
ip = t.0; sp = t.1; | ||
continue; | ||
} | ||
*/ | ||
|
||
// Search for user code. | ||
if (RiRuntime.userCodeList != null) { | ||
var userCode = RiRuntime.findUserCode(ip); | ||
if (userCode == null) return; // no user code found | ||
callback(ip, sp, userCode, 0); | ||
var t = userCode.nextFrame(ip, sp); | ||
ip = t.0; sp = t.1; | ||
continue; | ||
} | ||
return; // unknown frame; assume finished | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def main() -> int { | ||
foo(Array<byte>.new(4)); | ||
foo(Array<int>.new(8)); | ||
return 33; | ||
} | ||
def foo<T>(x: Array<T>) -> int { | ||
NativeStackWalker.iterateFrames(CiRuntime.callerIp() + -1, CiRuntime.callerSp(), printFrame); | ||
return x.length; | ||
} | ||
def printFrame(ip: Pointer, sp: Pointer, code: RiUserCode, size: int) -> bool { | ||
if (code == null) System.puts("--{virgil frame}--\n"); | ||
else System.puts("--{user frame}--\n"); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--{virgil frame}-- | ||
--{virgil frame}-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def main() -> int { | ||
foo(Array<byte>.new(4)); | ||
foo(Array<int>.new(8)); | ||
return 33; | ||
} | ||
def foo<T>(x: Array<T>) -> int { | ||
NativeStackWalker.iterateFrames(CiRuntime.callerIp() + -1, CiRuntime.callerSp(), printFrame); | ||
return x.length; | ||
} | ||
def printFrame(ip: Pointer, sp: Pointer, code: RiUserCode, size: int) -> bool { | ||
if (code == null) System.puts("--{virgil frame}--\n"); | ||
else System.puts("--{user frame}--\n"); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--{virgil frame}-- | ||
--{virgil frame}-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def main() -> int { | ||
foo(Array<byte>.new(4)); | ||
foo(Array<int>.new(8)); | ||
return 33; | ||
} | ||
def foo<T>(x: Array<T>) -> int { | ||
NativeStackWalker.iterateFrames(CiRuntime.callerIp() + -1, CiRuntime.callerSp(), printFrame); | ||
return x.length; | ||
} | ||
def printFrame(ip: Pointer, sp: Pointer, code: RiUserCode, size: int) -> bool { | ||
System.puts("--{user frame}--\n"); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def main() -> int { | ||
foo(Array<byte>.new(4)); | ||
foo(Array<int>.new(8)); | ||
return 33; | ||
} | ||
def foo<T>(x: Array<T>) -> int { | ||
NativeStackWalker.iterateFrames(CiRuntime.callerIp() + -1, CiRuntime.callerSp(), printFrame); | ||
return x.length; | ||
} | ||
def printFrame(ip: Pointer, sp: Pointer, code: RiUserCode, size: int) -> bool { | ||
if (code == null) System.puts("--{virgil frame}--\n"); | ||
else System.puts("--{user frame}--\n"); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--{virgil frame}-- | ||
--{virgil frame}-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def main() -> int { | ||
foo(Array<byte>.new(4)); | ||
foo(Array<int>.new(8)); | ||
return 33; | ||
} | ||
def foo<T>(x: Array<T>) -> int { | ||
NativeStackWalker.iterateFrames(CiRuntime.callerIp() + -1, CiRuntime.callerSp(), printFrame); | ||
return x.length; | ||
} | ||
def printFrame(ip: Pointer, sp: Pointer, code: RiUserCode, size: int) -> bool { | ||
if (code == null) System.puts("--{virgil frame}--\n"); | ||
else System.puts("--{user frame}--\n"); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--{virgil frame}-- | ||
--{virgil frame}-- |