Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e2f8a358afb1d6af797c391d1a6ecc846e5b09f (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
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
#!perl.exe

use strict;
use warnings;
use IO::Socket;

#####################################################################
# Copyright (c) 2004-2005 IBM Corporation 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:
#     Bjorn Freeman-Benson - initial API and implementation
#####################################################################
#
# This test is designed to run on Windows:
#
# cd c:\eclipse\workspace\org.eclipse.debug.examples.core
# perl pdavm\tests\vmtests.pl
#
# If the tests fail, they often indicate that by hanging in an
# infinite loop. Additionally, the vm under test often becomes
# a 100% CPU usage zombie. Use the task manager to kill them.
#
my $socket1;
my $socket2;

sub expect_output {
	my $expect = shift;
	my $line = <PROGRAM_OUTPUT>;
	chomp($line);
	return if( $line eq $expect );
	die "expected output: $expect\nSaw output: $line";
}
sub expect_output_eof {
	my $line = <PROGRAM_OUTPUT>;
	return if( !defined $line );
	die "expected: EOF on output";
}
sub send_command {
	my $string = shift;
	my $expect = shift;
	$expect = "ok" if( !defined $expect );
	#print STDERR "SEND: $string\n";
	print $socket1 "$string\n";
	my $result = <$socket1>;
	chomp($result);
	#print STDERR "RESULT: $result\n";
	die "sent: $string\nexpected: $expect\nsaw:      $result" if( !($result eq $expect) );
}
sub expect_event {
	my $string = shift;
	my $event = <$socket2>;
	chomp($event);
	#print STDERR "EVENT: $event\n";
	die "expected event: $string\nsaw event: $event" if( !($string eq $event) );
}
sub setup_sockets {
	#print STDERR "calling socket 12345\n";
	$socket1 = IO::Socket::INET->new(
			Proto    => "tcp",
			PeerAddr => "localhost",
			PeerPort => "12345",
			Timeout  => 10,
		    )
		  or die "cannot connect to debug socket 12345";
	#print STDERR "calling socket 12346\n";
	$socket2 = IO::Socket::INET->new(
			Proto    => "tcp",
			PeerAddr => "localhost",
			PeerPort => "12346",
			Timeout  => 10,
		    )
		  or die "cannot connect to debug socket 12346";
	#print STDERR "done calling sockets\n";
}

sub test2 {
	print "test2 (common debug commands)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest2.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("10");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		# test step
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		# test breakpoint
		send_command("set 4");
		send_command("data", "6|");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended breakpoint 4");
		# test data stack
		send_command("data", "6|7|8|9|");
		send_command("popdata");
		send_command("data", "6|7|8|");
		send_command("pushdata 11");
		send_command("data", "6|7|8|11|");
		send_command("setdata 1 2");
		send_command("data", "6|2|8|11|");
		# test call stack
		send_command("set 12");
		send_command("set 19");
		send_command("stepreturn");
		expect_event("resumed client");
		expect_event("suspended breakpoint 12");
		send_command("clear 19");
		send_command("stack", "pdavm\\tests\\vmtest2.pda|6|main#pdavm\\tests\\vmtest2.pda|18|sub1|m|n#pdavm\\tests\\vmtest2.pda|12|sub2" );
		send_command("stepreturn");
		expect_event("resumed client");
		expect_event("suspended step");
		send_command("stack", "pdavm\\tests\\vmtest2.pda|6|main#pdavm\\tests\\vmtest2.pda|18|sub1|m|n#pdavm\\tests\\vmtest2.pda|13|sub2" );
		send_command("stepreturn");
		expect_event("resumed client");
		expect_event("suspended step");
		send_command("stack", "pdavm\\tests\\vmtest2.pda|6|main#pdavm\\tests\\vmtest2.pda|22|sub1|m|n" );
		send_command("set 6");
		send_command("stepreturn");
		expect_event("resumed client");
		expect_event("suspended breakpoint 6");
		# test set and clear
		send_command("set 27");
		send_command("set 29");
		send_command("set 33");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended breakpoint 33");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended breakpoint 27");
		send_command("clear 33");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended breakpoint 29");
		# test var and setvar
		send_command("set 47");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended breakpoint 47");
		send_command("var 1 b", "4");
		send_command("var 2 b", "2");
		send_command("var 1 a", "0");
		send_command("setvar 1 a 99");
		send_command("data", "6|2|8|11|27|1|4|");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("var 1 a", "99");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("data", "6|2|8|11|27|1|4|99|");
		# test exit
		send_command("exit");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test2..SUCCESS\n";
}

sub test3 {
	print "test3 (uncaught events)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest3.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("10");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		send_command("resume");
		expect_event("resumed client");
		expect_event("unimplemented instruction foobar");
		expect_event("no such label zippy");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test3..SUCCESS\n";
}
sub test4 {
	print "test4 (caught events)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest3.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("10");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		send_command("eventstop unimpinstr 1");
		send_command("resume");
		expect_event("resumed client");
		expect_event("unimplemented instruction foobar");
		expect_event("suspended event unimpinstr");
		send_command("eventstop unimpinstr 0");
		send_command("resume");
		expect_event("resumed client");
		expect_event("unimplemented instruction foobar");
		expect_event("no such label zippy");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test4..SUCCESS\n";
}
sub test5 {
	print "test5 (caught events)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest3.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("10");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		send_command("eventstop nosuchlabel 1");
		send_command("resume");
		expect_event("resumed client");
		expect_event("unimplemented instruction foobar");
		expect_event("no such label zippy");
		expect_event("suspended event nosuchlabel");
		send_command("eventstop nosuchlabel 0");
		send_command("resume");
		expect_event("resumed client");
		expect_event("no such label zippy");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test5..SUCCESS\n";
}
sub test6 {
	print "test6 (watch points)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest6.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("8");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		send_command("watch inner::a 1");
		send_command("watch main::a 2");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended watch write main::a");
		send_command("stack", "pdavm\\tests\\vmtest6.pda|4|main|a|b");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended watch read inner::a");
		send_command("stack", "pdavm\\tests\\vmtest6.pda|10|main|a|b#pdavm\\tests\\vmtest6.pda|25|inner|a|c");
		send_command("watch inner::a 0");
		send_command("resume");
		expect_event("resumed client");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test6..SUCCESS\n";
}
sub test7 {
	print "test7 (eval)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest6.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("8");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		send_command("set 25");
		send_command("resume");
		expect_event("resumed client");
		expect_event("suspended breakpoint 25");
		#
		send_command("eval push%204|push%205|add");
		expect_event("resumed client");
		expect_event("evalresult 9");
		expect_event("suspended eval");
		#
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("stack", "pdavm\\tests\\vmtest6.pda|10|main|a|b#pdavm\\tests\\vmtest6.pda|26|inner|a|c");
 		send_command("data", "4|4|");
 		send_command("eval call%20other");
		expect_event("resumed client");
		expect_event("evalresult 15");
		expect_event("suspended eval");
		send_command("stack", "pdavm\\tests\\vmtest6.pda|10|main|a|b#pdavm\\tests\\vmtest6.pda|26|inner|a|c");
 		send_command("data", "4|4|");
		send_command("resume");
		expect_event("resumed client");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test7..SUCCESS\n";
}
sub test1 {
	print "test1 (normal run mode)..\n";
	open PROGRAM_OUTPUT, "perl pdavm\\pda.pl examples\\example.pda |" or die $!;
	expect_output("\"hello\"");
	expect_output("\"barfoo\"");
	expect_output("\"first\"");
	expect_output("\"second\"");
	expect_output("12");
	expect_output("11");
	expect_output("10");
	expect_output("\"barfoo\"");
	expect_output("\"first\"");
	expect_output("\"second\"");
	expect_output("\"end\"");
	expect_output_eof();
	print "test1..SUCCESS\n";
}
sub test8 {
	print "test8 (drop to frame)..\n";
	
	my $kidpid;
	die "can't fork: $!" unless defined($kidpid = fork());
	if( $kidpid ) {	
		#print STDERR "starting program\n";
		open PROGRAM_OUTPUT, "perl pdavm\\pda.pl pdavm\\tests\\vmtest8.pda -debug 12345 12346 |";
		#print STDERR "done starting program\n";
		expect_output("-debug 12345 12346");
		expect_output("debug connection accepted");
		expect_output("1");
		expect_output_eof();
		exit 0;
	} else {
		setup_sockets();
		expect_event("started");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("stack", "pdavm\\tests\\vmtest8.pda|2|main|a#pdavm\\tests\\vmtest8.pda|8|inner|b#pdavm\\tests\\vmtest8.pda|12|inner2|c");
		send_command("drop");
		expect_event("suspended drop");
		send_command("stack", "pdavm\\tests\\vmtest8.pda|2|main|a#pdavm\\tests\\vmtest8.pda|7|inner|b");
		send_command("step");
		expect_event("resumed step");
		expect_event("suspended step");
		send_command("stack", "pdavm\\tests\\vmtest8.pda|2|main|a#pdavm\\tests\\vmtest8.pda|8|inner|b#pdavm\\tests\\vmtest8.pda|10|inner2");
		send_command("resume");
		expect_event("resumed client");
		expect_event("terminated");
	}
	#print STDERR "waiting for child\n";
	wait();
	#print STDERR "child joined\n";
	close PROGRAM_OUTPUT;
	print "test8..SUCCESS\n";
}

#
# Run the tests
#
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
print "All tests complete\n";

Back to the top