-
Notifications
You must be signed in to change notification settings - Fork 19
/
OcamlDebugger.java
1554 lines (1335 loc) · 45.5 KB
/
OcamlDebugger.java
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ocaml.debugging;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ocaml.OcamlPlugin;
import ocaml.debugging.views.OcamlBreakpointsView;
import ocaml.debugging.views.OcamlCallStackView;
import ocaml.debugging.views.OcamlWatchView;
import ocaml.editors.OcamlEditor;
import ocaml.exec.ExecHelper;
import ocaml.exec.IExecEvents;
import ocaml.parser.Def;
import ocaml.parsers.OcamlNewInterfaceParser;
import ocaml.perspectives.OcamlDebugPerspective;
import ocaml.perspectives.OcamlPerspective;
import ocaml.preferences.PreferenceConstants;
import ocaml.util.FileUtil;
import ocaml.util.Misc;
import ocaml.util.OcamlPaths;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import beaver.Symbol;
/**
* Main class of the debugger. Manages the debugger state in a finite state automaton. Communicates
* with the OCaml debugger through its standard input and output.
*/
public class OcamlDebugger implements IExecEvents {
/** The possible states of the debugger */
public enum State {
NotStarted, Starting1, Starting1a, Starting1b, Starting1c, Starting1d, Starting2, Starting3, Starting3a, Starting4, Idle, Running, RunningBackwards, Stepping, BackStepping, SteppingOver, BackSteppingOver, Frame, SettingFrame, Quitting, PuttingBreakpoint, StepReturn, BackstepReturn, Displaying, BackTrace, RemovingBreakpoint, RemovingBreakpoints, RemovedBreakpoints, Restarting, DisplayingWatchVars, DisplayWatchVars
};
/** The current state of the debugger: idle, not started, stepping... */
private State state;
/** The project containing the debugged executable */
private IProject project;
/** list of paths of project */
private String[] projectPaths;
/** The debugger process, to which we send messages through its command line interface */
private ExecHelper debuggerProcess;
/** The debugged process */
private Process process;
/** The singleton instance of the ocaml debugger */
private static OcamlDebugger instance;
/** The debugger output since the last action */
private StringBuilder debuggerOutput;
/** process executable console */
private IOConsoleOutputStream console;
/** debugger error output console */
private IOConsoleOutputStream errorConsole;
/**
* A list of missing source files, so as to display an error message only once per missing file
*/
private ArrayList<String> missingSourceFiles = new ArrayList<String>();
/** To display the error message only once */
private boolean bDebuggingInfoMessage = true;
private ILaunch launch;
private File byteFile;
private File runFile;
private String[] args;
/** Indicates whether or not remote debugging is enabled. */
private boolean remoteDebugEnable;
/** The port on which to listen when remote debugging is enabled. */
private int remoteDebugPort;
private String scriptFile;
/** The list of variables to display in the "variables watch" after each step */
private ArrayList<String> watchVariables;
/** The answer of the debugger after sending "print var" for each variable */
private ArrayList<String> watchVariablesResult;
/** The index in <code>watchVariables</code> of the next variable to display */
private int iCurrentWatchVariable;
/** whether checkpoints are activated */
private boolean checkpoints;
/**
* private constructor (so that the user cannot create an instance of the debugger)
*
* @see #getInstance
*/
private OcamlDebugger() {
state = State.NotStarted;
watchVariables = new ArrayList<String>();
watchVariablesResult = new ArrayList<String>();
}
/** singleton design pattern */
public synchronized static OcamlDebugger getInstance() {
if (instance == null)
instance = new OcamlDebugger();
return instance;
}
/**
* Start the debugger
*
* @param ocamldebug
* the full path of the ocamldebug executable
* @param runFile
* the executable to run
* @param byteFile
* the bytecode file to debug
* @param project
* the project in which the executable is started
*
* @param launch
* the launch object, to which we add the started process
*
* @param remoteDebugEnable
* whether or not remote debugging should be enabled
*
* @param remoteDebugPort
* the port on which to listen if remote debugging is enabled
*
* @param scriptFile
* the script file to execute in debugger
*
* @param debuggerRootProject
* whether the debugger should be started from the project root instead of the executable folder
*/
public synchronized void start(
String ocamldebug, File runFile, File byteFile, IProject project, ILaunch launch,
String[] args, boolean remoteDebugEnable, int remoteDebugPort, String scriptFile, boolean debuggerRootProject)
{
this.runFile = runFile;
this.byteFile = byteFile;
this.args = args;
this.project = project;
OcamlPaths opaths = new OcamlPaths(project);
this.projectPaths = opaths.getPaths();
this.launch = launch;
this.remoteDebugEnable = remoteDebugEnable;
this.remoteDebugPort = remoteDebugPort;
this.scriptFile = scriptFile;
bDebuggingInfoMessage = true;
if (!state.equals(State.NotStarted)) {
message("The debugger is already started.");
return;
}
debuggerOutput = new StringBuilder();
if (!byteFile.exists() || !byteFile.isFile()) {
OcamlPlugin.logError("OcamlDebugger:start : not a file");
return;
}
try {
// System.err.println("starting debugger on " + exeFile.getAbsolutePath() + " in project
// " + project.getName());
state = State.Starting1;
missingSourceFiles.clear();
emptyBreakpointsView();
emptyCallStackView();
resetWatchVariables();
/*
* FIXME launch shortcuts on exe symbolic links don't work (debugger can't find other
* modules)
*/
// Build the command line arguments for ocamldebug
List<String> commandLineArgs = new ArrayList<String>();
commandLineArgs.add(ocamldebug);
if (remoteDebugEnable) {
commandLineArgs.add("-s");
commandLineArgs.add("0.0.0.0:" + remoteDebugPort);
}
OcamlPaths ocamlPaths = new OcamlPaths(project);
String[] paths = ocamlPaths.getPaths();
IPath projectLocation = project.getLocation();
for (String pathStr : paths) {
pathStr = pathStr.trim();
if (!".".equals(pathStr)) {
commandLineArgs.add("-I");
//These paths are either absolute or relative to the project
//directory. We must convert them to absolute paths in case
//we are running a bytecode within a nested directory.
Path path = Paths.get(pathStr);
if (!path.isAbsolute()) {
path = Paths.get(projectLocation.append(pathStr).toOSString());
}
commandLineArgs.add(path.toString());
}
}
// add the _build folder (for the cases making project by using Ocamlbuild)
String buildpath = projectLocation.append("_build").toOSString();
ArrayList<String> buildFolders = FileUtil.findSubdirectories(buildpath);
for (String path: buildFolders) {
commandLineArgs.add("-I");
commandLineArgs.add(path);
}
// add module Camlp4
commandLineArgs.add("-I");
commandLineArgs.add("+camlp4");
// add the root of the project
commandLineArgs.add("-I");
commandLineArgs.add(projectLocation.toOSString());
commandLineArgs.add(byteFile.getPath());
String[] commandLine = commandLineArgs.toArray(new String[commandLineArgs.size()]);
File debuggerRoot;
if(debuggerRootProject)
debuggerRoot = project.getLocation().toFile();
else
debuggerRoot = byteFile.getParentFile();
Process process = DebugPlugin.exec(commandLine, debuggerRoot);
debuggerProcess = ExecHelper.exec(this, process);
} catch (Exception e) {
OcamlPlugin.logError("ocaml plugin error", e);
state = State.NotStarted;
return;
}
}
public synchronized void kill() {
if (debuggerProcess != null) {
debuggerProcess.kill();
debuggerProcess = null;
}
if (process != null) {
process.destroy();
process = null;
}
remoteConnectionRequestDialog.close();
state = State.NotStarted;
}
public synchronized void run() {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
indicateRunningState("Running...");
state = State.Running;
send("run");
}
}
public synchronized void reverse() {
if (!checkStarted())
return;
if (!checkpoints) {
message("You cannot go back when checkpoints are disabled");
return;
}
if (state.equals(State.Idle)) {
indicateRunningState("Running backwards...");
state = State.RunningBackwards;
send("reverse");
}
}
public synchronized void restart() {
if (!checkStarted())
return;
if (!checkpoints) {
message("You cannot go back when checkpoints are disabled");
return;
}
if (state.equals(State.Idle)) {
emptyCallStackView();
resetWatchVariables();
state = State.Restarting;
send("goto 0");
}
}
public synchronized void step() {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
indicateRunningState("Stepping...");
state = State.Stepping;
send("step");
}
}
public synchronized void backstep() {
if (!checkStarted())
return;
if (!checkpoints) {
message("You cannot go back when checkpoints are disabled");
return;
}
if (state.equals(State.Idle)) {
indicateRunningState("Backstepping...");
state = State.BackStepping;
send("backstep");
}
}
public synchronized void stepOver() {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
indicateRunningState("Stepping over...");
state = State.SteppingOver;
send("next");
}
}
public synchronized void backstepOver() {
if (!checkStarted())
return;
if (!checkpoints) {
message("You cannot go back when checkpoints are disabled");
return;
}
if (state.equals(State.Idle)) {
indicateRunningState("Backstepping over...");
state = State.BackSteppingOver;
send("previous");
}
}
public synchronized void stepReturn() {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
indicateRunningState("Step-returning...");
state = State.StepReturn;
send("finish");
}
}
public synchronized void backstepReturn() {
if (!checkStarted())
return;
if (!checkpoints) {
message("You cannot go back when checkpoints are disabled");
return;
}
if (state.equals(State.Idle)) {
indicateRunningState("Backstep-returning...");
state = State.BackstepReturn;
send("start");
}
}
public synchronized void setFrame(int frame) {
if (!checkStarted())
return;
send("frame " + frame);
state = State.SettingFrame;
}
public synchronized void putBreakpointAt(String moduleName, int line, int column) {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
state = State.PuttingBreakpoint;
send("break @ " + moduleName + " " + (line + 1) + " " + (column + 1));
}
}
public void removeBreakpoint(int number) {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
state = State.RemovingBreakpoint;
send("delete " + number);
DebugMarkers.getInstance().removeBreakpoint(number);
refreshEditor();
}
}
public synchronized void removeBreakpoints() {
if (!checkStarted())
return;
if (state.equals(State.Idle)) {
state = State.RemovingBreakpoints;
send("delete");
}
}
public synchronized void quit() {
if (!checkStarted())
return;
if(state == State.Quitting){
// the user clicked twice on the "quit" button
kill();
}else{
state = State.Quitting;
send("quit");
}
}
public synchronized void displayWatchVars() {
if (!checkStarted())
return;
if (state.equals(State.Idle) || state.equals(State.DisplayWatchVars)) {
// obtainWatchVariables();
if (watchVariables.size() == 0)
return;
iCurrentWatchVariable = 0;
state = State.DisplayingWatchVars;
send("print " + watchVariables.get(iCurrentWatchVariable));
}
}
/** The result of evaluating a variable by sending "print" or "display" to the debugger */
private String displayExpression = "";
/**
* Ask the debugger to analyze the expression and return its value. This function is
* synchronous, so it is blocking until the debugger answers back, or 2000ms ellapsed.
*/
public synchronized String display(String expression) {
if (!checkStarted())
return "";
if(!Misc.isValidOcamlIdentifier(expression))
return "";
if (state.equals(State.Idle)) {
state = State.Displaying;
displayExpression = "";
send("print " + expression);
try {
/*
* The debugger thread will put the value in 'displayExpression' and call "notify()"
* as soon as it receives the value. Timeout after 2000ms (it means the debugger is
* stuck or busy)
*/
wait(2000);
} catch (InterruptedException e) {
OcamlPlugin.logError("unexpected thread interruption", e);
}
return displayExpression;
}
return "<debugger is busy>";
}
public synchronized boolean isStarted() {
return !(state.equals(State.NotStarted));
}
public synchronized boolean isReady() {
return state.equals(State.Idle);
}
public synchronized IProject getProject() {
return project;
}
public synchronized void processEnded(int exitValue) {
// System.err.println("ended with exit value " + exitValue);
state = State.NotStarted;
DebugMarkers.getInstance().clearCurrentPosition();
DebugMarkers.getInstance().removeBreakpoints();
emptyBreakpointsView();
emptyCallStackView();
resetWatchVariables();
refreshEditor();
showPerspective(OcamlPerspective.ID);
}
static final Pattern patternBindFailed = Pattern.compile("^Unix error : 'bind' failed :");
static final Pattern patternLostConnection = Pattern.compile("Lost connection with process \\d+");
public synchronized void processNewError(String error) {
if(!error.equals("done.\n"))
errorMessage(error);
if (bDebuggingInfoMessage) {
if (error.endsWith("has no debugging info.\n")) {
message("This executable has no debugging information, so it cannot be debugged. "
+ "To add debugging information, compile with the '-g' switch, " +
"or use a .d.byte target with ocamlbuild.");
bDebuggingInfoMessage = false;
state = State.Quitting;
send("quit");
return;
}
if (error.endsWith("is not a bytecode file.\n")) {
message("This executable is not a bytecode file so it cannot be debugged.");
bDebuggingInfoMessage = false;
state = State.Quitting;
send("quit");
return;
}
}
Matcher matcherBindFailed = patternBindFailed.matcher(error);
if (matcherBindFailed.find()) {
message("Unable to start a remote debugging session on port " +
remoteDebugPort + ", since that port is already in use. " +
"Please choose a different port in the launch configuration " +
"dialog and try again.");
bDebuggingInfoMessage = false;
state = State.Quitting;
send("quit");
return;
}
/* If the debugger lost the connection with its process, then we stop the debugger */
Matcher matcherLostConnection = patternLostConnection.matcher(error);
if (matcherLostConnection.find()) {
message("Lost connection with the debugged process.");
if (isStarted())
kill();
}
}
/**
* A message dialog that instructs the user to start the remote
* process they wish to debug.
*
* Invoking the {@link #open()} method will cause the dialog to
* be displayed. (Only one dialog can be displayed at any given
* time.)
*
* When the user starts the remote process, invoking the {@link
* #signalRemoteProcessConnected()} method will cause the dialog
* previously opened with {@link #open()} to disappear.
*
* However, if the user instead selects the cancel button, the
* thread originally spawned by {@link #open()} will invoke the
* {@link OcamlDebugger#kill()} method itself, thus immediately
* terminating the debugger session.
*/
private class RemoteConnectionRequestDialog {
/** Singleton reference to the message dialog box. */
private volatile MessageDialog dialog = null;
/** Whether or not a remote connection has been established. */
private volatile boolean remoteProcessConnected = false;
/** Opens the message dialog and waits for it to be closed. */
synchronized void open () {
// Do nothing if there is already an open message dialog.
if (dialog != null) {
OcamlPlugin.logError(
"Unexpected request to open the remote connection request dialog.");
return;
}
Display.getDefault().asyncExec(new Runnable () {
public void run() {
dialog = new MessageDialog(
null, "OCaml Debugger",
null, "Waiting for connection on port " + remoteDebugPort + "...\n",
MessageDialog.INFORMATION,
new String[] { IDialogConstants.CANCEL_LABEL }, 0
);
// Assume that no remote process will connect.
remoteProcessConnected = false;
// Wait for the dialog to be closed.
dialog.setBlockOnOpen(true);
dialog.open();
if (! remoteProcessConnected) {
// No remote process connected: the user selected cancel.
kill ();
}
}
});
}
/**
* Safely closes the message dialog if it is open.
*/
synchronized void close () {
Display.getDefault().asyncExec(new Runnable () {
public void run() {
if (dialog != null) {
dialog.close();
dialog = null;
}
}
});
}
/**
* Signals that a remote process has successfully connected,
* and closes the message dialog automatically.
*/
synchronized void signalRemoteProcessConnected () {
remoteProcessConnected = true;
close ();
}
}
/* Singleton reference to remote connection request dialog. */
private RemoteConnectionRequestDialog remoteConnectionRequestDialog =
new RemoteConnectionRequestDialog ();
public synchronized void processNewInput(String input) {
// System.out.print(input);
debuggerOutput.append(input);
String output = debuggerOutput.toString();
if (state.equals(State.Quitting)) {
if (output.endsWith("The program is running. Quit anyway ? (y or n) "))
send("y");
return;
}
else if (state.equals(State.RemovingBreakpoints)) {
if (output.endsWith("Delete all breakpoints ? (y or n) ")) {
state = State.RemovedBreakpoints;
// answer "yes" to the confirmation prompt
send("y");
debuggerOutput.setLength(0);
}
return;
} else if (state.equals(State.Starting3)) {
Pattern patternSocket = Pattern.compile("Loading program... \\n"
+ "Waiting for connection...\\(the socket is (.*?)\\)\\n");
Matcher matcher = patternSocket.matcher(output);
if (matcher.find()) {
if (scriptFile.length() > 0) {
state = State.Starting3a;
}
else {
state = State.Starting4;
}
if (remoteDebugEnable) {
remoteConnectionRequestDialog.open();
console = null;
errorConsole = null;
} else {
IProcess runprocess = loadProgram(matcher.group(1));
if (runprocess != null) {
IOConsole ioConsole = ((IOConsole) DebugUITools.getConsole(runprocess));
console = ioConsole.newOutputStream();
errorConsole = ioConsole.newOutputStream();
/* set the color and font; must be on the UI thread */
/* TODO: make this a preference */
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
public void run() {
console.setColor(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_MAGENTA));
console.setFontStyle(SWT.ITALIC);
errorConsole.setColor(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_RED));
}
});
} else {
state = State.NotStarted;
}
}
debuggerOutput.setLength(0);
}
return;
}
if (output.endsWith("(ocd) ")) {
// System.out.println("prompt");
if (state.equals(State.Stepping) || state.equals(State.BackStepping)
|| state.equals(State.SteppingOver) || state.equals(State.BackSteppingOver)
|| state.equals(State.Running) || state.equals(State.RunningBackwards)
|| state.equals(State.StepReturn) || state.equals(State.BackstepReturn)) {
if (!processMessage(output)) {
getFrame();
}
debuggerOutput.setLength(0);
} else if (state.equals(State.SettingFrame)) {
debuggerOutput.setLength(0);
getFrame();
} else if (state.equals(State.Starting1)) {
debuggerOutput.setLength(0);
state = State.Starting1a;
send("set loadingmode manual");
} else if (state.equals(State.Starting1a)) {
debuggerOutput.setLength(0);
state = State.Starting1b;
checkpoints = OcamlPlugin.getInstance().getPreferenceStore().getBoolean(
PreferenceConstants.P_DEBUGGER_CHECKPOINTS);
if (checkpoints)
send("set checkpoints on");
else
send("set checkpoints off");
} else if (state.equals(State.Starting1b)) {
debuggerOutput.setLength(0);
state = State.Starting1c;
int smallstep = OcamlPlugin.getInstance().getPreferenceStore().getInt(
PreferenceConstants.P_DEBUGGER_SMALL_STEP);
send("set smallstep " + smallstep);
} else if (state.equals(State.Starting1c)) {
debuggerOutput.setLength(0);
state = State.Starting1d;
int bigstep = OcamlPlugin.getInstance().getPreferenceStore().getInt(
PreferenceConstants.P_DEBUGGER_BIG_STEP);
send("set bigstep " + bigstep);
} else if (state.equals(State.Starting1d)) {
debuggerOutput.setLength(0);
state = State.Starting2;
int processcount = OcamlPlugin.getInstance().getPreferenceStore().getInt(
PreferenceConstants.P_DEBUGGER_PROCESS_COUNT);
send("set processcount " + processcount);
} else if (state.equals(State.Starting2)) {
debuggerOutput.setLength(0);
state = State.Starting3;
send("goto 0");
} else if (state.equals(State.Starting3a)) {
debuggerOutput.setLength(0);
send("source "+scriptFile);
state = State.Starting4;
} else if (state.equals(State.Starting4)) {
if (scriptFile.length() > 0) {
String strippedOutput = output;
int lastEolIndex = output.lastIndexOf("\n");
if (lastEolIndex >= 0)
strippedOutput = output.substring(0, lastEolIndex);
message(strippedOutput);
}
processMessage(output);
debuggerOutput.setLength(0);
if (remoteDebugEnable)
remoteConnectionRequestDialog.signalRemoteProcessConnected();
showPerspective(OcamlDebugPerspective.ID);
} else if (state.equals(State.Restarting)) {
debuggerOutput.setLength(0);
DebugMarkers.getInstance().clearCurrentPosition();
refreshEditor();
state = State.Idle;
}
else if (state.equals(State.Frame)) {
processFrame(output);
debuggerOutput.setLength(0);
// state = State.Idle;
state = State.BackTrace;
send("bt");
} else if (state.equals(State.BackTrace)) {
processCallStack(output);
debuggerOutput.setLength(0);
state = State.DisplayWatchVars;
displayWatchVars();
if (!state.equals(State.DisplayingWatchVars))
state = State.Idle;
} else if (state.equals(State.DisplayingWatchVars)) {
processWatchVar(output, iCurrentWatchVariable);
debuggerOutput.setLength(0);
iCurrentWatchVariable++;
if (iCurrentWatchVariable >= watchVariables.size()) {
// send the variables to the view
putWatchVariables();
state = State.Idle;
} else {
state = State.DisplayingWatchVars;
send("print " + watchVariables.get(iCurrentWatchVariable));
}
} else if (state.equals(State.PuttingBreakpoint)) {
processBreakpoint(output);
debuggerOutput.setLength(0);
state = State.Idle;
} else if (state.equals(State.RemovingBreakpoint)) {
debuggerOutput.setLength(0);
state = State.Idle;
} else if (state.equals(State.RemovedBreakpoints)) {
debuggerOutput.setLength(0);
DebugMarkers.getInstance().removeBreakpoints();
refreshEditor();
emptyBreakpointsView();
state = State.Idle;
}
/*
* This case happens only if there is no breakpoint to delete (and so the confirmation
* message isn't displayed)
*/
else if (state.equals(State.RemovingBreakpoints)) {
debuggerOutput.setLength(0);
state = State.Idle;
}
else if (state.equals(State.Displaying)) {
displayExpression = output.substring(0, output.length() - 6);
// Wake up the thread which asked for the expression to be displayed
notify();
debuggerOutput.setLength(0);
state = State.Idle;
}
else if (state.equals(State.Quitting)) {
// do nothing
}
else {
OcamlPlugin.logError("debugger: incoherent state (" + state + ")");
// System.err.println("###other###");
message("debugger internal error (incoherent state)");
}
}
}
private void getFrame() {
state = State.Frame;
send("frame");
}
private void processWatchVar(String output, int currentWatchVariable) {
// remove "(ocd) " from the end
String result = output.substring(0, output.length() - 6);
watchVariablesResult.set(currentWatchVariable, result);
}
private IProcess loadProgram(String socket) {
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add(runFile.getPath());
for(String arg : args)
commandLine.add(arg);
try {
ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
processBuilder.directory(runFile.getParentFile());
// add the CAML_DEBUG_SOCKET variable to the current environment
processBuilder.environment().put("CAML_DEBUG_SOCKET", socket);
// processBuilder.environment().put("OCAMLLIB", OcamlPlugin.getLibFullPath());
process = processBuilder.start();
return DebugPlugin.newProcess(launch, process, byteFile.getName());
} catch (Exception e) {
OcamlPlugin.logError("ocaml plugin error", e);
state = State.NotStarted;
message("couldn't start " + runFile.getName());
return null;
}
}
private void emptyBreakpointsView() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
final IWorkbenchPage activePage = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
OcamlBreakpointsView breakpointsview = (OcamlBreakpointsView) activePage
.findView(OcamlBreakpointsView.ID);
if (breakpointsview != null)
breakpointsview.removeAllBreakpoints();
} catch (Throwable e) {
OcamlPlugin.logError("ocaml plugin error", e);
}
}
});
}
private void emptyCallStackView() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
final IWorkbenchPage activePage = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
OcamlCallStackView callstackview = (OcamlCallStackView) activePage
.findView(OcamlCallStackView.ID);
if (callstackview != null)
callstackview.empty();
} catch (Throwable e) {
OcamlPlugin.logError("ocaml plugin error", e);
}
}
});
}
private void showPerspective(final String id) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
IWorkbench workbench = PlatformUI.getWorkbench();
try {
workbench.showPerspective(id, workbench.getActiveWorkbenchWindow());
} catch (WorkbenchException e) {
OcamlPlugin.logError("ocaml plugin error", e);
}
}
});
}
static final Pattern patternBeginning = Pattern.compile("Time\\s*:\\s+0\\nBeginning of program.\\n\\(ocd\\) ");
static final Pattern patternEnd = Pattern.compile("Time\\s*:\\s+\\d+\\nProgram (exit|end).\\n\\(ocd\\) ");
static final Pattern patternException = Pattern.compile("Time\\s*:\\s+\\d+\\nProgram end.\\nUncaught exception: (.*\\n)\\(ocd\\) ");