shithub: sl

Download patch

ref: 9ecc53223e7d13369c3d78ff1003e46bde6d953f
parent: edd468e09a359a3e056e9ad727b0f6dcff122b0e
author: spew <spew@cbza.org>
date: Wed Apr 9 00:11:27 EDT 2025

lsd: allow lsd to be loaded in a repl with (load "lsd.sl"), finish ptr conversion, other cleanups and renamings

--- a/src/plan9/lsd.c
+++ b/src/plan9/lsd.c
@@ -228,8 +228,8 @@
 	sl_gc_handle(&v);
 	vec_elt(v, 0) = lsd_framesym;
 	vec_elt(v, 1) = mk_symbol(fn);
-	vec_elt(v, 2) = mk_u64(retpc);
-	vec_elt(v, 3) = mk_u64(sp);
+	vec_elt(v, 2) = mk_ptr(retpc);
+	vec_elt(v, 3) = mk_ptr(sp);
 	vec_elt(v, 4) = localslist(fn, sp);
 	sl_free_gc_handles(1);
 	tracelist = mk_cons(v, tracelist);
@@ -375,7 +375,7 @@
 	foll = sl_nil;
 	sl_gc_handle(&foll);
 	for(p = f; p < f+n; p++)
-		foll = mk_cons(mk_u64(*p), foll);
+		foll = mk_cons(mk_ptr(*p), foll);
 	sl_free_gc_handles(1);
 	return foll;
 }
--- a/src/plan9/lsd.sl
+++ b/src/plan9/lsd.sl
@@ -145,99 +145,75 @@
   :doc-see global-symbol
   `(core-read (symbol-addr ,symbol) ,.rest))
 
-(def (str->symbol s)
+(def (symbol-find s)
   "Return a symbol, searching first in the local stack frame then
    in the global symbol table.
 
    Input is a string."
-  :doc-see str->addr
   :doc-group lsd
   (or (local-symbol s)
       (global-symbol s)))
 
-(def (str->addr s (:str->symbol str->symbol))
-  «Return the address corresponding either to a source code line or
-   symbol.
+(def (instr-addr loc)
+  «Return the program instruction address corresponding to a location.
 
-   Input is a string.  By default str->symbol is used to lookup
-   symbols in the local stack frame and then the global text and data
-   symbol tables.
+   A location can be:
+       1.  A string that refers to a symbol (such as a function) in
+           the process's symbol table,
+       2.  A string of the form "file:line" used to find a program
+           address corresponding to given line of source code,
+       3.  A number which is the address itself,
+       4.  A symbol in which case the symbol's address is used.
 
    Examples:
 
-       (str->addr "strecpy") → 2276985; look up a symbol
-       (str->addr "/sys/src/cmd/ls.c:75") → 2097311; source code address»
-  :doc-see loc->addr
-  :doc-see str->symbol
+       (instr-addr "strecpy") → 2276985; look up a symbol
+       (instr-addr "/sys/src/cmd/ls.c:75") → 2097311; source code address»
   :doc-see filepc
+  :doc-see symbol
   :doc-group lsd
-  (trycatch
-    (filepc s)
-    (λ (e) (when (eq? (car e) 'io-error) (raise e))
-           (let {[symb (str->symbol s)]}
-             (if symb
-                 (symbol-addr symb)
-                 (error "could not find symbol " s))))))
-
-(def (loc->addr loc (:str->symbol str->symbol))
-  «Return the address of a location.
-
-   A location refers to either
-       1.  A string that refers to a symbol in the process's
-           symbol table or call stack,
-       2.  A string of the form "file:line" used to look up
-           an instruction address corresponding to source code,
-       3.  A number which is an address in the process's address space,
-       4.  A symbol in which case the symbol's address is used.
-
-   By default str->symbol is used to find the corresponding symbol
-   which searches the local call stack then global text symbols then
-   global data symbols.»
-  :doc-see str->addr
-  (cond ((str? loc) (str->addr loc :str->symbol str->symbol))
+  (def (str-addr s)
+    (trycatch
+      (filepc s)
+      (λ (e) (when (eq? (car e) 'io-error) (raise e))
+             (let {[symb (global-symbol s :text T)]}
+               (if symb
+                   (symbol-addr symb)
+                   (error "could not find symbol " s))))))
+  (cond ((str? loc) (str-addr loc))
         ((num? loc) (ptr loc))
         ((symbol? loc) (symbol-addr loc))
         (else (error "str|num|symbol"))))
 
-(def (hex n)
-  "Display an integer in hex format."
-  :doc-group lsd
-  (str "0x" (num->str n 16)))
-
-(def (oct n)
-  "Display an integer in octal format."
-  :doc-group lsd
-  (str "0" (num->str n 8)))
-
 (def (bpsave a) (core-read a 'u8 (length bpinst)))
 
-(def (bpset loc)
+(def (bpset at)
   "Set a breakpoint at the location.
 
-   Location is as in loc->addr"
-  :doc-see loc->addr
+   Location is as in instr"
+  :doc-see instr
   :doc-see bpdel
   :doc-group lsd
   (def (txtsymb s) (global-symbol s :text T))
   (waitstop)
-  (let {[addr (loc->addr loc :str->symbol txtsymb)]}
+  (let {[addr (instr-addr at)]}
     (when (has? bptbl addr)
-          (error "breakpoint already set at " loc))
+          (error "breakpoint already set at " at))
     (put! bptbl addr (bpsave addr))
     (core-write addr bpinst)))
 
-(def (bpdel loc)
+(def (bpdel at)
   "Delete a breakpoint at the location.
 
-   Location is as in loc->addr"
-  :doc-see loc->addr
+   Location is as in instr"
+  :doc-see instr
   :doc-see bpdel
   :doc-group lsd
   (def (txtsymb s) (global-symbol s :text T))
   (waitstop)
-  (let {[addr (loc->addr loc :str->symbol txtsymb)]}
+  (let {[addr (instr-addr at)]}
     (unless (has? bptbl addr)
-            (error "breakpoint not set at " loc))
+            (error "breakpoint not set at " at))
     (core-write addr (get bptbl addr))
     (del! bptbl addr)))
 
@@ -294,16 +270,16 @@
           (princ
             (str-join
               (map (λ (p)
-                      (str (symbol-name p) "=" (hex (symbol-read p 'u64))))
+                      (str (symbol-name p) "=" (symbol-read p 'ptr)))
                    params)
               ", "))
-          (princ ")+" (hex (- pc (symbol-addr fsym))) " ")
+          (princ ")+" (ptr (- pc (symbol-addr fsym))) " ")
           (princ (src pc) "\n")
           (when locals
             (for-each (λ (l) (princ "	"
                                     (symbol-name l)
                                     "="
-                                    (hex (symbol-read l 'u64))
+                                    (symbol-read l 'ptr)
                                     "\n"))
                       autos))
           (go (frame-retpc f) (cdr fs))))))
@@ -311,7 +287,7 @@
          [topsym (lsd-findsym tos)]}
     (princ (symbol-name topsym)
            "+"
-           (hex (- tos (symbol-addr topsym)))
+           (ptr (- tos (symbol-addr topsym)))
            " "
            (src tos)
            "\n")))
@@ -324,7 +300,7 @@
   (stk :locals T :level level :n n))
 
 (def (curPC) (if (>= pid 0)
-                 (reg-read PC)
+                 (ptr (reg-read PC))
                  (error "No active process")))
 
 (def (step (n 1))
@@ -356,7 +332,7 @@
     (and print (not (void? note)) (princ note "\n")))
   (trycatch
     (let {[pc (curPC)]}
-      (and print pc (princ (hex pc) "\n")))
+      (and print (princ pc "\n")))
     (λ (_) (void))))
 
 (def (func)
@@ -411,18 +387,18 @@
         (let {[instr (lsd-das addr)]
               [isize (lsd-instsize addr)]}
           (when on-bp (core-write addr bpinst))
-          (cons (cons addr instr) (asmlist (1- n) (+ addr isize)))))))
+          (cons (cons (ptr addr) instr) (asmlist (1- n) (+ addr isize)))))))
 
 (def (asm (n 5) (addr (reg-read PC)))
-  "Print the next `n` disassembled instructions at `addr`.
+  «Print the next `n` disassembled instructions at `addr`.
 
    Examples:
 
        (asm) ; print out 5 from current program instruction.
        (asm 10) ; print out 10 from current program instruction.
-       (asm 3 (str->addr "strecpy")) ; 3 instructions from strecpy"
+       (asm 3 (instr-addr "strecpy")) ; 3 instructions from strecpy»
   :doc-group lsd
-  (for-each (λ (i) (princ (hex (car i)) "\t" (cdr i) "\n"))
+  (for-each (λ (i) (princ (car i) "\t" (cdr i) "\n"))
             (asmlist n addr)))
 
 (def (src (addr (curPC)))
@@ -548,17 +524,17 @@
     (set! registers (reverse! (aref v 1)))
     (set! bpinst (aref v 2))
     (set! globals (make-global :text text :data data)))
-  (and (>= pid 0) (attach)))
+  (if (>= pid 0) (attach) (void)))
 
 (def (usage)
   (princ "usage: " (car *argv*) " [pid | textfile]\n")
   (exit))
 
-(unless (cdr *argv*) (usage))
-
-(let* {[proc (cadr *argv*)]
-       [pid (str->num proc)]}
-  (if pid (lsd pid) (lsd proc)))
+(when (str-find (car *argv*) "lsd")
+  (unless (cdr *argv*) (usage))
+  (let* {[proc (cadr *argv*)]
+         [pid (str->num proc)]}
+    (if pid (lsd pid) (lsd proc))))
 
 (set! top-level-exception-handler
       (λ (e) (with-output-to *stderr*