guided.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /***** spin: guided.c *****/
  2. /* Copyright (c) 1989-2003 by Lucent Technologies, Bell Laboratories. */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* No guarantee whatsoever is expressed or implied by the distribution of */
  5. /* this code. Permission is given to distribute this code provided that */
  6. /* this introductory message is not removed and no monies are exchanged. */
  7. /* Software written by Gerard J. Holzmann. For tool documentation see: */
  8. /* http://spinroot.com/ */
  9. /* Send all bug-reports and/or questions to: bugs@spinroot.com */
  10. #include "spin.h"
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include "y.tab.h"
  14. extern RunList *run, *X;
  15. extern Element *Al_El;
  16. extern Symbol *Fname, *oFname;
  17. extern int verbose, lineno, xspin, jumpsteps, depth, merger, cutoff;
  18. extern int nproc, nstop, Tval, ntrail, columns;
  19. extern short Have_claim, Skip_claim;
  20. extern void ana_src(int, int);
  21. extern char **trailfilename;
  22. int TstOnly = 0, pno;
  23. static int lastclaim = -1;
  24. static FILE *fd;
  25. static void lost_trail(void);
  26. static void
  27. whichproc(int p)
  28. { RunList *oX;
  29. for (oX = run; oX; oX = oX->nxt)
  30. if (oX->pid == p)
  31. { printf("(%s) ", oX->n->name);
  32. break;
  33. }
  34. }
  35. static int
  36. newer(char *f1, char *f2)
  37. {
  38. #if defined(WIN32) || defined(WIN64)
  39. struct _stat x, y;
  40. #else
  41. struct stat x, y;
  42. #endif
  43. if (stat(f1, (struct stat *)&x) < 0) return 0;
  44. if (stat(f2, (struct stat *)&y) < 0) return 1;
  45. if (x.st_mtime < y.st_mtime) return 0;
  46. return 1;
  47. }
  48. void
  49. hookup(void)
  50. { Element *e;
  51. for (e = Al_El; e; e = e->Nxt)
  52. if (e->n
  53. && (e->n->ntyp == ATOMIC
  54. || e->n->ntyp == NON_ATOMIC
  55. || e->n->ntyp == D_STEP))
  56. (void) huntstart(e);
  57. }
  58. int
  59. not_claim(void)
  60. {
  61. return (!Have_claim || !X || X->pid != 0);
  62. }
  63. void
  64. match_trail(void)
  65. { int i, a, nst;
  66. Element *dothis;
  67. char snap[512], *q;
  68. /*
  69. * if source model name is leader.pml
  70. * look for the trail file under these names:
  71. * leader.pml.trail
  72. * leader.pml.tra
  73. * leader.trail
  74. * leader.tra
  75. */
  76. if (trailfilename)
  77. { if (strlen(*trailfilename) < sizeof(snap))
  78. { strcpy(snap, (const char *) *trailfilename);
  79. } else
  80. { fatal("filename %s too long", *trailfilename);
  81. }
  82. } else
  83. { if (ntrail)
  84. sprintf(snap, "%s%d.trail", oFname->name, ntrail);
  85. else
  86. sprintf(snap, "%s.trail", oFname->name);
  87. }
  88. if ((fd = fopen(snap, "r")) == NULL)
  89. { snap[strlen(snap)-2] = '\0'; /* .tra */
  90. if ((fd = fopen(snap, "r")) == NULL)
  91. { if ((q = strchr(oFname->name, '.')) != NULL)
  92. { *q = '\0';
  93. if (ntrail)
  94. sprintf(snap, "%s%d.trail",
  95. oFname->name, ntrail);
  96. else
  97. sprintf(snap, "%s.trail",
  98. oFname->name);
  99. *q = '.';
  100. if ((fd = fopen(snap, "r")) != NULL)
  101. goto okay;
  102. snap[strlen(snap)-2] = '\0'; /* last try */
  103. if ((fd = fopen(snap, "r")) != NULL)
  104. goto okay;
  105. }
  106. printf("spin: cannot find trail file\n");
  107. alldone(1);
  108. } }
  109. okay:
  110. if (xspin == 0 && newer(oFname->name, snap))
  111. printf("spin: warning, \"%s\" is newer than %s\n",
  112. oFname->name, snap);
  113. Tval = 1;
  114. /*
  115. * sets Tval because timeouts may be part of trail
  116. * this used to also set m_loss to 1, but that is
  117. * better handled with the runtime -m flag
  118. */
  119. hookup();
  120. while (fscanf(fd, "%d:%d:%d\n", &depth, &pno, &nst) == 3)
  121. { if (depth == -2) { start_claim(pno); continue; }
  122. if (depth == -4) { merger = 1; ana_src(0, 1); continue; }
  123. if (depth == -1)
  124. { if (verbose)
  125. { if (columns == 2)
  126. dotag(stdout, " CYCLE>\n");
  127. else
  128. dotag(stdout, "<<<<<START OF CYCLE>>>>>\n");
  129. }
  130. continue;
  131. }
  132. if (cutoff > 0 && depth >= cutoff)
  133. { printf("-------------\n");
  134. printf("depth-limit (-u%d steps) reached\n", cutoff);
  135. break;
  136. }
  137. if (Skip_claim && pno == 0) continue;
  138. for (dothis = Al_El; dothis; dothis = dothis->Nxt)
  139. { if (dothis->Seqno == nst)
  140. break;
  141. }
  142. if (!dothis)
  143. { printf("%3d: proc %d, no matching stmnt %d\n",
  144. depth, pno - Have_claim, nst);
  145. lost_trail();
  146. }
  147. i = nproc - nstop + Skip_claim;
  148. if (dothis->n->ntyp == '@')
  149. { if (pno == i-1)
  150. { run = run->nxt;
  151. nstop++;
  152. if (verbose&4)
  153. { if (columns == 2)
  154. { dotag(stdout, "<end>\n");
  155. continue;
  156. }
  157. if (Have_claim && pno == 0)
  158. printf("%3d: claim terminates\n",
  159. depth);
  160. else
  161. printf("%3d: proc %d terminates\n",
  162. depth, pno - Have_claim);
  163. }
  164. continue;
  165. }
  166. if (pno <= 1) continue; /* init dies before never */
  167. printf("%3d: stop error, ", depth);
  168. printf("proc %d (i=%d) trans %d, %c\n",
  169. pno - Have_claim, i, nst, dothis->n->ntyp);
  170. lost_trail();
  171. }
  172. if (!xspin && (verbose&32))
  173. { printf("i=%d pno %d\n", i, pno);
  174. }
  175. for (X = run; X; X = X->nxt)
  176. { if (--i == pno)
  177. break;
  178. }
  179. if (!X)
  180. { if (verbose&32)
  181. { printf("%3d: no process %d (step %d)\n", depth, pno - Have_claim, nst);
  182. printf(" max %d (%d - %d + %d) claim %d",
  183. nproc - nstop + Skip_claim,
  184. nproc, nstop, Skip_claim, Have_claim);
  185. printf("active processes:\n");
  186. for (X = run; X; X = X->nxt)
  187. { printf("\tpid %d\tproctype %s\n", X->pid, X->n->name);
  188. }
  189. printf("\n");
  190. continue;
  191. } else
  192. { printf("%3d:\tproc %d (?) ", depth, pno);
  193. lost_trail();
  194. }
  195. } else
  196. { X->pc = dothis;
  197. }
  198. lineno = dothis->n->ln;
  199. Fname = dothis->n->fn;
  200. if (dothis->n->ntyp == D_STEP)
  201. { Element *g, *og = dothis;
  202. do {
  203. g = eval_sub(og);
  204. if (g && depth >= jumpsteps
  205. && ((verbose&32) || ((verbose&4) && not_claim())))
  206. { if (columns != 2)
  207. { p_talk(og, 1);
  208. if (og->n->ntyp == D_STEP)
  209. og = og->n->sl->this->frst;
  210. printf("\t[");
  211. comment(stdout, og->n, 0);
  212. printf("]\n");
  213. }
  214. if (verbose&1) dumpglobals();
  215. if (verbose&2) dumplocal(X);
  216. if (xspin) printf("\n");
  217. }
  218. og = g;
  219. } while (g && g != dothis->nxt);
  220. if (X != NULL)
  221. { X->pc = g?huntele(g, 0, -1):g;
  222. }
  223. } else
  224. {
  225. keepgoing: if (dothis->merge_start)
  226. a = dothis->merge_start;
  227. else
  228. a = dothis->merge;
  229. if (X != NULL)
  230. { X->pc = eval_sub(dothis);
  231. if (X->pc) X->pc = huntele(X->pc, 0, a);
  232. }
  233. if (depth >= jumpsteps
  234. && ((verbose&32) || ((verbose&4) && not_claim()))) /* -v or -p */
  235. { if (columns != 2)
  236. { p_talk(dothis, 1);
  237. if (dothis->n->ntyp == D_STEP)
  238. dothis = dothis->n->sl->this->frst;
  239. printf("\t[");
  240. comment(stdout, dothis->n, 0);
  241. printf("]");
  242. if (a && (verbose&32))
  243. printf("\t<merge %d now @%d>",
  244. dothis->merge,
  245. (X && X->pc)?X->pc->seqno:-1);
  246. printf("\n");
  247. }
  248. if (verbose&1) dumpglobals();
  249. if (verbose&2) dumplocal(X);
  250. if (xspin) printf("\n");
  251. if (X && !X->pc)
  252. { X->pc = dothis;
  253. printf("\ttransition failed\n");
  254. a = 0; /* avoid inf loop */
  255. }
  256. }
  257. if (a && X && X->pc && X->pc->seqno != a)
  258. { dothis = X->pc;
  259. goto keepgoing;
  260. } }
  261. if (Have_claim && X && X->pid == 0
  262. && dothis->n
  263. && lastclaim != dothis->n->ln)
  264. { lastclaim = dothis->n->ln;
  265. if (columns == 2)
  266. { char t[128];
  267. sprintf(t, "#%d", lastclaim);
  268. pstext(0, t);
  269. } else
  270. {
  271. printf("Never claim moves to line %d\t[", lastclaim);
  272. comment(stdout, dothis->n, 0);
  273. printf("]\n");
  274. } } }
  275. printf("spin: trail ends after %d steps\n", depth);
  276. wrapup(0);
  277. }
  278. static void
  279. lost_trail(void)
  280. { int d, p, n, l;
  281. while (fscanf(fd, "%d:%d:%d:%d\n", &d, &p, &n, &l) == 4)
  282. { printf("step %d: proc %d ", d, p); whichproc(p);
  283. printf("(state %d) - d %d\n", n, l);
  284. }
  285. wrapup(1); /* no return */
  286. }
  287. int
  288. pc_value(Lextok *n)
  289. { int i = nproc - nstop;
  290. int pid = eval(n);
  291. RunList *Y;
  292. for (Y = run; Y; Y = Y->nxt)
  293. { if (--i == pid)
  294. return Y->pc->seqno;
  295. }
  296. return 0;
  297. }