guided.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. int TstOnly = 0, pno;
  22. static int lastclaim = -1;
  23. static FILE *fd;
  24. static void lost_trail(void);
  25. static void
  26. whichproc(int p)
  27. { RunList *oX;
  28. for (oX = run; oX; oX = oX->nxt)
  29. if (oX->pid == p)
  30. { printf("(%s) ", oX->n->name);
  31. break;
  32. }
  33. }
  34. static int
  35. newer(char *f1, char *f2)
  36. {
  37. #if defined(WIN32) || defined(WIN64)
  38. struct _stat x, y;
  39. #else
  40. struct stat x, y;
  41. #endif
  42. if (stat(f1, (struct stat *)&x) < 0) return 0;
  43. if (stat(f2, (struct stat *)&y) < 0) return 1;
  44. if (x.st_mtime < y.st_mtime) return 0;
  45. return 1;
  46. }
  47. void
  48. hookup(void)
  49. { Element *e;
  50. for (e = Al_El; e; e = e->Nxt)
  51. if (e->n
  52. && (e->n->ntyp == ATOMIC
  53. || e->n->ntyp == NON_ATOMIC
  54. || e->n->ntyp == D_STEP))
  55. (void) huntstart(e);
  56. }
  57. int
  58. not_claim(void)
  59. {
  60. return (!Have_claim || !X || X->pid != 0);
  61. }
  62. void
  63. match_trail(void)
  64. { int i, a, nst;
  65. Element *dothis;
  66. char snap[512], *q;
  67. /*
  68. * if source model name is leader.pml
  69. * look for the trail file under these names:
  70. * leader.pml.trail
  71. * leader.pml.tra
  72. * leader.trail
  73. * leader.tra
  74. */
  75. if (ntrail)
  76. sprintf(snap, "%s%d.trail", oFname->name, ntrail);
  77. else
  78. sprintf(snap, "%s.trail", oFname->name);
  79. if ((fd = fopen(snap, "r")) == NULL)
  80. { snap[strlen(snap)-2] = '\0'; /* .tra */
  81. if ((fd = fopen(snap, "r")) == NULL)
  82. { if ((q = strchr(oFname->name, '.')) != NULL)
  83. { *q = '\0';
  84. if (ntrail)
  85. sprintf(snap, "%s%d.trail",
  86. oFname->name, ntrail);
  87. else
  88. sprintf(snap, "%s.trail",
  89. oFname->name);
  90. *q = '.';
  91. if ((fd = fopen(snap, "r")) != NULL)
  92. goto okay;
  93. snap[strlen(snap)-2] = '\0'; /* last try */
  94. if ((fd = fopen(snap, "r")) != NULL)
  95. goto okay;
  96. }
  97. printf("spin: cannot find trail file\n");
  98. alldone(1);
  99. } }
  100. okay:
  101. if (xspin == 0 && newer(oFname->name, snap))
  102. printf("spin: warning, \"%s\" is newer than %s\n",
  103. oFname->name, snap);
  104. Tval = 1;
  105. /*
  106. * sets Tval because timeouts may be part of trail
  107. * this used to also set m_loss to 1, but that is
  108. * better handled with the runtime -m flag
  109. */
  110. hookup();
  111. while (fscanf(fd, "%d:%d:%d\n", &depth, &pno, &nst) == 3)
  112. { if (depth == -2) { start_claim(pno); continue; }
  113. if (depth == -4) { merger = 1; ana_src(0, 1); continue; }
  114. if (depth == -1)
  115. { if (verbose)
  116. { if (columns == 2)
  117. dotag(stdout, " CYCLE>\n");
  118. else
  119. dotag(stdout, "<<<<<START OF CYCLE>>>>>\n");
  120. }
  121. continue;
  122. }
  123. if (cutoff > 0 && depth >= cutoff)
  124. { printf("-------------\n");
  125. printf("depth-limit (-u%d steps) reached\n", cutoff);
  126. break;
  127. }
  128. if (Skip_claim && pno == 0) continue;
  129. for (dothis = Al_El; dothis; dothis = dothis->Nxt)
  130. { if (dothis->Seqno == nst)
  131. break;
  132. }
  133. if (!dothis)
  134. { printf("%3d: proc %d, no matching stmnt %d\n",
  135. depth, pno - Have_claim, nst);
  136. lost_trail();
  137. }
  138. i = nproc - nstop + Skip_claim;
  139. if (dothis->n->ntyp == '@')
  140. { if (pno == i-1)
  141. { run = run->nxt;
  142. nstop++;
  143. if (verbose&4)
  144. { if (columns == 2)
  145. { dotag(stdout, "<end>\n");
  146. continue;
  147. }
  148. if (Have_claim && pno == 0)
  149. printf("%3d: claim terminates\n",
  150. depth);
  151. else
  152. printf("%3d: proc %d terminates\n",
  153. depth, pno - Have_claim);
  154. }
  155. continue;
  156. }
  157. if (pno <= 1) continue; /* init dies before never */
  158. printf("%3d: stop error, ", depth);
  159. printf("proc %d (i=%d) trans %d, %c\n",
  160. pno - Have_claim, i, nst, dothis->n->ntyp);
  161. lost_trail();
  162. }
  163. for (X = run; X; X = X->nxt)
  164. { if (--i == pno)
  165. break;
  166. }
  167. if (!X)
  168. { printf("%3d: no process %d ", depth, pno - Have_claim);
  169. printf("(state %d)\n", nst);
  170. lost_trail();
  171. }
  172. X->pc = dothis;
  173. lineno = dothis->n->ln;
  174. Fname = dothis->n->fn;
  175. if (dothis->n->ntyp == D_STEP)
  176. { Element *g, *og = dothis;
  177. do {
  178. g = eval_sub(og);
  179. if (g && depth >= jumpsteps
  180. && ((verbose&32) || ((verbose&4) && not_claim())))
  181. { if (columns != 2)
  182. { p_talk(og, 1);
  183. if (og->n->ntyp == D_STEP)
  184. og = og->n->sl->this->frst;
  185. printf("\t[");
  186. comment(stdout, og->n, 0);
  187. printf("]\n");
  188. }
  189. if (verbose&1) dumpglobals();
  190. if (verbose&2) dumplocal(X);
  191. if (xspin) printf("\n");
  192. }
  193. og = g;
  194. } while (g && g != dothis->nxt);
  195. if (X != NULL)
  196. { X->pc = g?huntele(g, 0, -1):g;
  197. }
  198. } else
  199. {
  200. keepgoing: if (dothis->merge_start)
  201. a = dothis->merge_start;
  202. else
  203. a = dothis->merge;
  204. if (X != NULL)
  205. { X->pc = eval_sub(dothis);
  206. if (X->pc) X->pc = huntele(X->pc, 0, a);
  207. }
  208. if (depth >= jumpsteps
  209. && ((verbose&32) || ((verbose&4) && not_claim()))) /* -v or -p */
  210. { if (columns != 2)
  211. { p_talk(dothis, 1);
  212. if (dothis->n->ntyp == D_STEP)
  213. dothis = dothis->n->sl->this->frst;
  214. printf("\t[");
  215. comment(stdout, dothis->n, 0);
  216. printf("]");
  217. if (a && (verbose&32))
  218. printf("\t<merge %d now @%d>",
  219. dothis->merge,
  220. (X && X->pc)?X->pc->seqno:-1);
  221. printf("\n");
  222. }
  223. if (verbose&1) dumpglobals();
  224. if (verbose&2) dumplocal(X);
  225. if (xspin) printf("\n");
  226. if (X && !X->pc)
  227. { X->pc = dothis;
  228. printf("\ttransition failed\n");
  229. a = 0; /* avoid inf loop */
  230. }
  231. }
  232. if (a && X && X->pc && X->pc->seqno != a)
  233. { dothis = X->pc;
  234. goto keepgoing;
  235. } }
  236. if (Have_claim && X && X->pid == 0
  237. && dothis && dothis->n
  238. && lastclaim != dothis->n->ln)
  239. { lastclaim = dothis->n->ln;
  240. if (columns == 2)
  241. { char t[128];
  242. sprintf(t, "#%d", lastclaim);
  243. pstext(0, t);
  244. } else
  245. {
  246. printf("Never claim moves to line %d\t[", lastclaim);
  247. comment(stdout, dothis->n, 0);
  248. printf("]\n");
  249. } } }
  250. printf("spin: trail ends after %d steps\n", depth);
  251. wrapup(0);
  252. }
  253. static void
  254. lost_trail(void)
  255. { int d, p, n, l;
  256. while (fscanf(fd, "%d:%d:%d:%d\n", &d, &p, &n, &l) == 4)
  257. { printf("step %d: proc %d ", d, p); whichproc(p);
  258. printf("(state %d) - d %d\n", n, l);
  259. }
  260. wrapup(1); /* no return */
  261. }
  262. int
  263. pc_value(Lextok *n)
  264. { int i = nproc - nstop;
  265. int pid = eval(n);
  266. RunList *Y;
  267. for (Y = run; Y; Y = Y->nxt)
  268. { if (--i == pid)
  269. return Y->pc->seqno;
  270. }
  271. return 0;
  272. }