Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Stack_traces
note: Programming environment operations

View file

@ -0,0 +1,15 @@
Many programming languages allow for introspection of the current call stack environment. This can be for a variety of purposes such as enforcing security checks, debugging, or for getting access to the stack frame of callers.
;Task:
Print out (in a manner considered suitable for the platform) the current call stack.
The amount of information printed for each frame on the call stack is not constrained, but should include at least the name of the function or method at that level of the stack frame.
You may explicitly add a call to produce the stack trace to the (example) code being instrumented for examination.
The task should allow the program to continue after generating the stack trace.
The task report here must include the trace from a sample program.
<br><br>

View file

@ -0,0 +1,31 @@
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Traceback;
with GNAT.Traceback.Symbolic;
procedure Test_Stack_Trace is
procedure Call_Stack is
Trace : GNAT.Traceback.Tracebacks_Array (1..1_000);
Length : Natural;
begin
GNAT.Traceback.Call_Chain (Trace, Length);
Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (Trace (1..Length)));
end Call_Stack;
procedure Inner (K : Integer) is
begin
Call_Stack;
end Inner;
procedure Middle (X, Y : Integer) is
begin
Inner (X * Y);
end Middle;
procedure Outer (A, B, C : Integer) is
begin
Middle (A + B, B + C);
end Outer;
begin
Outer (2,3,5);
end Test_Stack_Trace;

View file

@ -0,0 +1,17 @@
f()
return
f()
{
return g()
}
g()
{
ListLines
msgbox, lines recently executed
x = local to g
ListVars
msgbox, variable bindings
}
#Persistent

View file

@ -0,0 +1,14 @@
001: f()
006: Return,g()
011: ListLines (0.05)
012: MsgBox,lines recently executed (3.81)
013: x = local to g
014: ListVars
015: MsgBox,variable bindings (3.94)
016: }
002: Return (181.66)
Global Variables (alphabetical)
--------------------------------------------------
0[1 of 3]: 0
ErrorLevel[1 of 3]: 0

View file

@ -0,0 +1,25 @@
using System;
using System.Diagnostics;
class Program
{
static void Inner()
{
Console.WriteLine(new StackTrace());
}
static void Middle()
{
Inner();
}
static void Outer()
{
Middle();
}
static void Main()
{
Outer();
}
}

View file

@ -0,0 +1,4 @@
at Program.Inner()
at Program.Middle()
at Program.Outer()
at Program.Main()

View file

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <execinfo.h>
#define MAX_BT 200
void print_stack_trace()
{
void *buffer[MAX_BT];
int n;
n = backtrace(buffer, MAX_BT);
fprintf(stderr, "--- (depth %d) ---\n", n);
backtrace_symbols_fd(buffer, n, STDERR_FILENO);
}
void inner(int k)
{
print_stack_trace();
}
void middle(int x, int y)
{
inner(x*y);
}
void outer(int a, int b, int c)
{
middle(a+b, b+c);
}
int main()
{
outer(2,3,5);
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,79 @@
/* stack_trace.c - macros for hinting/tracing where a program crashed
on a system _without_ any form of debugger.
Simple goodbye_cruel_world.c example:
#include <stdio.h>
#include <stdlib.h>
#define STACK_TRACE_ON // compile in these "stack_trace" routines
#include "stack_trace.h"
void goodbye_cruel_world()
BEGIN(goodbye_cruel_world)
print_stack_trace();
for(;;){}
END
int main()
BEGIN(main)
stack_trace.on = TRUE; // turn on runtime tracing
goodbye_cruel_world();
stack_trace.on = FALSE;
RETURN(EXIT_SUCCESS);
END
Output:
goodbye_cruel_world.c:8: BEGIN goodbye_cruel_world[0x80486a8], stack(depth:1, size:60)
goodbye_cruel_world.c:8: goodbye_cruel_world[0x80486a8] --- stack(depth:2, size:60) ---
goodbye_cruel_world.c:14: main[0x80486f4] --- stack(depth:1, size:0) ---
goodbye_cruel_world.c:8: --- (depth 2) ---
*/
#ifndef _LINUX_STDDEF_H
#include <stddef.h>
#endif
typedef struct stack_trace_frame_s {
const char *file_name;
int file_line;
const char *proc_name;
void *proc_addr;
struct stack_trace_frame_s *down, *up;
} stack_trace_frame_t;
#define SKIP
typedef enum {TRUE=1, FALSE=0} bool_t;
typedef struct {
bool_t on;
struct { const char *_begin, *_print, *_return, *_exit, *_end; } fmt;
struct { int depth; stack_trace_frame_t *lwb, *upb; } stack;
struct { int lwb, by, upb; const char *prefix; } indent;
} stack_trace_t;
extern stack_trace_t stack_trace;
void stack_trace_begin(char *SKIP, stack_trace_frame_t *SKIP);
void stack_trace_end(char *SKIP, int SKIP);
void print_stack_trace();
#ifdef STACK_TRACE_ON
/* Many ThanX to Steve R Bourne for inspiring the following macros ;-) */
#define BEGIN(x) { auto stack_trace_frame_t this = {__FILE__, __LINE__, #x, &x, NULL, NULL}; \
stack_trace_begin(stack_trace.fmt._begin, &this); {
#define RETURN(x) { stack_trace_end(stack_trace.fmt._return, __LINE__); return(x); }
#define EXIT(x) { stack_trace_end(stack_trace.fmt._exit, __LINE__); exit(x); }
#define END } stack_trace_end(stack_trace.fmt._end, __LINE__); }
#else
/* Apologies to Landon Curt Noll and Larry Bassel for the following macros :-) */
#define BEGIN(x) {
#define RETURN(x) return(x)
#define EXIT(x) exit(x)
#define END }
#endif

View file

@ -0,0 +1,85 @@
#include <stdio.h>
#include <stddef.h>
#define STACK_TRACE_ON
#include "stack_trace.h"
#define indent_fmt "%s"
#define std_cc_diag_fmt "%s:%d: "
#define stack_trace_diag_fmt " %s[0x%x], stack(depth:%d, size:%u)\n"
#define stack_trace_fmt "%s:%d:\t%s[0x%x]\t--- stack(depth:%d, size:%u) ---\n"
stack_trace_t stack_trace = {
FALSE, /* default: stack_trace.on == FALSE */
{ std_cc_diag_fmt""indent_fmt"BEGIN"stack_trace_diag_fmt,
stack_trace_fmt,
std_cc_diag_fmt""indent_fmt"RETURN"stack_trace_diag_fmt,
std_cc_diag_fmt""indent_fmt"EXIT"stack_trace_diag_fmt,
std_cc_diag_fmt""indent_fmt"END"stack_trace_diag_fmt },
{ 0, (stack_trace_frame_t*)NULL, (stack_trace_frame_t*)NULL }, /* stack */
{ 19, 2, 20, " " } /* indent wrap */
};
void stack_trace_begin(const char *fmt, stack_trace_frame_t *this){
if(stack_trace.on){
fprintf(stderr, fmt,
this->file_name, this->file_line, /* file details */
stack_trace.indent.prefix+stack_trace.indent.lwb,
this->proc_name, this->proc_addr, /* procedure details */
stack_trace.stack.depth, (unsigned)stack_trace.stack.lwb-(unsigned)this);
stack_trace.indent.lwb =
( stack_trace.indent.lwb - stack_trace.indent.by ) % stack_trace.indent.upb;
}
if(!stack_trace.stack.upb){ /* this IS the stack !! */
stack_trace.stack.lwb = stack_trace.stack.upb = this;
} else {
this -> down = stack_trace.stack.upb;
stack_trace.stack.upb -> up = this;
stack_trace.stack.upb = this;
}
stack_trace.stack.depth++;
}
void stack_trace_end(const char *fmt, int line){
stack_trace.stack.depth--;
if(stack_trace.on){
stack_trace.indent.lwb =
( stack_trace.indent.lwb + stack_trace.indent.by ) % stack_trace.indent.upb;
stack_trace_frame_t *this = stack_trace.stack.upb;
fprintf(stderr, fmt,
this->file_name, this->file_line, /* file details */
stack_trace.indent.prefix+stack_trace.indent.lwb,
this->proc_name, this->proc_addr, /* procedure details */
stack_trace.stack.depth, (unsigned)stack_trace.stack.lwb-(unsigned)this);
}
stack_trace.stack.upb = stack_trace.stack.upb -> down;
}
void print_indent(){
if(!stack_trace.stack.upb){
/* fprintf(stderr, "STACK_TRACE_ON not #defined during compilation\n"); */
} else {
stack_trace_frame_t *this = stack_trace.stack.upb;
fprintf(stderr, std_cc_diag_fmt""indent_fmt,
this->file_name, this->file_line, /* file details */
stack_trace.indent.prefix+stack_trace.indent.lwb
);
}
}
void print_stack_trace() {
if(!stack_trace.stack.upb){
/* fprintf(stderr, "STACK_TRACE_ON not #defined during compilation\n"); */
} else {
int depth = stack_trace.stack.depth;
stack_trace_frame_t *this = stack_trace.stack.upb;
for(this = stack_trace.stack.upb; this; this = this->down, depth--){
fprintf(stderr, stack_trace.fmt._print,
this->file_name, this->file_line, /* file details */
this->proc_name, this->proc_addr, /* procedure details */
depth, (unsigned)stack_trace.stack.lwb-(unsigned)this);
}
print_indent(); fprintf(stderr, "--- (depth %d) ---\n", stack_trace.stack.depth);
}
}

View file

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#define STACK_TRACE_ON /* compile in these "stack_trace" routines */
#include "stack_trace.h"
void inner(int k)
BEGIN(inner)
print_indent(); printf("*** Now dump the stack ***\n");
print_stack_trace();
END
void middle(int x, int y)
BEGIN(middle)
inner(x*y);
END
void outer(int a, int b, int c)
BEGIN(outer)
middle(a+b, b+c);
END
int main()
BEGIN(main)
stack_trace.on = TRUE; /* turn on runtime tracing */
outer(2,3,5);
stack_trace.on = FALSE;
RETURN(EXIT_SUCCESS);
END

View file

@ -0,0 +1,2 @@
(doall
(map println (.dumpAllThreads (java.lang.management.ManagementFactory/getThreadMXBean) false false)))

View file

@ -0,0 +1,3 @@
(swank-backend:call-with-debugging-environment
(lambda ()
(swank:backtrace 0 nil)))

View file

@ -0,0 +1,4 @@
((0 "((LAMBDA (SWANK-BACKEND::DEBUGGER-LOOP-FN)) #<FUNCTION (LAMBDA #) {100459BBC9}>)")
(1 "(SB-INT:SIMPLE-EVAL-IN-LEXENV (SWANK-BACKEND:CALL-WITH-DEBUGGING-ENVIRONMENT (LAMBDA () (SWANK:BACKTRACE 0 NIL))) #<NULL-LEXENV>)")
(2 "(SWANK::EVAL-REGION \"(swank-backend:call-with-debugging-environment\\n (lambda ()\\n (swank:backtrace 0 nil)))\\n\\n\")")
(3 "((LAMBDA ()))") ...)

View file

@ -0,0 +1,17 @@
CL-USER> (sb-debug:backtrace 7)
0: (SB-DEBUG::MAP-BACKTRACE
#<CLOSURE (LAMBDA (SB-DEBUG::FRAME)) {1193EFCD}>)[:EXTERNAL]
1: (BACKTRACE
7
#<TWO-WAY-STREAM
:INPUT-STREAM #<SWANK-BACKEND::SLIME-INPUT-STREAM {120F6519}>
:OUTPUT-STREAM #<SWANK-BACKEND::SLIME-OUTPUT-STREAM {1208F3E1}>>)
2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (BACKTRACE 7) #<NULL-LEXENV>)
3: (SWANK::EVAL-REGION
"(sb-debug:backtrace 7)
")
4: ((LAMBDA ()))
5: (SWANK::TRACK-PACKAGE #<CLOSURE (LAMBDA ()) {1193ECBD}>)
6: (SWANK::CALL-WITH-RETRY-RESTART
"Retry SLIME REPL evaluation request."
#<CLOSURE (LAMBDA ()) {1193EC4D}>)

View file

@ -0,0 +1,10 @@
import std.stdio, core.runtime;
void inner() { defaultTraceHandler.writeln; }
void middle() { inner; }
void outer() { middle; }
void main() {
outer;
"After the stack trace.".writeln;
}

View file

@ -0,0 +1,21 @@
procedure Inner;
begin
try
raise Exception.Create('');
except
on E: Exception do
PrintLn(E.StackTrace);
end;
end;
procedure Middle;
begin
Inner;
end;
procedure Outer;
begin
Middle;
end;
Outer;

View file

@ -0,0 +1,25 @@
import extensions;
public singleton program
{
inner()
{
console.printLine(new CallStack())
}
middle()
{
self.inner()
}
outer()
{
self.middle()
}
// program entry point
function()
{
program.outer()
}
}

View file

@ -0,0 +1,25 @@
defmodule Stack_traces do
def main do
{:ok, a} = outer
IO.inspect a
end
defp outer do
{:ok, a} = middle
{:ok, a}
end
defp middle do
{:ok, a} = inner
{:ok, a}
end
defp inner do
try do
throw(42)
catch 42 -> {:ok, :erlang.get_stacktrace}
end
end
end
Stack_traces.main

View file

@ -0,0 +1,18 @@
-module(stack_traces).
-export([main/0]).
main() ->
{ok,A} = outer(),
io:format("~p\n", [A]).
outer() ->
{ok,A} = middle(),
{ok,A}.
middle() ->
{ok,A} = inner(),
{ok,A}.
inner() ->
try throw(42) catch 42 -> {ok,erlang:get_stacktrace()} end.

View file

@ -0,0 +1,12 @@
open System.Diagnostics
type myClass() =
member this.inner() = printfn "%A" (new StackTrace())
member this.middle() = this.inner()
member this.outer() = this.middle()
[<EntryPoint>]
let main args =
let that = new myClass()
that.outer()
0

View file

@ -0,0 +1,2 @@
USE: prettyprint
get-callstack callstack.

View file

@ -0,0 +1,14 @@
[UNDEFINED] R.S [IF]
\ Return stack counterpart of DEPTH
\ Note the STACK-CELLS correction is there to hide RDEPTH itself
( -- n)
: RDEPTH STACK-CELLS -2 [+] CELLS RP@ - ;
\ Return stack counterpart of .S
\ Note the : R.S R> .. >R ; sequence is there to hide R.S itself
( --)
: R.S R> CR RDEPTH DUP 0> IF DUP
BEGIN DUP WHILE R> -ROT 1- REPEAT DROP DUP
BEGIN DUP WHILE ROT DUP . >R 1- REPEAT DROP
THEN ." (TORS) " DROP >R ;
[THEN]

View file

@ -0,0 +1,21 @@
#include "windows.bi"
Private Function Fn2() As Long
Dim frames(0 To 60) As Any Ptr
Dim framesPtr As Any Ptr Ptr = @frames(0)
Dim hash As DWORD
Dim As Long caught = CaptureStackBackTrace(0, 61, framesPtr, @hash)
Print Using "Caught & frames using stack capture"; caught
For i As Long = 0 To caught - 1
Print Using "&) &"; caught - i; Hex(frames(i))
Next
Return caught
End Function
Private Sub Fn1(num As Ulong)
Dim As Long numFn2 = Fn2()
Print Using "Fn2 returned & with num = &"; numFn2; num
End Sub
Fn1(87)
Sleep

View file

@ -0,0 +1,14 @@
package main
import (
"fmt"
"runtime"
)
func main() {
stackTrace := make([]byte, 1024)
n := runtime.Stack(stackTrace, true)
stackTrace = stackTrace[:n]
fmt.Printf("%s\n", stackTrace)
fmt.Printf("(%d bytes)\n", len(stackTrace))
}

View file

@ -0,0 +1 @@
def rawTrace = { Thread.currentThread().stackTrace }

View file

@ -0,0 +1,14 @@
def trace = rawTrace().collect {
def props = it.properties
def keys = (it.properties.keySet() - (new Object().properties.keySet()))
props.findAll{ k, v -> k in keys }
}
def propNames = trace[0].keySet().sort()
def propWidths = propNames.collect { name -> [name, trace.collect{ it[name].toString() }].flatten()*.size().max() }
propNames.eachWithIndex{ name, i -> printf("%-${propWidths[i]}s ", name) }; println ''
propWidths.each{ width -> print('-' * width + ' ') }; println ''
trace.each {
propNames.eachWithIndex{ name, i -> printf("%-${propWidths[i]}s ", it[name].toString()) }; println ''
}

View file

@ -0,0 +1,16 @@
import Utils # for buildStackTrace
procedure main()
g()
write()
f()
end
procedure f()
g()
end
procedure g()
# Using 1 as argument omits the trace of buildStackTrace itself
every write("\t",!buildStackTrace(1))
end

View file

@ -0,0 +1,22 @@
#<p>
# Compute the current stack trace. Starting at level <i>n</i> above
# the current procedure. Here, <i>n</i> defaults to 0, which will
# include this procedure in the stack trace.
# <i>ce</i> defaults to &current.
# <i>This only works with newer versions of Unicon!</i>
# <[generates the stacktrace from current call back to first
# in the co-expression]>
#</p>
procedure buildStackTrace(n:0, # starting distance from this call
ce # co-expr to trace stack in [&current]
)
local L
/ce := &current
L := []; n -:= 1
while pName := image(proc(ce, n+:=1)) do {
fName := keyword("&file",ce,n) | "no file name"
fLine := keyword("&line",ce,n) | "no line number"
put(L, pName||" ["||fName||":"||fLine||"]" )
}
return L
end

View file

@ -0,0 +1 @@
13!:0]1

View file

@ -0,0 +1 @@
13!:13''

View file

@ -0,0 +1,14 @@
f=:g
g=:13!:13 bind ''
f 7 NB. empty stack trace because debugging has not been enabled
13!:0]1
f 7
┌─┬─┬─┬─┬─────────────┬┬───┬──┬─┐
│g│0│0│3│13!:13@(''"_)││┌─┐│ │ │
│ │ │ │ │ │││7││ │ │
│ │ │ │ │ ││└─┘│ │ │
├─┼─┼─┼─┼─────────────┼┼───┼──┼─┤
│f│0│0│3│g ││┌─┐│ │ │
│ │ │ │ │ │││7││ │ │
│ │ │ │ │ ││└─┘│ │ │
└─┴─┴─┴─┴─────────────┴┴───┴──┴─┘

View file

@ -0,0 +1,11 @@
public class StackTracer {
public static void printStackTrace() {
StackTraceElement[] elems = Thread.currentThread().getStackTrace();
System.out.println("Stack trace:");
for (int i = elems.length-1, j = 2 ; i >= 3 ; i--, j+=2) {
System.out.printf("%" + j + "s%s.%s%n", "",
elems[i].getClassName(), elems[i].getMethodName());
}
}
}

View file

@ -0,0 +1,14 @@
public class StackTraceDemo {
static void inner() {
StackTracer.printStackTrace();
}
static void middle() {
inner();
}
static void outer() {
middle();
}
public static void main(String[] args) {
outer();
}
}

View file

@ -0,0 +1,5 @@
try {
throw new Error;
} catch(e) {
alert(e.stack);
}

View file

@ -0,0 +1,9 @@
function foo () {
var stack = "Stack trace:";
for (var f = arguments.callee // current function
; f; f = f.caller) {
stack += "\n" + f.name;
}
alert(stack);
}
foo();

View file

@ -0,0 +1,4 @@
f() = g()
g() = println.(stacktrace())
f()

View file

@ -0,0 +1,10 @@
// version 1.1.2 (stacktrace.kt which compiles to StacktraceKt.class)
fun myFunc() {
println(Throwable().stackTrace.joinToString("\n"))
}
fun main(args:Array<String>) {
myFunc()
println("\nContinuing ... ")
}

View file

@ -0,0 +1,19 @@
# fn.getStackTrace() returns a text-based stack trace
fp.printStackTrace = () -> fn.println(fn.getStackTrace())
# Example
fp.f1 = () -> {
fn.println(F1:)
fp.printStackTrace()
}
fp.f2 = () -> {
fn.println(F2:)
fp.printStackTrace()
fp.f1()
}
fp.f2()
fn.combA0(fp.f2)
# Partially called combinator functions' names are represented as "<comb...-func(...)>"
fn.combA(fn.combC(fn.combAE(), x), fp.f2)

View file

@ -0,0 +1,24 @@
// Define our own trace method
define trace => {
local(gb) = givenblock
// Set a depth counter
var(::_tracedepth)->isnota(::integer) ? $_tracedepth = 0
handle => {$_tracedepth--}
// Only output when supplied a capture
#gb ? stdoutnl(
// Indent
('\t' * $_tracedepth++) +
// Type + Method
#gb->self->type + '.' + #gb->calledname +
// Call site file
': ' + #gb->home->callsite_file +
// Line number and column number
' (line '+#gb->home->callsite_line + ', col ' + #gb->home->callsite_col +')'
)
return #gb()
}

View file

@ -0,0 +1,8 @@
define stackexample => type {
public oncreate => trace => { return self }
public inner => trace => { }
public middle => trace => { .inner }
public outer => trace => { .middle }
}
stackexample->outer

View file

@ -0,0 +1,14 @@
function Inner( k )
print( debug.traceback() )
print "Program continues..."
end
function Middle( x, y )
Inner( x+y )
end
function Outer( a, b, c )
Middle( a*b, c )
end
Outer( 2, 3, 5 )

View file

@ -0,0 +1 @@
f[g[1, Print[Stack[]]; 2]]

View file

@ -0,0 +1,2 @@
{f,g,CompoundExpression,Print}
f[g[1, 2]]

View file

@ -0,0 +1 @@
f[g[1, Print[Stack[_]]; 2]]

View file

@ -0,0 +1,2 @@
{f[g[1,Print[Stack[_]];2]],g[1,Print[Stack[_]];2],Print[Stack[_]];2,Print[Stack[_]]}
f[g[1, 2]]

View file

@ -0,0 +1,18 @@
def print_stack()
global __calls__
println "stack trace:"
for i in range(len(__calls__) - 2, 0)
println "\t" + __calls__[i]
end
end
print_stack()
println
for i in range(1, 1)
print_stack()
end
println
println "The program would continue."

View file

@ -0,0 +1,22 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
class RStackTraces
method inner() static
StackTracer.printStackTrace()
method middle() static
inner()
method outer() static
middle()
method main(args = String[]) public static
outer()
class RStackTraces.StackTracer
method printStackTrace() public static
elems = Thread.currentThread().getStackTrace()
say 'Stack trace:'
j_ = 2
loop i_ = elems.length - 1 to 2 by -1
say ''.left(j_) || elems[i_].getClassName()'.'elems[i_].getMethodName()
j_ = j_ + 2
end i_

View file

@ -0,0 +1,12 @@
proc g() =
# Writes the current stack trace to stderr.
writeStackTrace()
# Or fetch the stack trace entries for the current stack trace:
echo "----"
for e in getStackTraceEntries():
echo e.filename, "@", e.line, " in ", e.procname
proc f() =
g()
f()

View file

@ -0,0 +1,8 @@
let div a b = a / b
let () =
try let _ = div 3 0 in ()
with e ->
prerr_endline(Printexc.to_string e);
Printexc.print_backtrace stderr;
;;

View file

@ -0,0 +1,5 @@
let div a b = a / b
let () =
let _ = div 3 0 in ()
;;

View file

@ -0,0 +1,9 @@
#include <execinfo.h>
void *frames[128];
int len = backtrace(frames, 128);
char **symbols = backtrace_symbols(frames, len);
for (int i = 0; i < len; ++i) {
NSLog(@"%s", symbols[i]);
}
free(symbols);

View file

@ -0,0 +1,4 @@
NSArray *symbols = [NSThread callStackSymbols];
for (NSString *symbol in symbols) {
NSLog(@"%@", symbol);
}

View file

@ -0,0 +1,6 @@
: f1 Exception throw("An exception") ;
Integer method: f2 self f1 ;
: f3 f2 ;
: f4 f3 ;
10 f4

View file

@ -0,0 +1,80 @@
'32bit x86
static string Report
macro ReportStack(n)
'===================
'
scope
'
static sys stack[0X100],stackptr,e
'
'CAPTURE IMAGE OF UP TO 256 ENTRIES
'
'
mov eax,n
cmp eax,0x100
(
jle exit
mov eax,0x100 'UPPER LIMIT
)
mov e,eax
mov stackptr,esp
lea edx,stack
mov ecx,e
mov esi,esp
(
mov eax,[esi]
mov [edx],eax
add esi,4
add edx,4
dec ecx
jg repeat
)
sys i
string cr=chr(13)+chr(10), tab=chr(9)
'
for i=1 to e
report+=hex(stackptr+(i-1)*4,8) tab hex(i-1,2) tab hex(stack[i],8) cr
next
'
end scope
'
end macro
'====
'TEST
'====
function foo()
'=============
push 0x44556677
push 0x33445566
push 0x22334455
push 0x11223344
ReportStack(8)
end function
Report+="Trace inside foo
"
foo()
print report
'putfile "s.txt",Report
/*
RESULT:
Trace inside foo
0017FE00 00 11223344
0017FE04 01 22334455
0017FE08 02 33445566
0017FE0C 03 44556677
0017FE10 04 005EAB1C
0017FE14 05 0017FE40
0017FE18 06 10002D5F
0017FE1C 07 00000000
*/

View file

@ -0,0 +1,10 @@
declare
proc {Test}
_ = 1 div 0
end
in
try
{Test}
catch E then
{Inspect E}
end

View file

@ -0,0 +1,15 @@
%% make sure that simple function calls are not optimized away
\switch +controlflowinfo
declare
[Debug] = {Link ['x-oz://boot/Debug']}
proc {F} {G} end
proc {G} {H} end
proc {H}
{Inspect {Debug.getTaskStack {Thread.this} 100 true}}
end
in
{F}

View file

@ -0,0 +1,15 @@
<?php
class StackTraceDemo {
static function inner() {
debug_print_backtrace();
}
static function middle() {
self::inner();
}
static function outer() {
self::middle();
}
}
StackTraceDemo::outer();
?>

View file

@ -0,0 +1,7 @@
/* The SNAP option in the ON statement is sufficient to obtain */
/* a traceback. The SYSTEM option specifies that standard */
/* system action is to occur, which resume execution after the */
/* SIGNAL statement. */
on condition(traceback) snap system;
...
signal condition(traceback);

View file

@ -0,0 +1,6 @@
use Carp 'cluck';
sub g {cluck 'Hello from &g';}
sub f {g;}
f;

View file

@ -0,0 +1,58 @@
(notonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">W</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">RTN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PREVEBP</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">W</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_stack</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">symtab</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">symtabN</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rtn</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">prev_ebp</span>
#ilASM{
[32]
lea edi,[symtab]
call :%opGetST -- [edi]=symtab (ie our local:=the real symtab)
mov edi,[ebp+20] -- prev_ebp
mov eax,[edi+8] -- calling routine no
mov [rtn],eax
mov eax,edi
lea edi,[prev_ebp]
call :%pStoreMint
[64]
lea rdi,[symtab]
call :%opGetST -- [rdi]=symtab (ie our local:=the real symtab)
mov rdi,[rbp+40] -- prev_ebp
mov rax,[rdi+16] -- calling routine no
mov [rtn],rax
mov rax,rdi
lea rdi,[prev_ebp]
call :%pStoreMint
[]
}
<span style="color: #008080;">while</span> <span style="color: #000000;">rtn</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">21</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (T_maintls, main top level routine, always present)</span>
<span style="color: #000000;">symtabN</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">symtab</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rtn</span><span style="color: #0000FF;">]</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">symtabN</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">prev_ebp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peekNS</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prev_ebp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">PREVEBP</span><span style="color: #0000FF;">,</span><span style="color: #000000;">W</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">rtn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peekNS</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prev_ebp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">RTN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">W</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">three</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">die</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">die</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">show_stack</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">two</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">die</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">three</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">one</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">die</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">two</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">one</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dummy"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- see note below</span>
<span style="color: #000000;">one</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,3 @@
-->
<span style="color: #008080;">trace</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,36 @@
(off "Stack")
(de $$ "Prg"
(let "Stack" (cons (cons (car "Prg") (env)) "Stack") # Build stack frame
(set "Stack"
(delq (asoq '"Stack" (car "Stack")) # Remove self-created entries
(delq (asoq '"Prg" (car "Stack"))
(car "Stack") ) ) )
(run (cdr "Prg")) ) ) # Run body
(de stackAll (Excl)
(let *Dbg NIL
(for "X" (all)
(or
(memq "X" Excl)
(memq "X" '($$ @ @@ @@@))
(= `(char "*") (char "X"))
(cond
((= `(char "+") (char "X"))
(for "Y" (pair (val "X"))
(and
(pair "Y")
(fun? (cdr "Y"))
(unless (== '$$ (caaddr "Y"))
(con (cdr "Y")
(list
(cons '$$ (cons (car "Y" "X") (cddr "Y"))) ) ) ) ) ) )
((pair (getd "X"))
(let "Y" @
(unless (== '$$ (caadr "Y"))
(con "Y"
(list (cons '$$ "X" (cdr "Y"))) ) ) ) ) ) ) ) ) )
(de dumpStack ()
(more (reverse (cdr "Stack")))
T )

View file

@ -0,0 +1,9 @@
(de foo (A B)
(let C 3
(bar (inc 'A) (inc 'B) (inc 'C)) ) )
(de bar (A D E)
(let (A 7 B 8 C 9)
(! println A B C) ) ) # Set a breakpoint before (println A B C)
(stackAll)

View file

@ -0,0 +1,17 @@
Procedure Three()
a=7
ShowCallstack()
CallDebugger
EndProcedure
Procedure Two()
a=4
Three()
EndProcedure
Procedure One()
a=2
Two()
EndProcedure
One()

View file

@ -0,0 +1,6 @@
import traceback
def f(): return g()
def g(): traceback.print_stack()
f()

View file

@ -0,0 +1,10 @@
foo <- function()
{
bar <- function()
{
sys.calls()
}
bar()
}
foo()

View file

@ -0,0 +1,2 @@
trace("foo", recover)
foo()

View file

@ -0,0 +1,30 @@
/* call stack */
say 'Call A'
call A '123'
say result
exit 0
A:
say 'Call B'
call B '456'
say result
return ARG(1)
B:
say 'Call C'
call C '789'
say result
return ARG(1)
C:
call callstack
return ARG(1)
callstack: procedure
getcallstack(cs.)
say 'Dump call stack with' cs.0 'items'
do i = 1 to cs.0
parse var cs.i line func
say format(line, 3) ':' left(func, 9) ': source "' || sourceline(line) || '"'
end
return cs.0

View file

@ -0,0 +1,4 @@
C:
signal noname
call callstack
return ARG(1)

View file

@ -0,0 +1,17 @@
#lang racket
;; To see these calls we do two things: mutate the binding to prevent
;; Racket from inlining the value; use a (void) call at the end so the
;; calls are not tail calls (which will otherwise not show on the
;; stack).
(define foo #f)
(set! foo (λ() (bar) (void)))
(define bar #f)
(set! bar (λ() (show-stacktrace) (void)))
(define (show-stacktrace)
(for ([s (continuation-mark-set->context (current-continuation-marks))]
[i (in-naturals)])
;; show just the names, not the full source information
(when (car s) (printf "~s: ~s\n" i (car s)))))
(foo)

View file

@ -0,0 +1,3 @@
sub g { say Backtrace.new.concise }
sub f { g }
sub MAIN { f }

View file

@ -0,0 +1 @@
[1 2 3 4] 42 { 'a' 1 'b' 2 'c' 3 } 34.1234 ( -1 -2 -3 ) "The quick brown fox" FILE dump

View file

@ -0,0 +1,14 @@
def outer(a,b,c)
middle a+b, b+c
end
def middle(d,e)
inner d+e
end
def inner(f)
puts caller(0)
puts "continuing... my arg is #{f}"
end
outer 2,3,5

View file

@ -0,0 +1,19 @@
def outer(a,b,c)
middle a+b, b+c
end
def middle(d,e)
inner d+e
end
def inner(f)
raise
puts "this will not be printed"
end
begin
outer 2,3,5
rescue Exception => e
puts e.backtrace
end
puts "continuing after the rescue..."

View file

@ -0,0 +1 @@
p Thread.current.backtrace

View file

@ -0,0 +1,3 @@
def callStack = try { error("exception") } catch { case ex => ex.getStackTrace drop 2 }
def printStackTrace = callStack drop 1 /* don't print ourselves! */ foreach println

View file

@ -0,0 +1,8 @@
slate[1]> d@(Debugger traits) printCurrentStack &limit: limit &stream: out &showLocation: showLocation
[
d clone `>> [baseFramePointer: (d interpreter framePointerOf: #printCurrentStack).
buildFrames.
printBacktrace &limit: limit &stream: out &showLocation: showLocation ]
].
Defining function 'printCurrentStack' on: 'Debugger traits'
[printCurrentStack &limit: &stream: &showLocation:]

View file

@ -0,0 +1,14 @@
slate[2]> Debugger printCurrentStack.
Backtrace (method @ source):
frame: 0 [printCurrentStack &limit: &stream: &showLocation:] @ stdin:0
frame: 1 [evaluateIn: &optionals:] @ src/mobius/syntax.slate:180
frame: 2 [(arity: 0)] @ src/lib/repl.slate:155
frame: 3 [on:do:] @ src/core/condition.slate:43
frame: 4 [(arity: 0)] @ src/lib/repl.slate:147
frame: 5 [handlingCases:] @ src/core/condition.slate:64
frame: 6 [interpretHook:] @ src/lib/repl.slate:42
frame: 7 [(arity: 0)] @ src/lib/repl.slate:139
frame: 8 [enter] @ src/lib/repl.slate:135
frame: 9 [start &resource:] @ src/lib/repl.slate:185
frame: 10 [start] @ src/mobius/prelude.slate:38
Nil

View file

@ -0,0 +1,15 @@
Object subclass: Container [
Container class >> outer: a and: b and: c [
self middle: (a+b) and: (b+c)
]
Container class >> middle: x and: y [
self inner: (x*y)
]
Container class >> inner: k [
Smalltalk backtrace
]
].
Container outer: 2 and: 3 and: 5.
'Anyway, we continue with it' displayNl.

View file

@ -0,0 +1,6 @@
proc printStackTrace {} {
puts "Stack trace:"
for {set i 1} {$i < [info level]} {incr i} {
puts [string repeat " " $i][info level $i]
}
}

View file

@ -0,0 +1,10 @@
proc outer {a b c} {
middle [expr {$a+$b}] [expr {$b+$c}]
}
proc middle {x y} {
inner [expr {$x*$y}]
}
proc inner k {
printStackTrace
}
outer 2 3 5

View file

@ -0,0 +1,9 @@
var func2 = Fn.new {
Fiber.abort("Forced error.")
}
var func1 = Fn.new {
func2.call()
}
func1.call()

View file

@ -0,0 +1,2 @@
fcn f{println("F");vm.stackTrace().println()} fcn g{println("G")}
f();g();