shithub: sl

Download patch

ref: 98c4ced1fc5d1eca1db3137124e805d988bb990f
parent: 5c857048a92d2ec8219bc77b8e855dba95ce31b3
author: spew <spew@cbza.org>
date: Mon Mar 17 20:27:53 EDT 2025

lsd: add disassmbly, fix various things

--- a/src/plan9/lsd.c
+++ b/src/plan9/lsd.c
@@ -353,6 +353,21 @@
 	return foll;
 }
 
+BUILTIN("lsd-asm", lsd_asm)
+{
+	static char buf[512];
+	uvlong addr;
+
+	argcount(nargs, 1);
+	if(sl_unlikely(!sl_isnum(args[0])))
+		type_error("num", args[0]);
+	addr = tosize(args[0]);
+
+	if ((*machdata->das)(coremap, addr, 'i', buf, sizeof(buf)) < 0)
+		lerrorf(sl_errio, "could not disassemble at %ud", addr);
+	return str_from_cstr(buf);
+}
+
 BUILTIN("lsd-ctrace", lsd_ctrace)
 {
 	sl_v *a;
--- a/src/plan9/lsd.lsp
+++ b/src/plan9/lsd.lsp
@@ -14,6 +14,7 @@
 (def bptbl (table))
 
 (def (procfile s . flags)
+  (if (< pid 0) (error "no active pid"))
   (let ((path (str "/proc/" pid "/" s)))
     (apply file (cons path flags))))
 
@@ -22,14 +23,27 @@
     (io-write ctlf msg)
     (io-close ctlf)))
 
-(def (clearnote)
-  (let ((notef (procfile 'note :read)))
-    (io-readall notef)
-    (io-close notef)))
+(def (exited)
+  (if (< pid 0) (error "no active pid"))
+  (princ "process " pid " exited\n")
+  (set! pids (cdr pids))
+  (set! pid (if pids (car pids) -1))
+  (detach))
 
+(def (readnote)
+  (trycatch
+    (let* ((notef (procfile 'note :read))
+      	   (note (io-readall notef)))
+      (io-close notef)
+      note)
+    (λ (e) (if (and (eq? (car e) 'io-error)
+                    (= (str-find (cadr e) "could not open") 0))
+               (exited)
+               (raise e)))))
+
 (def (start) (writectl "start"))
-(def (startstop) (writectl "startstop") (clearnote))
-(def (stop) (writectl "stop") (clearnote))
+(def (startstop) (writectl "startstop") (readnote))
+(def (stop) (writectl "stop") (readnote))
 
 (def (follow addr) (reverse (lsd-follow addr)))
 
@@ -115,7 +129,7 @@
     (set! pids (cons pid pids))
     pid))
 
-(def (load a)
+(def (lsd a)
   (let* ((v (lsd-load a))
          (f (λ (symbol tbl) (put! tbl (sym (symbol-name symbol)) symbol)))
          (text (foldl f (table) (aref v 3)))
@@ -138,6 +152,8 @@
 
 (def _stk (get tracers (os-getenv "objtype")))
 
+(def (curPC) (if (>= pid 0) (readreg PC)))
+
 (def (step)
   (let* ((addr (readreg PC))
          (on-bp (has? bptbl addr)))
@@ -148,13 +164,21 @@
       (startstop)
       (map writecore f o)
       (if on-bp (writecore addr bpinst))
-      (readreg PC))))
+      (or (curPC) (void)))))
 
 (def (cont)
   (let ((addr (readreg PC)))
     (if (has? bptbl addr) (step))
-    (startstop)))
+    (startstop)
+    (or (curPC) (void))))
 
+(def (asm addr)
+  (let ((on-bp (has? bptbl addr)))
+    (if on-bp (writecore addr (get bptbl addr)))
+    (princ (lsd-asm addr) "\n")
+    (if on-bp (writecore addr bpinst))
+    (void)))
+
 (def (at-exit s)
   (if proc-stdin (io-close proc-stdin))
   (detach)
@@ -165,6 +189,6 @@
 
 (let* ((proc (cadr *argv*))
        (pid (str->num proc)))
-  (if pid (load pid) (load proc)))
+  (if pid (lsd pid) (lsd proc)))
 
 (repl)