summaryrefslogtreecommitdiffstats
path: root/zpu/sw/simulator/com/zylin/zpu/simulator/gdb/Packet.java
blob: ad6894787865a321b638095b6519328023dbac74 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Created on Nov 16, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.zylin.zpu.simulator.gdb;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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.NoAckException;
import com.zylin.zpu.simulator.exceptions.UnknownPacketException;


/** all packet related operations */
class Packet
{
	private final GDBServer server;
	
	Packet(GDBServer server)
	{
		this.server = server;
        reply=new StringBuffer();
	}
	
	void receive() throws IOException, GDBServerException, EndSessionException
	{
		int t;
		/* we spool until we see a $ */
		this.server.expect('$');
		
		StringBuffer packet=new StringBuffer();
	
		int cc=0;
		for (;;)
		{
			int t1;
			t1=this.server.read();
			t = t1;
			if (t==0x7d)
			{
				int t2;
				t2=this.server.read();
				/* the next char is escaped after a GDB specific scheme. See
				 * gdb/gdb/remote.c */
				t = t2;
				t^=0x20;
			} else
			{
				if (t=='#')
				{
					break;
				}
			}
			
			cc+=t;
			
			packet.append((char)t);
		}
		cc&=0xff;
		
		String checkSum;
		checkSum=""+(char)this.server.read()+(char)this.server.read();
		int readCheckSum;
		readCheckSum=Integer.parseInt(checkSum, 16);
		if (readCheckSum!=cc)
		{
			// error
			dumpHex(packet.toString());
			
			this.server.write("-".getBytes());
			throw new BadPacketException();
		} else
		{
			// ack
			this.server.write("+".getBytes());
		}
		
		cmd=packet.toString();
		this.server.print(GDBServer.PACKET, "Got " + number + ": #$" + cmd + "#" + checkSum); 
		origCmd=cmd;
	}
	
	void parseAndExecute() throws IOException, EndSessionException
	{
    	boolean silent=false;
	    try
		{
			if (checkPrefix("g"))
			{
				readRegisters();
			} else if (checkPrefix("?"))
			{
				querySignal();
			} else if (checkPrefix("s"))
			{
				doStep();
			} else if (checkPrefix("m"))
			{
				try
				{
					readMemory();
				} catch (CPUException e)
                {
                    silent=true; // happens all the time while hovering over variables in the GUI
                    throw e;
                }
			} else if (checkPrefix("c"))
			{
				continueExecution();
			} else if (checkPrefix("M"))
			{
				writeMemory();
			} else if (checkPrefix("z4"))
			{
				disableAccessWatchPoint();
			} else if (checkPrefix("Z4"))
			{
				enableAccessWatchPoint();
			} else if (checkPrefix("k"))
			{
				/* we must send a reply, but not wait for ack before we shut down
				   the connection.
				*/
			    server.alive=false;
			    reply("OK");
			} else
			{
				throw new UnknownPacketException();
			}
		} catch (UnknownPacketException e)
		{
			this.server.print(GDBServer.UNKNOWN, "Unknown packet: " + origCmd);
			// empty reply to unknown packets
		} catch (CPUException e)
        {
            if (!silent)
            {
                this.server.print(GDBServer.CPUEXCEPTION, "Exception handling GDB request");
                if (GDBServer.CPUEXCEPTION)
                {
                    e.printStackTrace();
                }
            }               
            reply("E01");
        } catch (GDBServerException e)
        {
            e.printStackTrace();
            reply("E01");
        }  catch (RuntimeException e)
		{
			e.printStackTrace();
			reply("E01");
		} 
        
		sendReply();
	}

	private void checkEmpty() throws GDBServerException
	{
		if (cmd.length()>0)
		{
			throw new GDBServerException();
		}
	}
	private void dumpHex(String arrayList2)
	{
		for (int i=0; i<arrayList2.length(); i++)
		{
			System.out.println(this.server.formatHex(arrayList2.charAt(i), "00"));
		}
	}
	/**
	 * @param packetString
	 * @param string
	 * @return
	 */
	private boolean checkPrefix(String string)
	{
		if (cmd.length() < string.length())
		{
			return false;
		} else
		{
			return cmd.substring(0, string.length()).equals(string);
		}
	}
	/**
	 * @throws GDBServerException
	 * @throws IOException 
	 * @throws MemoryAccessException
	 * 
	 */
	private void continueExecution() throws GDBServerException
	{
		extractString("c");
		try
		{
			if (!isEmpty())
			{
				int pc=extractInteger();
				this.server.simulator.setPc(pc);
			}
			checkEmpty();
			this.server.simulator.cont();
		} catch (MemoryAccessException e)
		{
			if (GDBServer.IGNOREDEXCEPTIONS)
			{
				e.printStackTrace();
			}
		}
		reply("S05");
	}
	private void doStep()
	{
		this.server.simulator.step();
		reply("S05");
	}
	protected int extractInteger() throws GDBServerException
	{
		String number;
		Pattern p=Pattern.compile("(\\-?[0-9a-fA-F]+)");
		Matcher m = p.matcher(cmd);
		if (!m.find())
		{
			throw new GDBServerException();
		}
		number=m.group();
		extractString(number);
		
		try
		{
			return (int)Long.parseLong(number, 16);
		} catch (NumberFormatException e)
		{
			throw new GDBServerException(e);
		}
	}
	/**
	 * @param string
	 * @throws GDBServerException
	 */
	private void extractString(String string) throws GDBServerException
	{
		if (!checkPrefix(string))
		{
			throw new GDBServerException();
		}
		stripStart(string.length());
	}
	/**
	 * @throws GDBServerException
	 * 
	 */
	private void readMemory() throws CPUException, GDBServerException
	{
		extractString("m");
		int address=extractInteger();
		extractString(",");
		int length=extractInteger();
		checkEmpty();
		
		for (int i=0; i<length; i++)
		{
			reply(this.server.formatHex(this.server.simulator.readByte(address+i), "00"));
		}
	}
	/**
	 * 
	 */
	private void readRegisters()  throws CPUException
	{
		for (int i=0; i<this.server.simulator.getREGNUM(); i++)
		{
			reply(this.server.printHex(this.server.simulator.getReg(i))); 
		}
	}
	private void writeMemory() throws GDBServerException, MemoryAccessException
	{
		extractString("M");
		int address=extractInteger();
		extractString(",");
		int length=extractInteger();
		extractString(":");
		
		for (int i=0; i<length; i++)
		{
			String t;
			t=cmd.substring(i*2, i*2+2);
			int val;
			val=Integer.parseInt(t, 16);
			this.server.simulator.writeByte(address+i, val);
		}
		reply("OK");
		
	}
	
	private void enableAccessWatchPoint() throws GDBServerException, CPUException 
	{
		extractString("Z4");
		extractString(",");
		int address=extractInteger();
		extractString(",");
		int length=extractInteger();
		checkEmpty();
		server.simulator.enableAccessWatchPoint(address, length);
		
		reply("OK");
	}

	private void disableAccessWatchPoint() throws GDBServerException, CPUException 
	{
		extractString("z4");
		extractString(",");
		int address=extractInteger();
		extractString(",");
		int length=extractInteger();
		checkEmpty();
		server.simulator.disableAccessWatchPoint(address, length);
		
		reply("OK");
	}

	
	private void stripStart(int length)
	{
		cmd=cmd.substring(length);
	}
	private boolean isEmpty() 
	{
		return cmd.length()==0;
	}
	/**
	 * @param string
	 */
	protected void reply(String string)
	{
		reply.append(string);
	}
	void sendReply()
	{
		// a bit easier to debug if we can see the entire string.
		StringBuffer buffer = new StringBuffer();
		buffer.append("$");
		number++;
		String replyString = reply.toString();
		buffer.append(replyString);
		byte[] data = replyString.toString().getBytes();
		int csum = 0;
		for (int i = 0; i < data.length; i++)
		{
			csum += data[i];
		}
		csum &= 0xff;
		buffer.append("#");
		String t = Integer.toHexString(csum);
		if (t.length() == 1)
		{
			t = "0" + t;
		}
		buffer.append(t);
		this.server.print(GDBServer.REPLY, "Reply " + number + " : " + buffer.toString());
		try
		{
			this.server.write(buffer.toString().getBytes());
		} catch (IOException e)
		{
			throw new RuntimeException(e);
		}
		
		if (server.alive)
		{
			/* check for ack. */
			int ack;
			try
			{
				ack = (char)this.server.read();
			} catch (IOException e)
			{
				throw new RuntimeException(e);
			}
			if (ack == '+')
			{
				return;
			}
			this.server.print(GDBServer.MINIMAL, "Retry");
			
			throw new NoAckException();
		}
		reply=new StringBuffer();
	}
	
	private void querySignal()
	{
		// SIGINT
		//	reply("S02");
		reply("S05");
	}
	protected String cmd;
	private int number;
	private StringBuffer reply;
	private String origCmd;
    int syscallErrno;
    int syscallRetval;


    
    void invokeSyscall(String string, int argNum, String types) throws IOException, NoAckException, GDBServerException, EndSessionException, CPUException
    {
        string="F"+string;
        
        int j=0;
        for (int i=0; i<argNum; i++)
        {
            string+=",";
            string+=Integer.toHexString(server.getSyscallArg(j));
            j++;
            if (types.charAt(i)=='s')
            {
                string+="/" + Integer.toHexString(server.getSyscallArg(j));
                j++;
            }
        }

        reply(string);
        sendReply();
        for (;;)
        {
            Packet packet=new Packet(server);
            packet.receive();
            if (packet.checkPrefix("F"))
            {
                /* this is the reply we are waiting for */
                packet.extractString("F");
                syscallRetval=packet.extractInteger();
                syscallErrno=0;
                if (packet.checkPrefix(","))
                {
                    /* errno is optional */
                    packet.extractString(",");
                    syscallErrno = packet.extractInteger();
                }
                break;
            } else
            {
                /* something else... */
                try
                {
                    packet.parseAndExecute();
                    packet.sendReply();
                } catch (IOException e)
                {
                    e.printStackTrace();
                } catch (EndSessionException e)
                {
                    e.printStackTrace();
                } catch (RuntimeException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}
OpenPOWER on IntegriCloud