guided.c 6.8 KB

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