shithub: sl

Download patch

ref: c1a95ad2f6d2211d3d562a17b44450836ba44d51
parent: db0e4ee44e5bdbc938414f6dcb2cf7d439732d32
author: spew <spew@cbza.org>
date: Wed Apr 9 22:55:18 EDT 2025

lsd: reimplement the accessors, update documentation

--- a/src/plan9/lsd.c
+++ b/src/plan9/lsd.c
@@ -443,7 +443,7 @@
 	addr = file2pc(file, line);
 	if(addr == ~0)
 		bthrow(lerrorf(sl_errio, "could not find address of %s:%d", file, line));
-	return size_wrap(addr);
+	return mk_ptr(addr);
 }
 
 BUILTIN("lsd-findsym", lsd_findsym)
--- a/src/plan9/lsd.sl
+++ b/src/plan9/lsd.sl
@@ -145,16 +145,65 @@
   :doc-see global-symbol
   `(core-read (symbol-addr ,symbol) ,.rest))
 
-(def (symbol-find s)
-  "Return a symbol, searching first in the local stack frame then
-   in the global symbol table.
+(defmacro (defcval name size)
+  `(def (,name loc (n NIL))
+     ,(str "Read the value at the location as a " size " value.\n\n"
+           "   Optionally read an array of n values\n\n"
+           "   The input to this function is as in @")
+     :doc-see @
+     :doc-group lsd
+     (if n (core-read (@ loc) ,size n)
+           (core-read (@ loc) ,size))))
 
-   Input is a string."
+(defcval c-byte 'utf8)
+(defcval c-short 's16)
+(defcval c-ushort 'u16)
+(defcval c-int 's32)
+(defcval c-uint 'u32)
+(defcval c-long 's32)
+(defcval c-ulong 'u32)
+(defcval c-vlong 's64)
+(defcval c-uvlong 'u64)
+(defcval c-ptr 'ptr)
+
+(def (c-str loc)
+  "Read the null-terminated string at the pointer location"
+  (def (go a)
+    (let {[v (core-read a 'utf8)]}
+      (if (= v (utf8 0))
+          ()
+          (cons v (go (1+ a))))))
+  (apply arr (cons 'utf8 (go (c-ptr (@ loc))))))
+
+(def (@ loc)
+  «Return the address of local or global data.
+
+   The input to this function can be:
+       1.  A string that refers to a local or global symbol of the program.
+       2.  A number which is the address itself,
+       3.  A symbol in which case the symbol's address is used.
+
+   Examples:»
   :doc-group lsd
-  (or (local-symbol s)
-      (global-symbol s)))
+  (def (symb-addr s)
+    (let {[type (symbol-type s)]}
+      (or (eq? type #\a)
+          (eq? type #\p)
+          (eq? type #\d)
+          (eq? type #\D)
+          (error "expected a data symbol" s))
+      (symbol-addr s)))
+  (def (str-addr s)
+    (let {[symb (or (local-symbol s)
+                    (global-symbol s :data T)
+                    (error "could not find symbol " s))]}
+      (symb-addr symb)))
+  (cond ((str? loc) (str-addr loc))
+        ((num? loc) (ptr loc))
+        ((symbol? loc) (symb-addr loc))
+        (else (error "str|num|symbol"))))
 
-(def (instr-addr loc)
+(def (pc loc)
   «Return the program instruction address corresponding to a location.
 
    A location can be:
@@ -167,8 +216,8 @@
 
    Examples:
 
-       (instr-addr "strecpy") → 2276985; look up a symbol
-       (instr-addr "/sys/src/cmd/ls.c:75") → 2097311; source code address»
+       (pc "strecpy") → #ptr(0x22be91); look up a symbol
+       (pc "/sys/src/cmd/ls.c:75") → #ptr(0x20009f); source code address»
   :doc-see filepc
   :doc-see symbol
   :doc-group lsd
@@ -195,7 +244,7 @@
   :doc-see bpdel
   :doc-group lsd
   (waitstop)
-  (let {[addr (instr-addr at)]}
+  (let {[addr (pc at)]}
     (when (has? bptbl addr)
           (error "breakpoint already set at " at))
     (put! bptbl addr (bpsave addr))
@@ -209,7 +258,7 @@
   :doc-see bpdel
   :doc-group lsd
   (waitstop)
-  (let {[addr (instr-addr at)]}
+  (let {[addr (pc at)]}
     (unless (has? bptbl addr)
             (error "breakpoint not set at " at))
     (core-write addr (get bptbl addr))
@@ -318,7 +367,7 @@
           (when on-bp (core-write addr bpinst))
           (step (1- n))))))
 
-(def (cont (:print T))
+(def (cont)
   "Continue program execution.
 
    Return the next instruction address to be executed or `void` if the
@@ -329,8 +378,7 @@
   (let {[note (startstop)]}
     (and print (not (void? note)) (princ note "\n")))
   (trycatch
-    (let {[pc (curPC)]}
-      (and print (princ pc "\n")))
+    (curPC)
     (λ (_) (void))))
 
 (def (func)
@@ -394,7 +442,7 @@
 
        (asm) ; print out 5 from current program instruction.
        (asm 10) ; print out 10 from current program instruction.
-       (asm 3 (instr-addr "strecpy")) ; 3 instructions from strecpy»
+       (asm 3 (pc "strecpy")) ; 3 instructions from strecpy»
   :doc-group lsd
   (for-each (λ (i) (princ (car i) "\t" (cdr i) "\n"))
             (asmlist n addr)))
@@ -438,9 +486,9 @@
 
    Examples:
 
-       (filepc "/sys/src/cmd/cat.c:5") → 2097192
-       (filepc "/sys/src/cmd/cat.c" 5) → 2097192
-       (src 2097192)              → "/sys/src/cmd/cat.c:5"»
+       (filepc "/sys/src/cmd/cat.c:5") → #ptr(0x200028)
+       (filepc "/sys/src/cmd/cat.c" 5) → #ptr(0x200028)
+       (src #ptr(0x200028))      → "/sys/src/cmd/cat.c:5"»
   :doc-group lsd
   (if line
       (lsd-file2pc f line)
@@ -458,7 +506,7 @@
 
    Examples:
 
-       (global-symbol "strecpy") → #(symbol "strecpy" #\T 2276784)»
+       (global-symbol "strecpy") → #(symbol "strecpy" #\T #ptr(0x22be91))»
   :doc-group lsd
   (def (find tbl k) (and (has? tbl k) (get tbl k)))
   (and (not text) (not data) (set! text T) (set! data T))
@@ -472,7 +520,7 @@
 
    Examples:
 
-       (local-symbol "i")    → #(symbol "i" #\a 140737488350940)
+       (local-symbol "i")    → #(symbol "i" #\a #ptr(0x7fffffffeec4))
        (symbol-read (local-symbol "argc") 's32) → #s32(2)»
   :doc-group lsd
   :doc-see _stk
--