shithub: sl

Download patch

ref: bca20e2c9f16df143d580a6e03634d926b582aa4
parent: 5822146e55b3aff8396e434c8724650251b97dfb
author: spew <spew@cbza.org>
date: Tue Mar 18 14:47:09 EDT 2025

lsd: use pairs, add asmlist, fix documentation, simplify asm

--- a/src/plan9/lsd.c
+++ b/src/plan9/lsd.c
@@ -371,9 +371,8 @@
 	if(size < 0)
 		lerrorf(sl_errio, "could not get instruction size at %ud", addr);
 
-	r = sl_nil;
+	r = str_from_cstr(buf);
 	sl_gc_handle(&r);
-	r = mk_cons(str_from_cstr(buf), r);
 	r = mk_cons(size_wrap(size), r);
 	sl_free_gc_handles(1);
 	return r;
--- a/src/plan9/lsd.lsp
+++ b/src/plan9/lsd.lsp
@@ -111,8 +111,8 @@
    to place the break.
 
    Examples:
-     (bpset 'strcpy) ; breakpoint on strcpy function.
-     (bpset (readreg PC)) ; breakpoint on current instruction.")
+     `(bpset 'strcpy)` ; breakpoint on strcpy function.
+     `(bpset (readreg PC))` ; breakpoint on current instruction.")
 
 (doc-for (bpdel loc)
   "Delete a breakpoint.
@@ -123,8 +123,8 @@
    to remove the break.
 
    Examples:
-     (bpdel 'strcpy) ; remove breakpoint on strcpy function.
-     (bpdel (readreg PC)) ; remove breakpoint on current instruction.")
+     `(bpdel 'strcpy)` ; remove breakpoint on strcpy function.
+     `(bpdel (readreg PC))` ; remove breakpoint on current instruction.")
 
 (def (detach)
   (if regsf (io-close regsf))
@@ -180,8 +180,8 @@
 (def (curPC) (if (>= pid 0) (readreg PC)))
 
 (def (step (n 1))
-  "Step n assembly instructions. Returns the next instruction
-   address to be executed or NIL if the program has exited."
+  "Step `n` assembly instructions. Returns the next instruction
+   address to be executed or `NIL` if the program has exited."
   (if (= n 0)
       (curPC)
       (let* ((addr (readreg PC))
@@ -197,40 +197,46 @@
 
 (def (cont)
   "Continue program execution. Returns the next instruction
-   address to be executed or NIL if the program has exited."
+   address to be executed or `NIL` if the program has exited."
   (let ((addr (readreg PC)))
     (if (has? bptbl addr) (step))
     (startstop)
     (curPC)))
 
-(def (asm (n 5) (addr (readreg PC)))
-  "Print the next n disassembled instructions at addr.
+(def (asmlist (n 5) (addr (readreg PC)))
+  "Returns a list of the next `n` disassembled instructions starting at `addr`.
 
-   This returns the following instruction address so it can be called again.
+   Each element in the list has the form `(address . instr)` where `instr`
+   is the disassembled instruction at the `address`.
 
-   Examples:
-     (asm) ; print out 5 from current program instruction.
-     (asm 10 (readreg PC)) ; print out 10 from current program instruction.
-     (asm 5 (step)) ; step and then print out 5.
-     (asm 3 (asm)) ; print 3 more."
+   Examples: just like `(asm)` but returns a list instead of printing."
   (if (<= n 0)
-      addr
+      ()
       (let ((on-bp (has? bptbl addr)))
         (if on-bp (writecore addr (get bptbl addr)))
         (let* ((a (lsd-asm addr))
                (next (car a))
-               (instr (cadr a)))
-          (princ instr "\n")
+               (instr (cdr a)))
           (if on-bp (writecore addr bpinst))
-          (asm (1- n) (+ addr next))))))
+          (cons (cons addr instr) (asmlist (1- n) (+ addr next)))))))
 
+(def (asm (n 5) (addr (readreg PC)))
+  "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 5 (step))` ; step and then print out 5."
+  (for-each (λ (i) (princ (car i) "\t" (cdr i) "\n"))
+            (asmlist n addr)))
+
 (def (src (addr (readreg PC)))
   "Returns a string of the filename and line number corresponding
    to the instruction address.
 
    Examples:
-     (src)
-     (src (asm 10)) ; read 10 instructions forward first."
+     `(src)`
+     `(src (asm 10))` ; read 10 instructions forward first."
   (lsd-fileline addr))
 
 (def (Bsrc (addr (readreg PC)))
@@ -238,7 +244,7 @@
    the source code corresponding to the instruction address
    can be viewed in your text editor.
 
-   See the examples for (src), this works the same."
+   See the examples for `(src)`, this works the same."
   (let ((s (src addr))
         (plumbf (file "/mnt/plumb/send" :write)))
     (io-write plumbf