[Git][cmucl/cmucl][issue-97-define-ud2-inst] 4 commits: Clean up changes.

Raymond Toy gitlab at common-lisp.net
Sat Mar 27 22:03:33 UTC 2021



Raymond Toy pushed to branch issue-97-define-ud2-inst at cmucl / cmucl


Commits:
b43c4c55 by Raymond Toy at 2021-03-27T11:21:18-07:00
Clean up changes.

- - - - -
6c9719ab by Raymond Toy at 2021-03-27T12:33:23-07:00
Reinstall breakpoint correctly.

Also remove some debugging #ifdefs and change debugging fprintfs to
DPRINTF.

More work needed.

- - - - -
fc557845 by Raymond Toy at 2021-03-27T14:44:08-07:00
Removing breakpoint stuff from sigill; more cleanups

For now, we get breakpoint trap in sigill, we lose.  This shouldn't
happen because the lisp code shouldn't be inserting breakpoints this
way.

Remove some #ifed code.

- - - - -
d039eb90 by Raymond Toy at 2021-03-27T15:03:12-07:00
Clean up implemenation, updating debugging prints

Change debugging prints to use DPRINTF.

- - - - -


3 changed files:

- src/code/x86-vm.lisp
- src/lisp/breakpoint.c
- src/lisp/x86-arch.c


Changes:

=====================================
src/code/x86-vm.lisp
=====================================
@@ -247,9 +247,9 @@
 	     (vector (make-array length :element-type '(unsigned-byte 8))))
 	(declare (type (unsigned-byte 8) length)
 		 (type (simple-array (unsigned-byte 8) (*)) vector))
-	#+t
+	#+nil
 	(format t "internal-error-args scp ~A: pc ~X len ~D~%" scp pc length)
-	;; Grab the bytes after length byte, which 
+	;; Grab the length bytes after the length byte.
 	(copy-from-system-area pc (* vm:byte-bits 4)
 			       vector (* vm:word-bits
 					 vm:vector-data-offset)


=====================================
src/lisp/breakpoint.c
=====================================
@@ -192,7 +192,9 @@ compute_offset(os_context_t * scp, lispobj code, boolean function_end)
 static int
 compute_offset(os_context_t * scp, lispobj code, boolean function_end)
 {
-    fprintf(stderr, "compute_offset: code = 0x%lx\n", code);
+    extern unsigned int debug_handlers;
+    
+    DPRINTF(debug_handlers, (stderr, "compute_offset: code = 0x%lx\n", code));
     
     if (code == NIL)
 	return 0;
@@ -209,16 +211,18 @@ compute_offset(os_context_t * scp, lispobj code, boolean function_end)
 	code_start = (unsigned long) codeptr
 	    + HeaderValue(codeptr->header) * sizeof(lispobj);
 
-        fprintf(stderr, "compute_offset: pc = 0x%lx, code_start = 0x%lx\n",
-                pc, code_start);
+        DPRINTF(debug_handlers,
+                (stderr, "compute_offset: pc = 0x%lx, code_start = 0x%lx\n",
+                 pc, code_start));
         
 	if (pc < code_start)
 	    return 0;
 	else {
 	    int offset = pc - code_start;
 
-            fprintf(stderr, "compute_offset: offset %d, size = %ld\n",
-                    offset, codeptr->code_size);
+            DPRINTF(debug_handlers,
+                    (stderr, "compute_offset: offset %d, size = %ld\n",
+                     offset, codeptr->code_size));
             
 	    if (offset >= codeptr->code_size) {
                 return 0;
@@ -253,17 +257,18 @@ handle_breakpoint(int signal, int subcode, os_context_t * scp)
 void
 handle_breakpoint(int signal, int subcode, os_context_t * scp)
 {
+    extern unsigned int debug_handlers;
+
     lispobj code, scp_sap = alloc_sap(scp);
 
     fake_foreign_function_call(scp);
 
     code = find_code(scp);
 
-#if 1
-    fprintf(stderr, "handle_breakpoint\n");
-    fprintf(stderr, " offset = %d\n", compute_offset(scp, code, 0));
-#endif    
-
+    DPRINTF(debug_handlers,
+            (stderr, "handle breakpoint: offset %d\n",
+             compute_offset(scp, code, 0)));
+    
     /*
      * Don't disallow recursive breakpoint traps.  Otherwise, we can't
      * use debugger breakpoints anywhere in here.


=====================================
src/lisp/x86-arch.c
=====================================
@@ -25,6 +25,13 @@
 
 unsigned long fast_random_state = 1;
 
+/*
+ * Set to positive value to enabled debug prints related to the sigill
+ * and sigtrap handlers.  Also enables prints related to handling of
+ * breakpoints.
+ */
+unsigned int debug_handlers = 0;
+
 #if defined(SOLARIS)
 /*
  * Use the /dev/cpu/self/cpuid interface on Solaris.  We could use the
@@ -140,7 +147,8 @@ arch_skip_instruction(os_context_t * context)
 {
     int vlen, code;
 
-    DPRINTF(1, (stderr, "[arch_skip_inst at %lx>]\n", SC_PC(context)));
+    DPRINTF(debug_handlers,
+            (stderr, "[arch_skip_inst at %lx>]\n", SC_PC(context)));
 
     /* Get and skip the lisp error code. */
     char* pc = (char *) SC_PC(context);
@@ -178,7 +186,8 @@ arch_skip_instruction(os_context_t * context)
 	  break;
     }
 
-    DPRINTF(0, (stderr, "[arch_skip_inst resuming at %lx>]\n", SC_PC(context)));
+    DPRINTF(debug_handlers,
+            (stderr, "[arch_skip_inst resuming at %lx>]\n", SC_PC(context)));
 }
 
 unsigned char *
@@ -207,28 +216,20 @@ arch_install_breakpoint(void *pc)
     unsigned char* ptr = (unsigned char *) pc;
     unsigned long result = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
 
-    fprintf(stderr, "arch_install_breakpoint at %p, old code = 0x%lx\n",
-            pc, result);
-    
-#if 1
-    *(char *) pc = BREAKPOINT_INST;	/* x86 INT3       */
-#if 0
-    *((char *) pc + 1) = trap_Breakpoint;	/* Lisp trap code */
-#endif
-#else
-    *ptr++ = 0x0f;              /* UD2 */
-    *ptr++ = 0x0b;
-    *ptr++ = trap_Breakpoint;   /* Lisp trap code */
-#endif
+    DPRINTF(debug_handlers,
+            (stderr, "arch_install_breakpoint at %p, old code = 0x%lx\n",
+             pc, result));
 
+    *(char *) pc = BREAKPOINT_INST;	/* x86 INT3       */
     return result;
 }
 
 void
 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
 {
-    fprintf(stderr, "arch_remove_breakpoint: %p orig %lx\n",
-            pc, orig_inst);
+    DPRINTF(debug_handlers,
+            (stderr, "arch_remove_breakpoint: %p orig %lx\n",
+             pc, orig_inst));
     unsigned char *ptr = (unsigned char *) pc;
     ptr[0] = orig_inst & 0xff;
     ptr[1] = (orig_inst >> 8) & 0xff;
@@ -256,23 +257,15 @@ arch_do_displaced_inst(os_context_t * context, unsigned long orig_inst)
 {
     unsigned char *pc = (unsigned char *) SC_PC(context);
 
-    fprintf(stderr, "arch_do_displaced_inst: pc %p orig_inst %lx\n",
-            pc, orig_inst);
+    DPRINTF(debug_handlers,
+            (stderr, "arch_do_displaced_inst: pc %p orig_inst %lx\n",
+             pc, orig_inst));
     
     /*
      * Put the original instruction back.
      */
 
-#if 1
     *((char *) pc) = orig_inst & 0xff;
-#if 0
-    *((char *) pc + 1) = (orig_inst & 0xff00) >> 8;
-#endif
-#else
-    pc[0] = orig_inst & 0xff;
-    pc[1] = (orig_inst >> 8) & 0xff;
-    pc[2] = (orig_inst >> 16) & 0xff;
-#endif
 
 #ifdef SC_EFLAGS
     /* Enable single-stepping */
@@ -317,71 +310,25 @@ sigill_handler(HANDLER_ARGS)
 {
     unsigned int trap;
     os_context_t* os_context = (os_context_t *) context;
-#if 1
-#if 0
-    fprintf(stderr, "x86sigtrap: %8x %x\n",
-            SC_PC(os_os_context), *(unsigned char *) (SC_PC(os_context) - 1));
-#else
-    fprintf(stderr,"x86sigill: fp=%lx sp=%lx pc=%lx { %x, %x, %x, %x, %x }\n",
-            SC_REG(context, reg_FP),
-            SC_REG(context, reg_SP),
-            SC_PC(context),
-            *(unsigned char*)(SC_PC(context) + 0), /* 0x0F */
-            *(unsigned char*)(SC_PC(context) + 1), /* 0x0B */
-            *(unsigned char*)(SC_PC(context) + 2),
-            *(unsigned char*)(SC_PC(context) + 3),
-            *(unsigned char*)(SC_PC(context) + 4));
-#endif    
-    fprintf(stderr, "sigtrap(%d %d %p)\n", signal, CODE(code), os_context);
-#endif
 
-#if 0
-    if (single_stepping && (signal == SIGTRAP)) {
-#if 1
-	fprintf(stderr, "* Single step trap %p\n", single_stepping);
-#endif
-
-#ifdef SC_EFLAGS
-	/* Disable single-stepping */
-	SC_EFLAGS(os_context) ^= 0x100;
-#else
-	/* Un-install single step helper instructions. */
-	*(single_stepping - 3) = single_step_save1;
-	*(single_stepping - 2) = single_step_save2;
-	*(single_stepping - 1) = single_step_save3;
-        DPRINTF(0, (stderr, "Uninstalling helper instructions\n"));
-#endif
-
-	/*
-	 * Re-install the breakpoint if possible.
-	 */
-        fprintf(stderr, "* Maybe reinstall breakpoint for pc %p with single_stepping %p\n",
-                (void*) SC_PC(os_context), single_stepping);
-        
-	if ((int) SC_PC(os_context) < (int) single_stepping + 3)
-	    fprintf(stderr, "* Breakpoint not re-install\n");
-	else {
-	    char *ptr = (char *) single_stepping;
-
-#if 0
-	    ptr[0] = BREAKPOINT_INST;	/* x86 INT3 */
-	    ptr[1] = trap_Breakpoint;
-#else
-            ptr[0] = 0x0f;
-            ptr[1] = 0x0b;
-            ptr[2] = trap_Breakpoint;
-#endif            
-	}
-
-	single_stepping = NULL;
-	return;
+    DPRINTF(debug_handlers,
+            (stderr,"sigill: fp=%lx sp=%lx pc=%lx { %x, %x, %x, %x, %x }\n",
+             SC_REG(context, reg_FP),
+             SC_REG(context, reg_SP),
+             SC_PC(context),
+             *((unsigned char*)SC_PC(context) + 0), /* 0x0F */
+             *((unsigned char*)SC_PC(context) + 1), /* 0x0B */
+             *((unsigned char*)SC_PC(context) + 2),
+             *((unsigned char*)SC_PC(context) + 3),
+             *((unsigned char*)SC_PC(context) + 4)));
+
+    if (single_stepping) {
+        lose("sigill handler with single-stepping enabled?\n");
     }
-#endif
     
     /* This is just for info in case monitor wants to print an approx */
     current_control_stack_pointer = (unsigned long *) SC_SP(os_context);
 
-
     /*
      * In many places in the switch below, we eventually throw instead
      * of returning from the signal handler.  So, just in case, set
@@ -396,22 +343,20 @@ sigill_handler(HANDLER_ARGS)
      * arguments to follow.
      */
 
-#if 1
-    fprintf(stderr, "pc %x\n",  *(unsigned short *)SC_PC(context));
-#endif    
+    DPRINTF(debug_handlers,
+            (stderr, "pc %x\n",  *(unsigned short *)SC_PC(context)));
+
     if (*(unsigned short *) SC_PC(context) == 0x0b0f) {
         trap = *(((char *)SC_PC(context)) + 2);
     } else {
         abort();
     }
 
-#if 1
-    fprintf(stderr, "code = %x\n", trap);
-#endif
+    DPRINTF(debug_handlers, (stderr, "code = %x\n", trap));
 
     switch (trap) {
       case trap_PendingInterrupt:
-	  DPRINTF(1, (stderr, "<trap Pending Interrupt.>\n"));
+	  DPRINTF(debug_handlers, (stderr, "<trap Pending Interrupt.>\n"));
 	  arch_skip_instruction(os_context);
 	  interrupt_handle_pending(os_context);
 	  break;
@@ -432,28 +377,15 @@ sigill_handler(HANDLER_ARGS)
 
       case trap_Error:
       case trap_Cerror:
-	  DPRINTF(1, (stderr, "<trap Error %x>\n", CODE(code)));
+	  DPRINTF(debug_handlers, (stderr, "<trap Error %x>\n", CODE(code)));
 	  interrupt_internal_error(signal, code, os_context, CODE(code) == trap_Cerror);
 	  break;
 
       case trap_Breakpoint:
-#if 1
-	  fprintf(stderr, "*C break\n");
-#endif
-#if 0
-	  SC_PC(os_context) -= 1;
-#endif          
-
-	  handle_breakpoint(signal, CODE(code), os_context);
-#if 1
-	  fprintf(stderr, "*C break return\n");
-#endif
+          lose("Unexpected breakpoint trap in sigill-hander.\n");
 	  break;
 
       case trap_FunctionEndBreakpoint:
-#if 0
-	  SC_PC(os_context) -= 1;
-#endif          
 	  SC_PC(os_context) =
 	      (int) handle_function_end_breakpoint(signal, CODE(code), os_context);
 	  break;
@@ -473,7 +405,7 @@ sigill_handler(HANDLER_ARGS)
 	  break;
 #endif
       default:
-	  DPRINTF(1,
+	  DPRINTF(debug_handlers,
 		  (stderr, "[C--trap default %d %d %p]\n", signal, CODE(code),
 		   os_context));
 	  interrupt_handle_now(signal, code, os_context);
@@ -486,21 +418,19 @@ sigtrap_handler(HANDLER_ARGS)
 {
     os_context_t* os_context = (os_context_t *) context;
 
-#if 1
-    fprintf(stderr,"sigtrap: fp=%lx sp=%lx pc=%lx { %x, %x, %x, %x, %x }\n",
-            SC_REG(context, reg_FP),
-            SC_REG(context, reg_SP),
-            SC_PC(context),
-            *(unsigned char*)(SC_PC(context) + 0), /* 0x0F */
-            *(unsigned char*)(SC_PC(context) + 1), /* 0x0B */
-            *(unsigned char*)(SC_PC(context) + 2),
-            *(unsigned char*)(SC_PC(context) + 3),
-            *(unsigned char*)(SC_PC(context) + 4));
-#endif    
+    DPRINTF(debug_handlers,
+            (stderr,"sigtrap: fp=%lx sp=%lx pc=%lx { %x, %x, %x, %x, %x }\n",
+             SC_REG(context, reg_FP),
+             SC_REG(context, reg_SP),
+             SC_PC(context),
+             *((unsigned char*)SC_PC(context) + 0), /* 0x0F */
+             *((unsigned char*)SC_PC(context) + 1), /* 0x0B */
+             *((unsigned char*)SC_PC(context) + 2),
+             *((unsigned char*)SC_PC(context) + 3),
+             *(unsigned char*)(SC_PC(context) + 4)));
+
     if (single_stepping && (signal == SIGTRAP)) {
-#if 1
-	fprintf(stderr, "* Single step trap %p\n", single_stepping);
-#endif
+	DPRINTF(debug_handlers, (stderr, "* Single step trap %p\n", single_stepping));
 
 #ifdef SC_EFLAGS
 	/* Disable single-stepping */
@@ -516,38 +446,29 @@ sigtrap_handler(HANDLER_ARGS)
 	/*
 	 * Re-install the breakpoint if possible.
 	 */
-        fprintf(stderr, "* Maybe reinstall breakpoint for pc %p with single_stepping %p\n",
-                (void*) SC_PC(os_context), single_stepping);
+        DPRINTF(debug_handlers,
+                (stderr, "* Maybe reinstall breakpoint for pc %p with single_stepping %p\n",
+                 (void*) SC_PC(os_context), single_stepping));
         
 	if ((unsigned long) SC_PC(os_context) <= (unsigned long) single_stepping)
 	    fprintf(stderr, "* Breakpoint not re-install\n");
 	else {
 	    char *ptr = (char *) single_stepping;
 
-#if 0
 	    ptr[0] = BREAKPOINT_INST;	/* x86 INT3 */
-	    ptr[1] = trap_Breakpoint;
-#else
-            ptr[0] = 0x0f;
-            ptr[1] = 0x0b;
-            ptr[2] = trap_Breakpoint;
-#endif            
 	}
 
 	single_stepping = NULL;
 	return;
     }
-#if 1
-	  fprintf(stderr, "*C break\n");
-#endif
-#if 1
-	  SC_PC(os_context) -= 1;
-#endif          
 
-	  handle_breakpoint(signal, CODE(code), os_context);
-#if 1
-	  fprintf(stderr, "*C break return\n");
-#endif
+    DPRINTF(debug_handlers, (stderr, "*C break\n"));
+
+    SC_PC(os_context) -= 1;
+
+    handle_breakpoint(signal, CODE(code), os_context);
+
+    DPRINTF(debug_handlers, (stderr, "*C break return\n"));
 }
 
 



View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/21dd39ea7b8e5bf144ee67f06d246dc69a6d2b8b...d039eb90c6e86457c946c9f7d75c6a4c14ab4d78

-- 
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/21dd39ea7b8e5bf144ee67f06d246dc69a6d2b8b...d039eb90c6e86457c946c9f7d75c6a4c14ab4d78
You're receiving this email because of your account on gitlab.common-lisp.net.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cmucl-cvs/attachments/20210327/5f2bc417/attachment-0001.html>


More information about the cmucl-cvs mailing list