Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 281f6c8bf4274ef85336e0d2c4e0fb595f212701 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2017 Xilinx, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Xilinx - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.debug.gdb.test;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.LinkedList;

class TestThread extends Thread {

    final Object sync = new Object();
    final String prompt = "(gdb) ";

    class OutputReader extends Thread {
        final InputStream inp;
        final StringBuffer buf = new StringBuffer();
        final LinkedList<String> lst = new LinkedList<String>();

        OutputReader(InputStream inp) {
            this.inp = new BufferedInputStream(inp);
        }

        @Override
        public void run() {
            try {
                for (;;) {
                    int ch = inp.read();
                    if (ch < 0) break;
                    if (ch == '\r') continue;
                    synchronized (sync) {
                        if (ch == '\n') {
                            lst.add(buf.toString());
                            buf.setLength(0);
                        }
                        else {
                            buf.append((char) ch);
                        }
                        sync.notify();
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                System.exit(7);
            }
        }
    }

    Process prs;
    OutputReader std_out;
    OutputReader std_err;
    Writer std_inp;

    void checkOutput() throws Exception {
        long time = System.currentTimeMillis();
        do {
            synchronized (sync) {
                while (!std_err.lst.isEmpty()) {
                    System.err.println(std_err.lst.removeFirst());
                    System.err.flush();
                }
                while (!std_out.lst.isEmpty()) {
                    System.out.println(std_out.lst.removeFirst());
                    System.out.flush();
                }
                sync.wait(100);
                if (std_err.lst.isEmpty() && std_out.lst.isEmpty() &&
                        std_out.buf.length() > 0 && std_out.buf.toString().equals(prompt)) break;
            }
        }
        while (System.currentTimeMillis() < time + 10000);
        synchronized (sync) {
            if (std_out.buf.length() > 0) {
                System.out.print(std_out.buf.toString());
                std_out.buf.setLength(0);
            }
        }
    }

    void cmd(String c) throws Exception {
        checkOutput();
        std_inp.write(c);
        std_inp.write("\n");
        std_inp.flush();
        System.out.println(c);
        System.out.flush();
        if (c.startsWith("mon ") || c.startsWith("target ")) {
            sleep(1000); // Need to sleep because command output goes to stderr
        }
        synchronized (sync) {
            for (;;) {
                if (std_out.buf.length() > 0 && std_out.buf.toString().equals(prompt)) break;
                if (!prs.isAlive()) break;
                sync.wait(500);
            }
        }
    }

    @Override
    public void run() {
        try {
            String gdb = System.getenv().get("TCF_TEST_GDB_EXE");
            if (gdb == null) gdb = "gdb";

            String port = System.getenv().get("TCF_TEST_GDB_PORT");
            if (port == null) port = "3000";

            BigInteger prev_pc = null;
            prs = Runtime.getRuntime().exec(new String[] {
                gdb,
                "-q"
            }, null);
            std_inp = new BufferedWriter(new OutputStreamWriter(prs.getOutputStream()));
            std_out = new OutputReader(prs.getInputStream());
            std_err = new OutputReader(prs.getErrorStream());
            std_out.start();
            std_err.start();

            cmd("set remotetimeout 1000");

            cmd("target extended-remote 127.0.0.1:" + port);

            cmd("mon ps");
            if (std_err.lst.size() < 1)
                throw new Exception("Invalid 'mon ps' reply: cnt < 1");
            if (!std_err.lst.get(0).startsWith("1: "))
                throw new Exception("Invalid 'mon ps' reply: list");

            for (int pass = 0; pass < 10; pass++) {

                cmd("info thread");
                if (std_out.lst.size() < 2)
                    throw new Exception("Invalid 'info thread' reply: cnt < 2");
                if (!std_out.lst.get(0).startsWith("  Id   Target Id"))
                    throw new Exception("Invalid 'info thread' reply: header");
                if (!std_out.lst.get(1).startsWith("* 1    Thread 1.1 ("))
                    throw new Exception("Invalid 'info thread' reply: list");

                cmd("info infer");
                if (std_out.lst.size() < 2)
                    throw new Exception("Invalid 'info infer' reply: cnt < 2");
                if (!std_out.lst.get(0).startsWith("  Num  Description"))
                    throw new Exception("Invalid 'info infer' reply: header");
                if (!std_out.lst.get(1).startsWith("* 1    process "))
                    throw new Exception("Invalid 'info infer' reply: list");

                cmd("info reg");

                cmd("bt");
                if (std_out.lst.size() < 1)
                    throw new Exception("Invalid 'bt' reply: cnt < 1");
                if (!std_out.lst.get(0).startsWith("#0  0x"))
                    throw new Exception("Invalid 'bt' reply");
                String x = std_out.lst.get(0).substring(6);
                BigInteger pc = new BigInteger(x.substring(0, x.indexOf(' ')), 16);
                if (prev_pc != null && pc.equals(prev_pc))
                    throw new Exception("Prev PC = PC: 0x" + pc.toString(16));

                cmd("disass /r 0x" + pc.toString(16) + ",+64");
                if (std_out.lst.size() < 2)
                    throw new Exception("Invalid 'disass' reply: cnt < 2");
                if (!std_out.lst.get(0).startsWith("Dump of assembler code from 0x"))
                    throw new Exception("Invalid 'disass' reply: header");
                if (!std_out.lst.get(1).startsWith("=> 0x"))
                    throw new Exception("Invalid 'disass' reply: list");

                cmd("b *0x" + pc.toString(16));
                if (std_out.lst.size() < 1)
                    throw new Exception("Invalid 'break' reply: cnt < 1");
                if (!std_out.lst.get(0).startsWith("Breakpoint "))
                    throw new Exception("Invalid 'break' reply");
                String y = std_out.lst.get(0).substring(11);
                int b = Integer.parseInt(y.substring(0, y.indexOf(' ')));

                cmd("d " + b);


                if (pass == 0) {
                    cmd("cont");
                }
                else {
                    cmd("stepi");
                }
                prev_pc = pc;

            }

            /* Run until exit */
            cmd("cont");
            if (std_out.lst.size() < 2)
                throw new Exception("Invalid 'cont' reply: cnt < 2");
            if (!std_out.lst.get(0).startsWith("Continuing."))
                throw new Exception("Invalid 'cont' reply: start");
            if (!std_out.lst.get(1).startsWith("[Inferior 1 "))
                throw new Exception("Invalid 'cont' reply: end");

            cmd("info thread");
            if (std_out.lst.size() != 1)
                throw new Exception("Invalid 'infor thread' reply: cnt != 1");
            if (!std_out.lst.get(0).startsWith("No threads."))
                throw new Exception("Invalid 'info threads' reply");

            cmd("quit");
            prs.waitFor();
            std_out.join();
            std_err.join();
            System.exit(0);
        }
        catch (Exception e) {
            try {
                checkOutput();
            }
            catch (Exception x) {
            }
            e.printStackTrace();
            System.exit(7);
        }
    }
}

Back to the top