shithub: sl

Download patch

ref: 51051fb643bbb36db7a365cd639bbd5b75b0a1f6
parent: 7c3809229021fa3206971e359ed6c7c0f538897a
author: spew <spew@cbza.org>
date: Mon Mar 24 12:21:52 EDT 2025

lsd: implement (stk)

--- a/src/plan9/lsd.sl
+++ b/src/plan9/lsd.sl
@@ -1,8 +1,14 @@
 #!/bin/sl -i
 
 (defstruct reg
-  "A register of the process.  The fields are internal.  To read the
-   value of a register use `readreg`"
+  "A register of the processor.  All registers are exposed as
+   top-level symbols.  The fields are internal.  The top-level symbol
+   `registers` is a list of all available registers.  To read the
+   value of a register use `readreg`
+
+   Examples:
+
+       `(readreg AX)`"
   :doc-group lsd
   :doc-see readreg
   name type addr size)
@@ -209,18 +215,34 @@
   (reverse! (ctrace)))
 
 (def (stk)
-  "Pretty print the stack trace without showing locals.  Still WIP."
+  "Print the stack trace without showing local auto values.
+
+   Bugs:
+
+       Prints the values of parameters as unsigned 64 bit
+       integers, not as their actual types."
   :doc-group lsd
   :doc-see _stk
-  (def (go f)
-    (receive (params autos)
-             (partition (λ (x) (equal? #\p (symbol-type x)))
-                        (frame-locals f))
-      (set! params (sort params #.< :key symbol-addr))
-      (set! autos (sort autos #.> :key symbol-addr))
-      (princ (symbol-name (frame-loc f))
-             "(" params ")\n")))
-  (for-each go (_stk)))
+  (def (go pc fs)
+    (if (not fs) pc
+      (let* {[f (car fs)]
+             [floc (frame-loc f)]}
+        (receive (params autos)
+                 (partition (λ (x) (equal? #\p (symbol-type x)))
+                            (frame-locals f))
+          (set! params (sort params #.< :key symbol-addr))
+          (set! autos (sort autos #.> :key symbol-addr))
+          (princ (symbol-name floc) "(")
+          (princ
+            (str-join
+              (map (λ (p)
+                     (str (symbol-name p) "=" (hex (symbol-read p 'u64))))
+                   params)
+              ", "))
+          (princ ")+" (hex (- pc (symbol-addr floc))) " ")
+          (princ (src pc) "\n")
+          (go (frame-retpc f) (cdr fs))))))
+  (go (curPC) (_stk)))
 
 (def (curPC) (and (>= pid 0) (readreg PC)))
 
@@ -434,7 +456,7 @@
          [text (foldl f (table) (aref v 3))]
          [data (foldl f (table) (aref v 4))]}
     (set! pid (aref v 0))
-    (set! registers (aref v 1))
+    (set! registers (reverse! (aref v 1)))
     (set! bpinst (aref v 2))
     (set! globals (make-global :text text :data data)))
   (and (>= pid 0) (attach)))