1 package com.tapina.robe.debug;
2
3 import com.tapina.robe.runtime.Environment;
4 import com.tapina.robe.runtime.MemoryMap;
5
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.io.Writer;
9 import java.text.DateFormat;
10 import java.util.Date;
11
12 /***
13 * This class scans an area maintained by 'flex' and writes its state to an output log
14 */
15 public class FlexWalk {
16 public static final int MAXLINE = 5;
17
18 public static void walk(Environment env, int baseAddress) throws IOException {
19 walk(env, baseAddress, new OutputStreamWriter(System.out));
20 }
21
22 public static void walk(Environment env, int baseAddress, Writer out) throws IOException {
23 out.write("\nFlex Walk: ");
24 out.write(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(new Date()));
25 out.write(" at &");
26 out.write(Integer.toHexString(baseAddress));
27 out.write("\n");
28 out.write("Flex area information\n---------------------\n\n");
29 out.flush();
30 int p = baseAddress;
31 do {
32 out.write("Flex record at &" + Integer.toHexString(p));
33 out.write("\n-----------------------\n\n");
34 final MemoryMap memoryMap = env.getMemoryMap();
35 final int size = memoryMap.getWord(p + 4);
36 if (size == 0) {
37 break;
38 }
39 out.write(
40 "Block at &" + Integer.toHexString(p + 8) + " size " + size + " (&" + Integer.toHexString(size) +
41 ")\n");
42 final int anchor = memoryMap.getWord(p);
43 out.write(
44 "Client's pointer at &" + Integer.toHexString(anchor) + ", its block at &" +
45 Integer.toHexString(memoryMap.getWord(anchor)));
46 out.write(" (" + ((memoryMap.getWord(anchor) == (p + 8)) ? "ok" : "INCORRECT") + ")\n\n");
47 out.flush();
48 out.write(" Addr Offset 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n\n");
49 final int total = 1 + ((size - 1) / 16);
50 int line = 0;
51 for (int off = 0; off < size; off += 16)
52 ++line;
53 if (line == (MAXLINE + 1)) {
54 out.write(" : :\n");
55 }
56 if (line > MAXLINE && line <= (total - MAXLINE)) {
57 continue;
58 }
59
60 final int q = ((p + 8)) + off;
61
62 final String qs = "0000000" + Integer.toHexString(q);
63 out.write("&" + qs.substring(qs.length() - 7, qs.length()));
64 final String offs = " " + String.valueOf(off);
65 out.write(" " + offs.substring(offs.length() - 6, offs.length()) + " : ");
66 for (int i = 0; i < 16; ++i) {
67 final String s = Integer.toHexString(memoryMap.getByte(q + i)) + " ";
68 out.write(s.length() == 2 ? "0" + s : s);
69 }
70 out.write(": ");
71 for (int i = 0; i < 16; ++i) {
72 final char c = (char) memoryMap.getByte(q + i);
73 out.write(Character.isISOControl(c) ? '.' : c);
74 }
75 out.write("\n");
76 }
77 out.write("\n\n");
78 out.flush();
79 p = p + 8 + ((size + 3) & (~3));
80 } while (true);
81 }
82 }