-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbrowser.lisp
376 lines (323 loc) · 12.3 KB
/
browser.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
;;;;------------------------------------------------------------------
;;;;
;;;; Copyright (C) 2001-2002, 2004,
;;;; Department of Computer Science, University of Tromso, Norway.
;;;;
;;;; For distribution policy, see the accompanying file COPYING.
;;;;
;;;; Filename: browser.lisp
;;;; Description: A CLIM browser/inspector of Movitz images.
;;;; Author: Frode Vatvedt Fjeld <[email protected]>
;;;; Created at: Thu Jun 14 15:14:35 2001
;;;;
;;;; $Id: browser.lisp,v 1.2 2004/01/19 11:23:41 ffjeld Exp $
;;;;
;;;;------------------------------------------------------------------
(eval-when (:compile-toplevel :load-toplevel)
#+allegro (require :climxm))
(defpackage movitz-browser
(:use clim clim-lisp movitz binary-types)
(:export browse-file
browse-pid
browse-word
browser))
(in-package movitz-browser)
(define-command-table browser-file-commands
:menu (("Print" :command print-graph)
("Print preview" :command print-graph-preview)
("" :divider :line)
("Quit" :command quit)))
(define-command-table browser-tree-commands
:menu (("Set NIL as root" :command (read-and-set-root nil))
("Enter word" :command set-root-word)))
(define-command quit ()
(frame-exit *application-frame*))
(define-command print-graph ()
(multiple-value-call #'warn
"print!: ~S :"
(select-file *application-frame*)))
(define-command print-graph-preview ()
(let ((temp-name (sys:make-temp-file-name "browser-graph-preview")))
(with-open-file (temp-file temp-name :direction :output)
(with-output-to-postscript-stream (ps-stream temp-file)
(display-graph *application-frame* ps-stream)))
(excl:run-shell-command (format nil "gv -resize ~S; rm ~S" temp-name temp-name) :wait nil)))
(define-application-frame browser ()
((root-tuple
:initarg :root-tuple
:accessor browser-root-tuple))
(:menu-bar t)
(:pointer-documentation t)
(:command-table (browser
:menu (("File" :menu browser-file-commands)
("Tree" :menu browser-tree-commands))
:inherit-from (browser-file-commands browser-tree-commands)))
(:panes
(graph
:application
;; :label "Object Graph"
;; :scroll-bars nil
:initial-cursor-visibility nil
:display-function 'display-graph))
(:layouts
(default (horizontally ()
graph))))
(defstruct graph-tuple
tree
object
parent
slot-name)
(define-presentation-type graph-tuple () :inherit-from t)
(defun display-graph (browser *standard-output*)
(format-graph-from-root (browser-root-tuple browser)
;; printer
#'(lambda (tuple *standard-output*)
(with-output-as-presentation (t tuple 'graph-tuple)
(with-slots (object slot-name) tuple
(formatting-table ()
(formatting-column ()
(when slot-name
(formatting-cell (t :align-x :center)
(display-child-spec slot-name)))
(formatting-cell (t :align-x :center)
(present object)))))))
;; child-producer
#'(lambda (tuple)
(with-slots (tree object parent slot-name) tuple
;; (warn "child-of: ~S" (type-of object))
(mapcar #'(lambda (child-slot-name)
(make-graph-tuple
:tree tree
:object (browser-child object child-slot-name)
:parent object
:slot-name child-slot-name))
(browser-open-slots tree object parent slot-name))))
:graph-type :digraph
:within-generation-separation 2
:maximize-generations nil
:generation-separation 60
:store-objects t
:center-nodes nil
:orientation :horizontal ;; :vertical
;; :duplicate-key #'cdr
;; :duplicate-test #'equalp
:merge-duplicates t
:cutoff-depth 50))
(defun display-child-spec (spec)
(case (first spec)
(slot-value
(with-drawing-options (t :ink +green+ :size :small)
(princ (string-downcase (format nil "~A" (second spec))))))
(otherwise
(princ spec))))
(defmethod movitz-object-browser-properties ((object t)) nil)
(defmethod browser-child ((object movitz-heap-object) child-spec)
(ecase (car child-spec)
(slot-value
(slot-value object (second child-spec)))))
(defclass browser-array ()
((type
:initarg :type
:reader browser-array-type)
(elements
:initarg :elements
:reader browser-array-elements)))
(define-presentation-type browser-array ())
(defmethod browser-child ((object movitz-vector) child-spec)
(destructuring-bind (operator &rest operands)
child-spec
(case operator
(aref
(nth (first operands)
(movitz-vector-symbolic-data object)))
(array
(make-instance 'browser-array
:type (movitz-vector-element-type object)
:elements (movitz-vector-symbolic-data object)))
(t (call-next-method object child-spec)))))
(defmethod browser-child ((object movitz-struct) child-spec)
(destructuring-bind (operator &rest operands)
child-spec
(case operator
(struct-ref
(nth (first operands)
(movitz-struct-slot-values object)))
(t (call-next-method)))))
(defun browser-slot-value (object slot-name)
(case (binary-slot-type (type-of object) slot-name)
(word (movitz-word (binary-slot-value object slot-name)))
(t (if (slot-boundp object slot-name)
(slot-value object slot-name)
(make-symbol "[UNBOUND]")))))
(defun browser-all-slots (object)
(mapcar #'(lambda (slot-name) (list 'slot-value slot-name))
(binary-record-slot-names (type-of object))))
(defmethod browser-default-open-slots ((object movitz-heap-object))
(reverse (set-difference (browser-all-slots object)
'((slot-value movitz::type))
:key #'second)))
(defmethod browser-default-open-slots ((object movitz-vector))
(assert (= (length (movitz-vector-symbolic-data object))
(movitz-vector-num-elements object)))
(append (remove 'movitz::data (call-next-method object) :key #'second)
(case (movitz-vector-element-type object)
(:any-t
;; merge EQ elements..
(loop for (value next-value) on (movitz-vector-symbolic-data object)
as i upfrom 0
with start-index = 0
unless (and next-value
(= (movitz-intern value)
(movitz-intern next-value)))
collect `(aref ,start-index ,@(unless (= i start-index) (list i)))
and do (setf start-index (1+ i))))
(t (list `(array ,(movitz-vector-num-elements object)))))))
(defmethod browser-default-open-slots ((object movitz-struct))
(append (remove 'movitz::slot0 (call-next-method object) :key #'second)
(loop for x from 0 below (movitz-struct-length object)
collect `(struct-ref ,x))))
(defun browse-image (*image* &key (root (make-graph-tuple
:object (movitz-word (movitz-read-and-intern nil 'word))
:tree (gensym))))
(let ((*endian* :little-endian)
(*print-radix* t)
(*print-base* 16))
(run-frame-top-level
(make-application-frame 'browser
:width 700
:height 700
:root-tuple root))))
(defun browse-word (word)
(browse-image movitz::*image*
:root (make-graph-tuple :object (movitz-word word)
:tree (gensym))))
(defun browser ()
(multiprocessing:process-run-function "browser" #'browse-image *i*))
(defun browse-pid (pid)
(flet ((do-browse-pid (pid)
(with-procfs-image (pid)
(browse-image *image*))))
(multiprocessing:process-run-function
`(:name "browser")
#'do-browse-pid
pid)))
(defun browse-file (&key (threadp t) (path *default-image-file*)
(offset (- 512 #x100000)) (direction :input))
(flet ((do-browse-path (path offset direction)
(with-binary-file (stream path :direction direction)
(browse-image (make-instance 'stream-image
:stream stream
:offset offset)))))
(if threadp
(multiprocessing:process-run-function "browser" #'do-browse-path
path offset direction)
(do-browse-path path offset direction))))
(define-presentation-type movitz-object ())
(define-presentation-method present (object (type movitz-object)
*standard-output*
(view textual-view) &key)
(formatting-table ()
(formatting-column ()
(formatting-cell (t :align-x :center)
(with-drawing-options (t :size :small)
(browser-print-safely object)))
(formatting-cell (t :align-x :center)
(format t "#x~8,'0X" (movitz-intern object))))))
(define-presentation-method present
(object (type movitz-object) *standard-output* (view textual-menu-view) &key)
(format t "#x~8,'0X" (movitz-intern object)))
(define-presentation-method present
(object (type graph-tuple) *standard-output* (view textual-menu-view) &key)
(format t "#x~8,'0X" (movitz-intern (graph-tuple-object object))))
(define-presentation-method present (object (type movitz-character)
*standard-output*
(view textual-view) &key)
(write (movitz-char object)))
(define-presentation-method present
(object (type movitz-symbol) *standard-output* (view textual-view) &key)
(format t "#x~8,'0X: |~A|" (movitz-intern object) (browser-print-safely object)))
(define-presentation-method present
(object (type movitz-vector) *standard-output* (view textual-view) &key)
(if (not (eq :character (movitz-vector-element-type object)))
(call-next-method)
(format t "#x~8,'0X: \"~A\"" (movitz-intern object) (browser-print-safely object))))
(defun browser-print-safely (object)
(handler-case
(movitz::movitz-print object)
(error ()
(write-string (string-downcase (symbol-name (type-of object)))))))
(define-presentation-method present (object (type browser-array)
*standard-output*
(view textual-view) &key)
(let ((rows-per-col (typecase (length (browser-array-elements object))
((integer 0 15) 1)
((integer 16 47) 2)
((integer 48 127) 4)
(t 8))))
(formatting-table ()
(loop for row on (browser-array-elements object) by #'(lambda (x) (nthcdr rows-per-col x))
as i upfrom 0 by rows-per-col
do (formatting-row ()
(formatting-cell (t :align-x :right)
(format t "~D:" i))
(loop for r from 1 to rows-per-col
as element in row
do (formatting-cell ()
(case (browser-array-type object)
(:u32 (format t "#x~8,'0X" element))
((:u8 :code) (format t "#x~2,'0X" element))
(t #+ignore(warn "unk: ~S" (browser-array-type object))
(write element))))))))))
(define-browser-command read-and-set-root ((object 't))
(set-root (movitz-word (movitz-read-and-intern object 'word))))
(define-browser-command toggle ((tuple 'graph-tuple))
(with-slots (tree object parent slot-name) tuple
(cond
((null (browser-open-slots tree object parent slot-name))
(setf (browser-open-slots tree object parent slot-name)
(browser-default-open-slots object)))
;; (warn "now open: ~S" (browser-open-slots tree object parent slot-name)))
(t
(setf (browser-open-slots tree object parent slot-name)
nil)))))
(define-presentation-to-command-translator toggle
(graph-tuple
toggle
browser
:gesture :select
:tester ((object)
(typep (graph-tuple-object object) 'movitz-heap-object)))
(object)
(list object))
(define-browser-command set-root ((object 'movitz-object))
(setf (browser-root-tuple *application-frame*)
(make-graph-tuple :tree (gensym) :object object)))
(define-presentation-to-command-translator set-root-tuple
(graph-tuple set-root browser)
(object)
(list (graph-tuple-object object)))
(define-browser-command new-browser ((object 'movitz-object))
(browse-image *image* :root (make-graph-tuple :tree (gensym)
:object object)))
(define-presentation-to-command-translator new-browser-tuple
(graph-tuple new-browser browser)
(object)
(list (graph-tuple-object object)))
(defun (setf browser-open-slots) (value tree object parent slot-name)
(let ((old-slot (assoc-if #'(lambda (x) (and (eq (car x) parent) (eq (cdr x) slot-name)))
(getf (movitz-object-browser-properties object) tree))))
(if old-slot
(setf (cdr old-slot) value)
(setf (getf (movitz-object-browser-properties object) tree)
(acons (cons parent slot-name)
value
(getf (movitz-object-browser-properties object) tree)))))
value)
(defun browser-open-slots (tree object parent slot-name)
(cdr (assoc-if #'(lambda (x) (and (eq (car x) parent) (eq (cdr x) slot-name)))
(getf (movitz-object-browser-properties object) tree))))
(define-browser-command set-root-word ()
(let ((word (accepting-values (t :own-window t)
(accept '((integer 0 #xffffffff) :base 16)))))
(when word
(set-root (movitz-word word)))))