55import api .messages .params .AnalyzeParams ;
66import api .messages .params .Params ;
77import com .ibm .wala .classLoader .Module ;
8+ import goblintserver .GoblintConfWatcher ;
89import goblintserver .GoblintServer ;
910import gobpie .GobPieConfiguration ;
1011import gobpie .GobPieException ;
2021import org .zeroturnaround .exec .InvalidExitValueException ;
2122import org .zeroturnaround .exec .ProcessExecutor ;
2223import org .zeroturnaround .exec .ProcessResult ;
23- import org .zeroturnaround .process .UnixProcess ;
24- import util .FileWatcher ;
2524
2625import java .io .File ;
2726import java .io .IOException ;
28- import java .nio .file .Path ;
2927import java .util .ArrayList ;
3028import java .util .Arrays ;
3129import java .util .Collection ;
5149
5250public class GoblintAnalysis implements ServerAnalysis {
5351
54- private final static int SIGINT = 2 ;
55-
5652 private final MagpieServer magpieServer ;
5753 private final GoblintServer goblintServer ;
5854 private final GoblintService goblintService ;
5955 private final GobPieConfiguration gobpieConfiguration ;
60- private final FileWatcher goblintConfWatcher ;
61-
62- private static boolean configValid = false ;
56+ private final GoblintConfWatcher goblintConfWatcher ;
6357 private static Future <?> lastAnalysisTask = null ;
6458
65- private final Logger log = LogManager .getLogger (GoblintAnalysis .class );
59+ //private final Logger log = LogManager.getLogger(GoblintAnalysis.class);
60+ private Logger log = LogManager .getLogger (GoblintAnalysis .class );
6661
6762
68- public GoblintAnalysis (MagpieServer magpieServer , GoblintServer goblintServer , GoblintService goblintService , GobPieConfiguration gobpieConfiguration ) {
63+
64+ public GoblintAnalysis (MagpieServer magpieServer , GoblintServer goblintServer , GoblintService goblintService , GobPieConfiguration gobpieConfiguration , GoblintConfWatcher goblintConfWatcher ) {
6965 this .magpieServer = magpieServer ;
7066 this .goblintServer = goblintServer ;
7167 this .goblintService = goblintService ;
7268 this .gobpieConfiguration = gobpieConfiguration ;
73- this .goblintConfWatcher = new FileWatcher ( Path . of ( gobpieConfiguration . getGoblintConf ())) ;
69+ this .goblintConfWatcher = goblintConfWatcher ;
7470 }
7571
7672
@@ -100,7 +96,7 @@ public void analyze(Collection<? extends Module> files, AnalysisConsumer consume
10096 return ;
10197 }
10298
103- if (!goblintServer .getGoblintRunProcess (). getProcess (). isAlive ()) {
99+ if (!goblintServer .isAlive ()) {
104100 // Goblint server has crashed. Exit GobPie because without the server no analysis is possible.
105101 magpieServer .exit ();
106102 return ;
@@ -109,29 +105,31 @@ public void analyze(Collection<? extends Module> files, AnalysisConsumer consume
109105 if (lastAnalysisTask != null && !lastAnalysisTask .isDone ()) {
110106 lastAnalysisTask .cancel (true );
111107 try {
112- abortAnalysis ();
108+ goblintServer . abortAnalysis ();
113109 log .info ("--------------- This analysis has been aborted -------------" );
114110 } catch (IOException e ) {
115111 log .error ("Aborting analysis failed." );
116112 }
117113 }
118114
119- refreshGoblintConfig ();
120-
121- if (!configValid ) {
115+ if (!goblintConfWatcher .refreshGoblintConfig ()) {
122116 return ;
123117 }
124118
125119 magpieServer .forwardMessageToClient (new MessageParams (MessageType .Info , source () + " started analyzing the code." ));
126-
127- preAnalyse ();
128-
120+ goblintServer .preAnalyse ();
129121 log .info ("---------------------- Analysis started ----------------------" );
122+
130123 lastAnalysisTask = reanalyse ().thenAccept (response -> {
124+ for (AnalysisResult analysisResult : response ) {
125+ System .out .println (analysisResult .toString ());
126+ }
131127 consumer .consume (new ArrayList <>(response ), source ());
128+
132129 log .info ("--------------------- Analysis finished ----------------------" );
133130 magpieServer .forwardMessageToClient (new MessageParams (MessageType .Info , source () + " finished analyzing the code." ));
134131 }).exceptionally (ex -> {
132+
135133 Throwable cause = ex instanceof CompletionException ? ex .getCause () : ex ;
136134 // TODO: handle closed socket exceptions:
137135 // org.eclipse.lsp4j.jsonrpc.JsonRpcException: java.net.SocketException: Broken pipe; errno=32
@@ -144,59 +142,6 @@ public void analyze(Collection<? extends Module> files, AnalysisConsumer consume
144142 }
145143
146144
147- /**
148- * Aborts the previous running analysis by sending a SIGINT signal to Goblint.
149- */
150- private void abortAnalysis () throws IOException {
151- Process goblintProcess = goblintServer .getGoblintRunProcess ().getProcess ();
152- int pid = Math .toIntExact (goblintProcess .pid ());
153- UnixProcess unixProcess = new UnixProcess (pid );
154- unixProcess .kill (SIGINT );
155- }
156-
157-
158- /**
159- * Reloads Goblint config if it has been changed or is currently invalid.
160- */
161- private void refreshGoblintConfig () {
162- if (goblintConfWatcher .checkModified () || !configValid ) {
163- configValid = goblintService .reset_config ()
164- .thenCompose (_res ->
165- goblintService .read_config (new Params (new File (gobpieConfiguration .getGoblintConf ()).getAbsolutePath ())))
166- .handle ((_res , ex ) -> {
167- if (ex != null ) {
168- Throwable cause = ex instanceof CompletionException ? ex .getCause () : ex ;
169- String msg = "Goblint was unable to successfully read the new configuration: " + cause .getMessage ();
170- magpieServer .forwardMessageToClient (new MessageParams (MessageType .Error , msg ));
171- log .error (msg );
172- return false ;
173- }
174- return true ;
175- })
176- .join ();
177- }
178- }
179-
180-
181- /**
182- * The method that is triggered before each analysis.
183- * <p>
184- * preAnalyzeCommand is read from the GobPie configuration file.
185- * Can be used for automating the compilation database generation.
186- */
187- private void preAnalyse () {
188- String [] preAnalyzeCommand = gobpieConfiguration .getPreAnalyzeCommand ();
189- if (preAnalyzeCommand != null ) {
190- try {
191- log .info ("Preanalyze command ran: \" " + Arrays .toString (preAnalyzeCommand ) + "\" " );
192- runCommand (new File (System .getProperty ("user.dir" )), preAnalyzeCommand );
193- log .info ("Preanalyze command finished." );
194- } catch (IOException | InvalidExitValueException | InterruptedException | TimeoutException e ) {
195- this .magpieServer .forwardMessageToClient (
196- new MessageParams (MessageType .Warning , "Running preanalysis command failed. " + e .getMessage ()));
197- }
198- }
199- }
200145
201146 /**
202147 * Sends the requests to Goblint server and gets their results.
@@ -207,7 +152,8 @@ private void preAnalyse() {
207152 * @return a CompletableFuture of a collection of warning messages and cfg code lenses if request was successful.
208153 * @throws GobPieException in case the analysis was aborted or returned a VerifyError.
209154 */
210- private CompletableFuture <Collection <AnalysisResult >> reanalyse () {
155+ public CompletableFuture <Collection <AnalysisResult >> reanalyse () {
156+ //return goblintService.analyze(new AnalyzeParams(true))
211157 return goblintService .analyze (new AnalyzeParams (!gobpieConfiguration .useIncrementalAnalysis ()))
212158 .thenCompose (this ::getComposedAnalysisResults );
213159 }
0 commit comments