Raymond Toy pushed to branch issue-97-define-ud2-inst at cmucl / cmucl
Commits:
62d67c1e by Raymond Toy at 2021-04-10T09:24:32-07:00
More cleanups
When installing a breakpoint, we only need to save the single byte.
When removing a breakpoint, we only need to restore the single byte.
- - - - -
1 changed file:
- src/lisp/x86-arch.c
Changes:
=====================================
src/lisp/x86-arch.c
=====================================
@@ -213,17 +213,21 @@ arch_set_pseudo_atomic_interrupted(os_context_t * context)
+/*
+ * Installs a breakpoint (INT3) at |pc|. We return the byte that was
+ * replaced by the int3 instruction.
+ */
unsigned long
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);
+ unsigned long result = *ptr;
DPRINTF(debug_handlers,
(stderr, "arch_install_breakpoint at %p, old code = 0x%lx\n",
pc, result));
- *(char *) pc = BREAKPOINT_INST; /* x86 INT3 */
+ *ptr = BREAKPOINT_INST; /* x86 INT3 */
return result;
}
@@ -235,14 +239,9 @@ arch_remove_breakpoint(void *pc, unsigned long orig_inst)
pc, orig_inst));
unsigned char *ptr = (unsigned char *) pc;
/*
- * Just restore all the bytes from orig_inst. Should we just
- * re-install just the one byte that was taken by the int3
- * instruction?
+ * Just restore the byte from orig_inst.
*/
ptr[0] = orig_inst & 0xff;
- ptr[1] = (orig_inst >> 8) & 0xff;
- ptr[2] = (orig_inst >> 16) & 0xff;
- ptr[3] = (orig_inst >> 24) & 0xff;
}
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/62d67c1ed634686a0bc1ac5…
--
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/62d67c1ed634686a0bc1ac5…
You're receiving this email because of your account on gitlab.common-lisp.net.
Raymond Toy pushed to branch issue-97-define-ud2-inst at cmucl / cmucl
Commits:
9a86d9f0 by Raymond Toy at 2021-04-10T08:42:04-07:00
Change disassembly note for trap codes
Minor tweak to print the trap number first before the description of
the trap.
- - - - -
9b09c6d9 by Raymond Toy at 2021-04-10T08:50:17-07:00
Add comments
- - - - -
46620f56 by Raymond Toy at 2021-04-10T09:15:38-07:00
Address review comments and add more comments
Fixed up a few things mentioned in the review.
Add a more comments on how things work, and added a TODO on computing
the instruction length and inserting a int3/ud1 after it, kind of like
how other archs handle it.
- - - - -
2 changed files:
- src/compiler/x86/insts.lisp
- src/lisp/x86-arch.c
Changes:
=====================================
src/compiler/x86/insts.lisp
=====================================
@@ -2110,32 +2110,39 @@
(defun ud1-control (chunk inst stream dstate)
(declare (ignore inst))
(flet ((nt (x) (if stream (disassem:note x dstate))))
- (case (ldb (byte 6 16) chunk)
- (#.vm:error-trap
- (nt #.(format nil "Error trap: ~D" vm:error-trap))
- (disassem:handle-break-args #'snarf-error-junk stream dstate))
- (#.vm:cerror-trap
- (nt #.(format nil "Cerror trap: ~D" vm:cerror-trap))
- (disassem:handle-break-args #'snarf-error-junk stream dstate))
- (#.vm:breakpoint-trap
- (nt #.(format nil "Breakpoint trap: ~D" vm:breakpoint-trap)))
- (#.vm:pending-interrupt-trap
- (nt #.(format nil "Pending interrupt trap: ~D" vm:pending-interrupt-trap)))
- (#.vm:halt-trap
- (nt #.(format nil "Halt trap: ~D" vm:halt-trap)))
- (#.vm:function-end-breakpoint-trap
- (nt #.(format nil "Function end breakpoint trap: ~D" vm:function-end-breakpoint-trap)))
- )))
+ (let ((code (ldb (byte 6 16) chunk)))
+ (case code
+ (#.vm:error-trap
+ (nt #.(format nil "Trap ~D: Error trap" vm:error-trap))
+ (disassem:handle-break-args #'snarf-error-junk stream dstate))
+ (#.vm:cerror-trap
+ (nt #.(format nil "Trap ~D: Cerror trap" vm:cerror-trap))
+ (disassem:handle-break-args #'snarf-error-junk stream dstate))
+ (#.vm:pending-interrupt-trap
+ (nt #.(format nil "Trap ~D: Pending interrupt trap" vm:pending-interrupt-trap)))
+ (#.vm:halt-trap
+ (nt #.(format nil "Trap ~D: Halt trap" vm:halt-trap)))
+ (#.vm:function-end-breakpoint-trap
+ (nt #.(format nil "Trap ~D: Function end breakpoint trap"
+ vm:function-end-breakpoint-trap)))
+ (t
+ (nt #.(format nil "Trap ~D: Unexpected trap type!!!!" )))))))
;; The ud1 instruction where we smash the code (trap type) into the
-;; mod r/m byte. We don't care about what that actually encodes to.
-;; We just want the trap code in the third byte of the instruction.
+;; low 6 bits of the mod r/m byte. The mod bits are set to #b11 to
+;; make sure the reg/mem part is interpreted to be a register and not
+;; memory.
(define-instruction ud1 (segment code)
(:declare (type (unsigned-byte 8) code))
(:printer ud1 ((op #b10111001) (reg nil :type 'word-reg))
:default
:control #'ud1-control)
(:emitter
+ ;; We should not be using the breakpoint trap with UD1 anymore.
+ ;; Breakpoint traps are handled in C now, using plain int3.
+ (assert (/= code vm:breakpoint-trap))
+
+ ;; Emit the bytes of the instruction.
(emit-byte segment #x0f)
(emit-byte segment #xb9)
(emit-mod-reg-r/m-byte segment
=====================================
src/lisp/x86-arch.c
=====================================
@@ -260,6 +260,23 @@ unsigned int single_step_save2;
unsigned int single_step_save3;
#endif
+/*
+ * This is called when we need to continue after a breakpoint. This
+ * works by putting the original byte back into the code, and then
+ * enabling single-step mode to step one instruction. When we return,
+ * the instruction will get run and a sigtrap will get triggered when
+ * the one instruction is done.
+ *
+ * TODO: Be more like other archs where the original inst is put back,
+ * and the next inst is replaced with a afterBreakpoint trap. When we
+ * run, the afterBreakpoint trap is hit at the next instruction and
+ * then we can put back the original breakpoint and replace the
+ * afterBreakpoint trap with the original inst there too.
+ *
+ * For x86, this means computing how many bytes are used in the
+ * current instruction, and then placing an int3 (or maybe ud1) after
+ * it.
+ */
void
arch_do_displaced_inst(os_context_t * context, unsigned long orig_inst)
{
@@ -273,7 +290,7 @@ arch_do_displaced_inst(os_context_t * context, unsigned long orig_inst)
* Put the original instruction back.
*/
- *((char *) pc) = orig_inst & 0xff;
+ *pc = orig_inst & 0xff;
/*
* If we have the SC_EFLAGS macro, we can enable single-stepping
@@ -318,9 +335,8 @@ arch_do_displaced_inst(os_context_t * context, unsigned long orig_inst)
/*
- * Handles the break instruction from lisp, which is now UD2 followed
- * by the trap code. In particular, this does not handle the
- * breakpoint traps.
+ * Handles the ud1 instruction from lisp that is used to signal
+ * errors. In particular, this does not handle the breakpoint traps.
*/
void
sigill_handler(HANDLER_ARGS)
@@ -351,10 +367,10 @@ sigill_handler(HANDLER_ARGS)
RESTORE_FPU(os_context);
/*
- * On entry %eip points just after the INT3 byte and aims at the
- * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
- * number of bytes will follow, the first is the length of the byte
- * arguments to follow.
+ * On entry %eip points just to the beginning of the UD1
+ * instruction. For error-trap and cerror-trap a number of bytes
+ * will follow, the first is the length of the byte arguments to
+ * follow.
*/
DPRINTF(debug_handlers,
@@ -459,7 +475,13 @@ sigtrap_handler(HANDLER_ARGS)
*((unsigned char*)SC_PC(context) + 3),
*(unsigned char*)(SC_PC(context) + 4)));
- if (single_stepping && (signal == SIGTRAP)) {
+ if (single_stepping) {
+ /*
+ * We were single-stepping so we now need to disable
+ * single-stepping. We want to put back the breakpoint (int3)
+ * instruction so that the next time the breakpoint will be
+ * hit again as expected.
+ */
DPRINTF(debug_handlers, (stderr, "* Single step trap %p\n", single_stepping));
#ifdef SC_EFLAGS
@@ -492,6 +514,9 @@ sigtrap_handler(HANDLER_ARGS)
return;
}
+ /*
+ * We weren't single-stepping, so this we've just hit the breakpoint (int3). Handle it.
+ */
DPRINTF(debug_handlers, (stderr, "*C break\n"));
/*
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/cfb35624edee9113a81d90…
--
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/cfb35624edee9113a81d90…
You're receiving this email because of your account on gitlab.common-lisp.net.
Raymond Toy pushed to branch issue-97-define-ud2-inst at cmucl / cmucl
Commits:
cfb35624 by Raymond Toy at 2021-04-09T16:14:44-07:00
Modify UD1 inst so it prints out registers correctly
Hack the definition of the ud1 instruction format so that the reg/mem
field is really a word register. This then makes the pending
interrupt trap print out as "ud1 ecx, ecx" instead of "ud1 ecx, cl".
I don't know why the printer wanted to print cl, but `print-reg` was
passing `:byte` as the size (from the dstate `'width`).
This works around the issue.
- - - - -
1 changed file:
- src/compiler/x86/insts.lisp
Changes:
=====================================
src/compiler/x86/insts.lisp
=====================================
@@ -2062,13 +2062,20 @@
(code :field (byte 8 8)))
+;; The UD1 instruction. The mod bits of the mod r/m byte MUST be #b11
+;; so that the reg/mem field is actually a register. This is a hack
+;; to allow us to print out the reg/mem reg as a 32 reg. Using just
+;; reg/mem, the register sometimes printed out as a byte reg and I
+;; (toy.raymond) don't know why.
(disassem:define-instruction-format
(ud1 24 :default-printer '(:name :tab reg ", " reg/mem))
(prefix :field (byte 8 0) :value #b00001111)
(op :field (byte 8 8) :value #b10111001)
- (reg/mem :fields (list (byte 2 22) (byte 3 16))
- :type 'reg/mem)
- (reg :field (byte 3 19) :type 'word-reg))
+ ;; The mod bits ensure that the reg/mem field is interpreted as a
+ ;; register, not memory.
+ (reg/mem :field (byte 3 16) :type 'word-reg)
+ (reg :field (byte 3 19) :type 'word-reg)
+ (mod :field (byte 2 22) :value #b11))
(defun snarf-error-junk (sap offset &optional length-only)
(let* ((length (system:sap-ref-8 sap offset))
@@ -2125,7 +2132,7 @@
;; We just want the trap code in the third byte of the instruction.
(define-instruction ud1 (segment code)
(:declare (type (unsigned-byte 8) code))
- (:printer ud1 ((op #b10111001))
+ (:printer ud1 ((op #b10111001) (reg nil :type 'word-reg))
:default
:control #'ud1-control)
(:emitter
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/cfb35624edee9113a81d900…
--
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/cfb35624edee9113a81d900…
You're receiving this email because of your account on gitlab.common-lisp.net.
Raymond Toy pushed to branch issue-97-define-ud2-inst at cmucl / cmucl
Commits:
40fff139 by Raymond Toy at 2021-04-09T14:14:52-07:00
Use TRAP_CODE to insert the desired traps
- - - - -
3adaeb13 by Raymond Toy at 2021-04-09T14:15:50-07:00
Correctly extract the trap code from the instruction stream.
The code is in the low 6 bits. Need to ignore everything else when
extracting it.
Verified that function end breakpoints now work again instead of
getting a sigill error because of an unknown code.
- - - - -
2 changed files:
- src/lisp/x86-arch.c
- src/lisp/x86-assem.S
Changes:
=====================================
src/lisp/x86-arch.c
=====================================
@@ -369,7 +369,12 @@ sigill_handler(HANDLER_ARGS)
* trap code?
*/
if (*(unsigned short *) SC_PC(context) == 0xb90f) {
- trap = *(((char *)SC_PC(context)) + 2);
+ /*
+ * This must match what the lisp code is doing. The trap
+ * number is placed in the low 6-bits of the 3rd byte of the
+ * instruction.
+ */
+ trap = *(((char *)SC_PC(context)) + 2) & 63;
} else {
abort();
}
=====================================
src/lisp/x86-assem.S
=====================================
@@ -18,6 +18,16 @@
#include "internals.h"
#include "lispregs.h"
+/*
+ * Emit the appropriate instruction used for implementing traps.
+ * Currently, this is the UD1 instruction. However, it make it
+ * easy to add the trap code, use a sequence of bytes. The code
+ * is smashed into the mod r/m byte with the mod bits set to
+ * #b11. This MUST be coordinated with the Lisp code and the C
+ * code.
+ *
+ * Also, clang doesn't recognize the ud1 instruction.
+ */
#define TRAP_CODE(code) \
.byte 0x0f ; \
.byte 0xb9 ; \
@@ -249,10 +259,7 @@ ENDFUNC(sse_restore)
* The undefined-function trampoline.
*/
FUNCDEF(undefined_tramp)
- # UD1
- .byte 0x0f
- .byte 0xb9
- .byte trap_Error
+ TRAP_CODE(trap_Error)
/* Number of argument bytes */
.byte 2
.byte UNDEFINED_SYMBOL_ERROR
@@ -300,40 +307,28 @@ GNAME(function_end_breakpoint_trap):
.byte 0xb9
.byte 0xc0 + trap_PendingInterrupt
*/
- # UD1
- .byte 0x0f
- .byte 0xb9
- .byte 0xc0 + trap_FunctionEndBreakpoint
+ TRAP_CODE(trap_FunctionEndBreakpoint)
hlt # Should never return here.
+ENDFUNC(function_end_breakpoint_trap)
.globl GNAME(function_end_breakpoint_end)
GNAME(function_end_breakpoint_end):
-
FUNCDEF(do_pending_interrupt)
- # UD1
- .byte 0x0f
- .byte 0xb9
- .byte trap_PendingInterrupt
+ TRAP_CODE(trap_PendingInterrupt)
ret
ENDFUNC(do_pending_interrupt)
#ifdef trap_DynamicSpaceOverflowError
FUNCDEF(do_dynamic_space_overflow_error)
- # UD1
- .byte 0x0f
- .byte 0xb9
- .byte trap_DynamicSpaceOverflowError
+ TRAP_CODE(trap_DynamicSpaceOverflowError)
ret
ENDFUNC(do_dynamic_space_overflow_error)
#endif
#ifdef trap_DynamicSpaceOverflowWarning
FUNCDEF(do_dynamic_space_overflow_warning)
- # UD1
- .byte 0x0f
- .byte 0xb9
- .byte trap_DynamicSpaceOverflowWarning
+ TRAP_CODE(trap_DynamicSpaceOverflowWarning)
ret
ENDFUNC(do_dynamic_space_overflow_warning)
#endif
@@ -515,10 +510,7 @@ FUNCDEF(undefined_foreign_symbol_trap)
movl 8(%ebp),%eax
/* Now trap to Lisp */
- # UD1
- .byte 0x0f
- .byte 0xb9
- .byte trap_Error
+ TRAP_CODE(trap_Error)
/* Number of argument bytes */
.byte 2
.byte UNDEFINED_FOREIGN_SYMBOL_ERROR
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/93d7cf02396314c79a176f…
--
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/93d7cf02396314c79a176f…
You're receiving this email because of your account on gitlab.common-lisp.net.
Raymond Toy pushed to branch master at cmucl / cmucl
Commits:
20de3e8e by Jon Boone at 2021-04-09T20:59:59+00:00
adds additional keyword arguments to instance-usage for more fine-grained tracking of space allocation
- - - - -
68e97641 by Raymond Toy at 2021-04-09T21:00:00+00:00
Merge branch 'org.cmucl.debug.dynamic-space' into 'master'
adding additional keyword arguments to instance-usage ....
See merge request cmucl/cmucl!74
- - - - -
2 changed files:
- src/code/room.lisp
- src/i18n/locale/cmucl.pot
Changes:
=====================================
src/code/room.lisp
=====================================
@@ -707,11 +707,24 @@
;;; INSTANCE-USAGE -- Public
;;;
-(defun instance-usage (space &key (top-n 15))
- (declare (type spaces space) (type (or fixnum null) top-n))
- "Print a breakdown by instance type of all the instances allocated in
- Space. If TOP-N is true, print only information for the the TOP-N types with
- largest usage."
+(defun instance-usage (space &key
+ (top-n 15)
+ entries-var
+ (call-source "Unknown Caller"))
+ "Print a breakdown by instance type of all the allocation in Space.
+
+ :TOP-N
+ If true, print only the TOP-N types by largest usage.
+
+ :ENTRIES-VAR
+ If bound, contains the name of the symbol used to store the hash-table
+ of allocated entries for later processing.
+
+ :CALL-SOURCE
+ A string identifying the location from which instance-usage was called."
+
+ (declare (type spaces space) (type (or fixnum null) top-n)
+ (type (or symbol null) entries-var) (type string call-source))
(format t (intl:gettext "~2&~@[Top ~D ~]~(~A~) instance types:~%") top-n space)
(let ((totals (make-hash-table :test #'eq))
(total-objects 0)
@@ -734,6 +747,10 @@
space)
(collect ((totals-list))
+ ;; set entries-var to the list of entries in totals
+ (when entries-var
+ (setf (symbol-value entries-var) (list call-source totals)))
+
(maphash #'(lambda (class what)
(totals-list (cons (prin1-to-string
(class-proper-name class))
=====================================
src/i18n/locale/cmucl.pot
=====================================
@@ -6333,10 +6333,19 @@ msgstr ""
#: src/code/room.lisp
msgid ""
-"Print a breakdown by instance type of all the instances allocated in\n"
-" Space. If TOP-N is true, print only information for the the TOP-N types "
-"with\n"
-" largest usage."
+"Print a breakdown by instance type of all the allocation in Space. \n"
+"\n"
+" :TOP-N \n"
+" If true, print only the TOP-N types by largest usage.\n"
+"\n"
+" :ENTRIES-VAR\n"
+" If bound, contains the name of the symbol used to store the hash-table"
+"\n"
+" of allocated entries for later processing.\n"
+"\n"
+" :CALL-SOURCE\n"
+" A string identifying the location from which instance-usage was "
+"called."
msgstr ""
#: src/code/room.lisp
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/5c92c14c7a8497c7f6bf86…
--
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/5c92c14c7a8497c7f6bf86…
You're receiving this email because of your account on gitlab.common-lisp.net.