1 /***** spin: guided.c *****/
4 * This file is part of the public release of Spin. It is subject to the
5 * terms in the LICENSE file that is included in this source directory.
6 * Tool documentation is available at http://spinroot.com
10 #include <sys/types.h>
15 extern RunList *run, *X;
16 extern Element *Al_El;
17 extern Symbol *Fname, *oFname;
18 extern int verbose, lineno, xspin, jumpsteps, depth, merger, cutoff;
19 extern int nproc, nstop, Tval, ntrail, columns;
20 extern short Have_claim, Skip_claim, has_code;
21 extern void ana_src(int, int);
22 extern char **trailfilename;
26 static int lastclaim = -1;
28 static void lost_trail(void);
34 for (oX = run; oX; oX = oX->nxt)
36 { printf("(%s) ", oX->n->name);
42 newer(char *f1, char *f2)
44 #if defined(WIN32) || defined(WIN64)
50 if (stat(f1, (struct stat *)&x) < 0) return 0;
51 if (stat(f2, (struct stat *)&y) < 0) return 1;
52 if (x.st_mtime < y.st_mtime) return 0;
61 for (e = Al_El; e; e = e->Nxt)
63 && (e->n->ntyp == ATOMIC
64 || e->n->ntyp == NON_ATOMIC
65 || e->n->ntyp == D_STEP))
72 return (!Have_claim || !X || X->pid != 0);
75 int globmin = INT_MAX;
85 for (e = s->frst; e; e = e->nxt)
86 { if (e->status & 512)
91 if (e->n->ntyp == ATOMIC
92 || e->n->ntyp == NON_ATOMIC
93 || e->n->ntyp == D_STEP)
94 { int n = find_min(e->n->sl->this);
98 } else if (e->Seqno < s->minel)
99 { s->minel = e->Seqno;
101 for (l = e->sub; l; l = l->nxt)
102 { int n = find_min(l->this);
107 if (s->minel < globmin)
108 { globmin = s->minel;
114 find_max(Sequence *s)
116 if (s->last->Seqno > globmax)
117 { globmax = s->last->Seqno;
119 return s->last->Seqno;
129 { printf("spin: important:\n");
130 printf(" =======================================warning====\n");
131 printf(" this model contains embedded c code statements\n");
132 printf(" these statements will not be executed when the trail\n");
133 printf(" is replayed in this way -- they are just printed,\n");
134 printf(" which will likely lead to inaccurate variable values.\n");
135 printf(" for an accurate replay use: ./pan -r\n");
136 printf(" =======================================warning====\n\n");
140 * if source model name is leader.pml
141 * look for the trail file under these names:
149 { if (strlen(*trailfilename) < sizeof(snap))
150 { strcpy(snap, (const char *) *trailfilename);
152 { fatal("filename %s too long", *trailfilename);
156 sprintf(snap, "%s%d.trail", oFname->name, ntrail);
158 sprintf(snap, "%s.trail", oFname->name);
161 if ((fd = fopen(snap, "r")) == NULL)
162 { snap[strlen(snap)-2] = '\0'; /* .tra */
163 if ((fd = fopen(snap, "r")) == NULL)
164 { if ((q = strchr(oFname->name, '.')) != NULL)
167 sprintf(snap, "%s%d.trail",
168 oFname->name, ntrail);
170 sprintf(snap, "%s.trail",
174 if ((fd = fopen(snap, "r")) != NULL)
177 snap[strlen(snap)-2] = '\0'; /* last try */
178 if ((fd = fopen(snap, "r")) != NULL)
181 printf("spin: cannot find trail file\n");
185 if (xspin == 0 && newer(oFname->name, snap))
186 { printf("spin: warning, \"%s\" is newer than %s\n",
192 * sets Tval because timeouts may be part of trail
193 * this used to also set m_loss to 1, but that is
194 * better handled with the runtime -m flag
199 while (fscanf(fd, "%d:%d:%d\n", &depth, &pno, &nst) == 3)
202 { printf("starting claim %d\n", pno);
209 { printf("using statement merging\n");
218 dotag(stdout, " CYCLE>\n");
220 dotag(stdout, "<<<<<START OF CYCLE>>>>>\n");
225 if (cutoff > 0 && depth >= cutoff)
226 { printf("-------------\n");
227 printf("depth-limit (-u%d steps) reached\n", cutoff);
231 if (Skip_claim && pno == 0) continue;
233 for (dothis = Al_El; dothis; dothis = dothis->Nxt)
234 { if (dothis->Seqno == nst)
238 { printf("%3d: proc %d, no matching stmnt %d\n",
239 depth, pno - Have_claim, nst);
243 i = nproc - nstop + Skip_claim;
245 if (dothis->n->ntyp == '@')
251 { dotag(stdout, "<end>\n");
254 if (Have_claim && pno == 0)
255 printf("%3d: claim terminates\n",
258 printf("%3d: proc %d terminates\n",
259 depth, pno - Have_claim);
263 if (pno <= 1) continue; /* init dies before never */
264 printf("%3d: stop error, ", depth);
265 printf("proc %d (i=%d) trans %d, %c\n",
266 pno - Have_claim, i, nst, dothis->n->ntyp);
270 if (0 && !xspin && (verbose&32))
271 { printf("step %d i=%d pno %d stmnt %d\n", depth, i, pno, nst);
274 for (X = run; X; X = X->nxt)
281 { printf("%3d: no process %d (stmnt %d)\n", depth, pno - Have_claim, nst);
282 printf(" max %d (%d - %d + %d) claim %d ",
283 nproc - nstop + Skip_claim,
284 nproc, nstop, Skip_claim, Have_claim);
285 printf("active processes:\n");
286 for (X = run; X; X = X->nxt)
287 { printf("\tpid %d\tproctype %s\n", X->pid, X->n->name);
292 { printf("%3d:\tproc %d (?) ", depth, pno);
296 { int min_seq = find_min(X->ps);
297 int max_seq = find_max(X->ps);
300 if (nst < min_seq || nst > max_seq)
301 { printf("%3d: error: invalid statement", depth);
303 { printf(": pid %d:%d (%s:%d:%d) stmnt %d (valid range %d .. %d)",
304 pno, X->pid, X->n->name, X->tn, X->b, nst, min_seq, max_seq);
313 lineno = dothis->n->ln;
314 Fname = dothis->n->fn;
316 if (dothis->n->ntyp == D_STEP)
317 { Element *g, *og = dothis;
320 if (g && depth >= jumpsteps
321 && ((verbose&32) || ((verbose&4) && not_claim())))
325 if (og->n->ntyp == D_STEP)
326 og = og->n->sl->this->frst;
329 comment(stdout, og->n, 0);
332 if (verbose&1) dumpglobals();
333 if (verbose&2) dumplocal(X);
334 if (xspin) printf("\n");
337 } while (g && g != dothis->nxt);
339 { X->pc = g?huntele(g, 0, -1):g;
343 keepgoing: if (dothis->merge_start)
344 a = dothis->merge_start;
349 { X->pc = eval_sub(dothis);
350 if (X->pc) X->pc = huntele(X->pc, 0, a);
353 if (depth >= jumpsteps
354 && ((verbose&32) || ((verbose&4) && not_claim()))) /* -v or -p */
358 if (dothis->n->ntyp == D_STEP)
359 dothis = dothis->n->sl->this->frst;
362 comment(stdout, dothis->n, 0);
364 if (a && (verbose&32))
365 printf("\t<merge %d now @%d>",
367 (X && X->pc)?X->pc->seqno:-1);
370 if (verbose&1) dumpglobals();
371 if (verbose&2) dumplocal(X);
372 if (xspin) printf("\n");
376 printf("\ttransition failed\n");
377 a = 0; /* avoid inf loop */
380 if (a && X && X->pc && X->pc->seqno != a)
385 if (Have_claim && X && X->pid == 0
387 && lastclaim != dothis->n->ln)
388 { lastclaim = dothis->n->ln;
391 sprintf(t, "#%d", lastclaim);
395 printf("Never claim moves to line %d\t[", lastclaim);
396 comment(stdout, dothis->n, 0);
399 printf("spin: trail ends after %d steps\n", depth);
407 while (fscanf(fd, "%d:%d:%d:%d\n", &d, &p, &n, &l) == 4)
408 { printf("step %d: proc %d ", d, p); whichproc(p);
409 printf("(state %d) - d %d\n", n, l);
411 wrapup(1); /* no return */
416 { int i = nproc - nstop;
420 for (Y = run; Y; Y = Y->nxt)