1 | ;;; inspect.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2005 Peter Graves |
---|
4 | ;;; $Id: inspect.lisp 14105 2012-08-17 08:50:58Z ehuelsmann $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package #:system) |
---|
33 | |
---|
34 | (require 'clos) |
---|
35 | (require 'format) |
---|
36 | |
---|
37 | |
---|
38 | (defvar *inspect-break* nil) |
---|
39 | (defvar *inspected-object-stack* nil) |
---|
40 | (defvar *inspected-object* nil) |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | (defun leader (name) |
---|
45 | (let ((size (max 0 (- 16 (length (string name)))))) |
---|
46 | (concatenate 'string (make-string size :initial-element #\-) "->"))) |
---|
47 | |
---|
48 | (defun safe-length (x) |
---|
49 | (do ((n 0 (+ n 2)) |
---|
50 | (fast x (cddr fast)) |
---|
51 | (slow x (cdr slow))) |
---|
52 | (()) |
---|
53 | (when (null fast) |
---|
54 | (return (values n :proper))) |
---|
55 | (when (atom fast) |
---|
56 | (return (values n :dotted))) |
---|
57 | (when (null (cdr fast)) |
---|
58 | (return (values (+ n 1) :proper))) |
---|
59 | (when (atom (cdr fast)) |
---|
60 | (return (values (+ n 1) :dotted))) |
---|
61 | (when (and (eq fast slow) (> n 0)) |
---|
62 | (return (values nil :circular))))) |
---|
63 | |
---|
64 | (defun display-object (obj) |
---|
65 | (let ((*print-length* 2) |
---|
66 | (*print-level* 2)) |
---|
67 | (cond ((typep obj 'standard-object) |
---|
68 | (let ((parts (inspected-parts obj)) |
---|
69 | (i 0)) |
---|
70 | (dolist (part parts) |
---|
71 | (let ((name (car part)) |
---|
72 | (value (cdr part))) |
---|
73 | (format t "~4D ~A ~A ~S~%" |
---|
74 | i |
---|
75 | name |
---|
76 | (leader name) |
---|
77 | value) |
---|
78 | (incf i))))) |
---|
79 | ((simple-vector-p obj) |
---|
80 | (format t "~A at #x~X~%" (inspected-description obj) (identity-hash-code obj)) |
---|
81 | (let ((limit (min (length obj) 25))) |
---|
82 | (dotimes (i limit) |
---|
83 | (format t "~4D-> ~A~%" i (aref obj i))))) |
---|
84 | ((vectorp obj) |
---|
85 | (format t "~A~%" (inspected-description obj)) |
---|
86 | (let ((limit (min (length obj) 25))) |
---|
87 | (dotimes (i limit) |
---|
88 | (format t "~4D-> ~A~%" i (aref obj i))))) |
---|
89 | ((consp obj) |
---|
90 | (multiple-value-bind (len kind) (safe-length obj) |
---|
91 | (case kind |
---|
92 | (:proper |
---|
93 | (format t "A proper list with ~D elements at #x~X~%" |
---|
94 | len |
---|
95 | (identity-hash-code obj)) |
---|
96 | (let ((i 0)) |
---|
97 | (dolist (item obj) |
---|
98 | (cond ((< i 25) |
---|
99 | (format t "~4D-> ~S~%" i item)) |
---|
100 | ((= i 25) |
---|
101 | (format t " ...~%")) |
---|
102 | ((= i (1- len)) |
---|
103 | (format t "~4D-> ~S~%" i item))) |
---|
104 | (incf i)))) |
---|
105 | (:dotted |
---|
106 | (format t "A dotted list with ~D elements at #x~X~%" |
---|
107 | len |
---|
108 | (identity-hash-code obj)) |
---|
109 | (let* ((rest obj) |
---|
110 | (item (car rest)) |
---|
111 | (i 0)) |
---|
112 | (loop |
---|
113 | (cond ((< i 25) |
---|
114 | (format t "~4D-> ~S~%" i item)) |
---|
115 | ((= i 25) |
---|
116 | (format t " ...~%"))) |
---|
117 | (incf i) |
---|
118 | (setf rest (cdr rest)) |
---|
119 | (when (atom rest) |
---|
120 | (return)) |
---|
121 | (setf item (car rest))) |
---|
122 | (format t "tail-> ~S~%" rest))) |
---|
123 | (:circular |
---|
124 | (format t "A circular list at #x~X~%" (identity-hash-code obj)))))) |
---|
125 | (t |
---|
126 | (format t "~A~%" (inspected-description obj)) |
---|
127 | (let ((parts (inspected-parts obj)) |
---|
128 | (i 0) |
---|
129 | (limit 25)) |
---|
130 | (dolist (part parts) |
---|
131 | (let ((name (string (car part))) |
---|
132 | (value (cdr part))) |
---|
133 | (format t "~4D ~A ~A ~S~%" i |
---|
134 | name |
---|
135 | (leader name) |
---|
136 | value) |
---|
137 | (incf i) |
---|
138 | (when (> i limit) |
---|
139 | (return)))))))) |
---|
140 | (values)) |
---|
141 | |
---|
142 | (defun display-current () |
---|
143 | (if *inspect-break* |
---|
144 | (display-object *inspected-object*) |
---|
145 | (format t "No object is being inspected."))) |
---|
146 | |
---|
147 | (defun inspect (obj) |
---|
148 | (when ext:*inspector-hook* |
---|
149 | (funcall ext:*inspector-hook* obj)) |
---|
150 | (when *inspected-object* |
---|
151 | (push *inspected-object* *inspected-object-stack*)) |
---|
152 | (setf *inspected-object* obj) |
---|
153 | (let* ((*inspect-break* t) |
---|
154 | (*debug-level* (1+ *debug-level*))) |
---|
155 | (setf *** ** |
---|
156 | ** * |
---|
157 | * obj) |
---|
158 | (display-current) |
---|
159 | (catch 'inspect-exit |
---|
160 | (tpl::repl))) |
---|
161 | (setf *** ** |
---|
162 | ** * |
---|
163 | * obj) |
---|
164 | (values)) |
---|
165 | |
---|
166 | (defun istep (args) |
---|
167 | (if (null args) |
---|
168 | (display-current) |
---|
169 | (let* ((pos (position #\space args)) |
---|
170 | (option-string (if pos (subseq args 0 pos) args)) |
---|
171 | (option (read-from-string option-string))) |
---|
172 | (cond ((string= option-string "-") |
---|
173 | (if *inspected-object-stack* |
---|
174 | (progn |
---|
175 | (setf *inspected-object* (pop *inspected-object-stack*)) |
---|
176 | (setf *** ** |
---|
177 | ** * |
---|
178 | * *inspected-object*) |
---|
179 | (display-current)) |
---|
180 | (format t "Object has no parent."))) |
---|
181 | ((string= option-string "q") |
---|
182 | (setf *inspected-object* nil |
---|
183 | *inspected-object-stack* nil |
---|
184 | *inspect-break* nil) |
---|
185 | (throw 'inspect-exit nil)) |
---|
186 | ((fixnump option) |
---|
187 | (let* ((index option) |
---|
188 | (parts (inspected-parts *inspected-object*))) |
---|
189 | (cond ((null parts) |
---|
190 | (if (typep *inspected-object* 'sequence) |
---|
191 | (if (or (minusp index) |
---|
192 | (>= index (length *inspected-object*))) |
---|
193 | (format t "Invalid index (~D)." index) |
---|
194 | (progn |
---|
195 | (push *inspected-object* *inspected-object-stack*) |
---|
196 | (setf *inspected-object* |
---|
197 | (elt *inspected-object* index)) |
---|
198 | (setf * *inspected-object*) |
---|
199 | (display-current))) |
---|
200 | (format t "Object has no selectable components."))) |
---|
201 | ((or (minusp index) |
---|
202 | (>= index (length parts))) |
---|
203 | (format t "Invalid index (~D)." index)) |
---|
204 | (t |
---|
205 | (push *inspected-object* *inspected-object-stack*) |
---|
206 | (setf *inspected-object* (cdr (elt parts index))) |
---|
207 | (setf * *inspected-object*) |
---|
208 | (display-current))))))))) |
---|