summaryrefslogtreecommitdiffstats
path: root/zpu/sw/simulator/com/zylin/zpu/simulator/gdb/GDBServer.java
blob: 182e42606b84da97f4e2dff6b9186a5af70c2a04 (plain)
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
/** 
 * Handles TCP/IP communication between simulator and GDB
 */

package com.zylin.zpu.simulator.gdb;

import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

import com.zylin.zpu.simulator.Host;
import com.zylin.zpu.simulator.Sim;
import com.zylin.zpu.simulator.SimApp;
import com.zylin.zpu.simulator.exceptions.BadPacketException;
import com.zylin.zpu.simulator.exceptions.CPUException;
import com.zylin.zpu.simulator.exceptions.EndSessionException;
import com.zylin.zpu.simulator.exceptions.GDBServerException;
import com.zylin.zpu.simulator.exceptions.MemoryAccessException;
import com.zylin.zpu.simulator.exceptions.UnsupportedSyscallException;

public class GDBServer implements Host 
{
	/* logging filter */
	static final boolean UNKNOWN=false;
	static final boolean ALL=false;
	static final boolean CPUEXCEPTION = false;
	static protected boolean MINIMAL=true;
	static boolean PACKET=true;
	static boolean REPLY=true;
	static protected boolean IGNOREDEXCEPTIONS=false;
	
	
	
	protected Throwable packetException;
	protected Object packetReady=new Object();
	private Packet packet;
	boolean done;
	private Socket sc;
    public boolean alive;
    static private int sessionNr;
	private SimApp app;
    Sim simulator;
    
    public GDBServer(Sim simulator, SimApp app)
    {
        this.simulator=simulator;
    	this.app=app;
    }
    
	void print(boolean filter, String str)
	{
		if (filter)
		{
			System.out.println(str);
		}
	}
	
	/** infinite loop that waits for debug sessions to be initiated via TCP/IP */
	public void gdbServer() throws MemoryAccessException, IOException, GDBServerException, EndSessionException 
	{
		sc=app.serverSocket.accept();
		try
		{
			debugSession();
		} catch (IOException e)
		{
			// the session failed...
			if (IGNOREDEXCEPTIONS)
			{
				e.printStackTrace();
			}
		} catch (GDBServerException e)
		{
			// connect failed...
			if (IGNOREDEXCEPTIONS)
			{
				e.printStackTrace();
			}
		} catch (EndSessionException e)
		{
		} catch (Throwable e)
		{
			// some terrible unforseen failure.
			e.printStackTrace();
		} finally
		{
			sc.close();
		}
	}


	
	protected void sleepABit()
	{
		try
		{
			// just to avoid locking up the machine in a busy loop when
			// debugging the Simulator
			Thread.sleep(2000);
		} catch (InterruptedException e1)
		{
			e1.printStackTrace();
		}
	}

	private void debugSession() throws IOException, GDBServerException, EndSessionException, MemoryAccessException 
	{
		print(MINIMAL, "GDB server waiting for connection " + sessionNr++ + "...");

        try
        {
            sessionStarted();

            expect('+'); // connection ack.

            sessionLoop();
        } finally
        {
			print(MINIMAL, "Session ended");
        }
            
	}

	private void sessionStarted()
	{
		simulator.sessionStarted();
		print(MINIMAL, "Session started");
	}

	
	private void sessionLoop() throws IOException, EndSessionException
	{
		alive=true;
		while (alive)
		{
			try
			{
				/* wait for new packet to arrive and notify the packet execution thread... */
				packet=new Packet(this);
				packet.receive();
		
				// During execution we can receive an abort/suspend command...
				packet.parseAndExecute();
				
				if (!alive)
				    throw new EndSessionException();
			} catch (BadPacketException e)
			{
				// do nothing.
				if (IGNOREDEXCEPTIONS)
				{
					e.printStackTrace();
				}
				sleepABit();
			} catch (GDBServerException e)
			{
				if (IGNOREDEXCEPTIONS)
				{
					e.printStackTrace();
				}
				// continue processing packets
				sleepABit();
			}
		}
	}


	

	void expect(char nextChar) throws IOException, GDBServerException
	{
		int t = read();
		if (t!=nextChar)
		{
			throw new BadPacketException();
		}
	}

	int read() throws IOException
	{
		flush();
		int t=sc.getInputStream().read();
		if (t==-1)
			throw new IOException();
		return t;
	}

	/**
	 * @param value
	 * @return
	 */
	protected String printHex(int value)
	{
		return formatHex(value, "00000000");
	}

	/**
	 * @param value
	 * @param pad TODO
	 * @return
	 */
	protected String formatHex(int value, String pad)
	{
		String t=Integer.toHexString(value);
		if (t.length()>pad.length())
		{
			t=t.substring(0, pad.length());
		}
		return pad.substring(0, pad.length()-t.length())+t;
	}

	public void write(byte[] bytes) throws IOException
	{
		sc.getOutputStream().write(bytes);
	}

	void flush() throws IOException 
	{
		sc.getOutputStream().flush();
	}

    
    private boolean enterSyscall;


    
    /* handle all sorts of IO calls, etc. by sending them to the
     */
    public void syscall(Sim s) throws CPUException
    {
        simulator.suspend();
        enterSyscall=true;
    }


    protected void performSyscall() 
    {
        enterSyscall=false;
        try
        {
            int id;
            id=simulator.getArg(1);
            Packet syscall;
            syscall=new Packet(this);
            switch (id)
            {
            case Host.SYS_write:
                syscall.invokeSyscall("write", 3, "iii");
                break;
            case Host.SYS_read:
                syscall.invokeSyscall("read", 3, "iii");
                break;
            case Host.SYS_lseek:
                syscall.invokeSyscall("lseek", 3, "iii");
                break;
            case Host.SYS_open:
                syscall.invokeSyscall("open", 3, "sii");
                break;
            case Host.SYS_close:
                syscall.invokeSyscall("close", 1, "i");
                break;
            case Host.SYS_fstat:
                syscall.invokeSyscall("fstat", 2, "ii");
                break;
            case Host.SYS_stat:
                syscall.invokeSyscall("stat", 2, "si");
                break;
            case Host.SYS_isatty:
                syscall.invokeSyscall("isatty", 1, "i");
                break;
            case Host.SYS_unlink:
                syscall.invokeSyscall("unlink", 1, "s");
                break;
            default:
                simulator.suspend();
                throw new UnsupportedSyscallException();
            }
            simulator.cpuWriteLong(simulator.getArg(0), syscall.syscallErrno);
            simulator.cpuWriteLong(0, syscall.syscallRetval);
        } catch (CPUException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (GDBServerException e)
        {
            e.printStackTrace();
        } catch (EndSessionException e)
        {
            e.printStackTrace();
        }
    }
    
    public boolean doneContinue()
    {
        if (!enterSyscall)
            return true;
        performSyscall();
        return false;
    }
    
    public int getSyscallArg(int i) throws CPUException
    {
        return simulator.getArg(i+2);
    }

    public void writeUART(int val)
    {
        System.out.print((char)val);
        System.out.flush();
        

        Packet p=new Packet(this);
        p.reply("O" + formatHex(val, "00"));
        p.sendReply();
        
        
    }
    public int readUART() throws CPUException
    {
        try
        {
            if (System.in.available()<=0)
            {
                throw new MemoryAccessException();
            }
            
            return System.in.read();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    public int readFIFO()
    {
        try
        {
            return System.in.available()>0?0:1;
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return 1;
    }

	public void halted()
	{
		// TODO Auto-generated method stub
		
	}

	public void running()
	{
		// TODO Auto-generated method stub
		
	}
}
OpenPOWER on IntegriCloud