shithub: sl

Download patch

ref: 32a1b47da6fe0e872c66d2721ff221ff1a5e8ba8
parent: 54c693d34204bae61ed9784bb9e8a8be22606711
author: spew <spew@cbza.org>
date: Fri Mar 28 21:43:37 EDT 2025

start adding some c data accessors

--- a/src/plan9/lsd.c
+++ b/src/plan9/lsd.c
@@ -368,7 +368,7 @@
 		type_error("num", args[0]);
 	addr = tosize(args[0]);
 
-	n = (*machdata->foll)(coremap, addr, rget, f);
+	n = machdata->foll(coremap, addr, rget, f);
 	if(n < 0)
 		lerrorf(sl_errio, "follow(%ux): %r", addr);
 
@@ -459,4 +459,12 @@
 	if(!findsym(addr, CTEXT, &s))
 		lerrorf(sl_errio, "could not locate sym near %ud", addr);
 	return mk_symbol(&s);
+}
+
+sl_purefn
+BUILTIN("lsd-ptrsize", lsd_ptrsize)
+{
+	USED(args);
+	argcount(nargs, 0);
+	return sizeof(void*) == 4 ? sl_u32sym : sl_u64sym;
 }
--- a/src/plan9/lsd.sl
+++ b/src/plan9/lsd.sl
@@ -4,11 +4,12 @@
   "Debugging functionality.")
 
 (defstruct reg
-  "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`
+  "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)`"
@@ -15,23 +16,30 @@
   :doc-group lsd
   :doc-see readreg
   name type addr size)
+  
 (defstruct symbol
-  "A symbol of the process.  Name is a string denoting the symbol,
-   type is a character as described in a.out(6), and address is the
-   location of the symbol in the process address space"
+  "A symbol of the process.
+  
+  Name is a string denoting the symbol, type is a character as
+  described in a.out(6), and address is the location of the symbol in
+  the process address space"
   :doc-group lsd
   name type addr)
+  
 (defstruct global
   "All the global symbols, separated into text and data symbols.
 
-   The text and data fields are both tables from syms to symbols."
+  The text and data fields are both tables from syms to symbols."
   :doc-group lsd
   :doc-see symbol
   text data)
+  
 (defstruct frame
-  "A stack frame.  Sym is the enclosing function symbol and instruction
-   address of the frame.  Retpc is the return instruction address.  Sp is
-   the stack pointer value.  Locals are all the local symbols."
+  "A stack frame.
+  
+  Sym is the enclosing function symbol and instruction address of the
+  frame.  Retpc is the return instruction address.  Sp is the stack
+  pointer value.  Locals are all the local symbols."
   :doc-group lsd
   :doc-see symbol
   sym retpc sp locals)
@@ -121,7 +129,34 @@
   :doc-see sym-addr
   (unless coref (error "not attached to proc"))
   (apply readcore (cons (symbol-addr symbol) rest)))
+  
+(def (loc->addr loc)
+  (cond ((sym? loc) (symbol-addr
+                       (or (sym-local loc)
+                           (sym-global loc))))
+        ((num? loc) (u64 loc))
+        ((symbol? loc) (symbol-addr loc))
+        (else (error "sym|num|symbol"))))
 
+(def (read-loc loc sz)
+  (readcore (loc->addr loc) sz))
+
+(def (c-ptr loc) (read-loc loc ptrsz))
+
+(def (c-int loc) (read-loc loc 's32))
+
+(def c-long c-int)
+
+(def (c-uint loc) (read-loc loc 'u32))
+
+(def (c-str loc)
+  (def (go a)
+    (let {[v (readcore a 'byte)]}
+      (if (= v (byte 0))
+          ()
+          (cons v (go (1+ a))))))
+  (apply arr (cons 'byte (go (c-ptr (loc->addr loc))))))
+
 (def (hex n)
   "Display an integer in hex format."
   :doc-group lsd
@@ -228,6 +263,8 @@
   "power64" (λ () (lsd-ctrace (curPC) (readreg SP) (readreg LR)))))
 
 (def ctrace (get tracers (os-getenv "objtype")))
+
+(def ptrsz (lsd-ptrsize))
 
 (def (_stk (:level 0) (:n NIL))
   "Return the call stack in the form of a list of stack frames.