guided.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /***** spin: guided.c *****/
  2. /* Copyright (c) 1991-2000 by Lucent Technologies - Bell Laboratories */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* Permission is given to distribute this code provided that this intro- */
  5. /* ductory message is not removed and no monies are exchanged. */
  6. /* No guarantee is expressed or implied by the distribution of this code. */
  7. /* Software written by Gerard J. Holzmann as part of the book: */
  8. /* `Design and Validation of Computer Protocols,' ISBN 0-13-539925-4, */
  9. /* Prentice Hall, Englewood Cliffs, NJ, 07632. */
  10. /* Send bug-reports and/or questions to: gerard@research.bell-labs.com */
  11. #include "spin.h"
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #ifdef PC
  15. #include "y_tab.h"
  16. #else
  17. #include "y.tab.h"
  18. #endif
  19. extern RunList *run, *X;
  20. extern Element *Al_El;
  21. extern Symbol *Fname, *oFname;
  22. extern int verbose, lineno, xspin, jumpsteps, depth, merger;
  23. extern int nproc, nstop, Tval, Rvous, m_loss, ntrail, columns;
  24. extern short Have_claim, Skip_claim;
  25. extern void ana_src(int, int);
  26. int TstOnly = 0, pno;
  27. static int lastclaim = -1;
  28. static FILE *fd;
  29. static void lost_trail(void);
  30. static void
  31. whichproc(int p)
  32. { RunList *oX;
  33. for (oX = run; oX; oX = oX->nxt)
  34. if (oX->pid == p)
  35. { printf("(%s) ", oX->n->name);
  36. break;
  37. }
  38. }
  39. static int
  40. newer(char *f1, char *f2)
  41. { struct stat x, y;
  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 && e->n->ntyp == ATOMIC
  52. || e->n && e->n->ntyp == NON_ATOMIC
  53. || e->n && e->n->ntyp == D_STEP)
  54. (void) huntstart(e);
  55. }
  56. void
  57. match_trail(void)
  58. { int i, a, nst;
  59. Element *dothis;
  60. RunList *oX;
  61. char snap[256];
  62. if (ntrail)
  63. sprintf(snap, "%s%d.trail", oFname->name, ntrail);
  64. else
  65. sprintf(snap, "%s.trail", oFname->name);
  66. if (!(fd = fopen(snap, "r")))
  67. { snap[strlen(snap)-2] = '\0'; /* .tra on some pc's */
  68. if (!(fd = fopen(snap, "r")))
  69. { printf("spin: cannot find trail file\n");
  70. alldone(1);
  71. } }
  72. if (xspin == 0 && newer(oFname->name, snap))
  73. printf("spin: warning, \"%s\" is newer than %s\n",
  74. oFname->name, snap);
  75. Tval = m_loss = 1; /* timeouts and losses may be part of trail */
  76. hookup();
  77. while (fscanf(fd, "%d:%d:%d\n", &depth, &pno, &nst) == 3)
  78. { if (depth == -2) { start_claim(pno); continue; }
  79. if (depth == -4) { merger = 1; ana_src(0, 1); continue; }
  80. if (depth == -1)
  81. { if (verbose)
  82. { if (columns == 2)
  83. dotag(stdout, " CYCLE>\n");
  84. else
  85. dotag(stdout, "<<<<<START OF CYCLE>>>>>\n");
  86. }
  87. continue;
  88. }
  89. if (Skip_claim && pno == 0) continue;
  90. for (dothis = Al_El; dothis; dothis = dothis->Nxt)
  91. { if (dothis->Seqno == nst)
  92. break;
  93. }
  94. if (!dothis)
  95. { printf("%3d: proc %d, no matching stmnt %d\n",
  96. depth, pno, nst);
  97. lost_trail();
  98. }
  99. i = nproc - nstop + Skip_claim;
  100. if (dothis->n->ntyp == '@')
  101. { if (pno == i-1)
  102. { run = run->nxt;
  103. nstop++;
  104. if (verbose&4)
  105. { if (columns == 2)
  106. { dotag(stdout, "<end>\n");
  107. continue;
  108. }
  109. printf("%3d: proc %d terminates\n",
  110. depth, pno);
  111. }
  112. continue;
  113. }
  114. if (pno <= 1) continue; /* init dies before never */
  115. printf("%3d: stop error, ", depth);
  116. printf("proc %d (i=%d) trans %d, %c\n",
  117. pno, i, nst, dothis->n->ntyp);
  118. lost_trail();
  119. }
  120. for (X = run; X; X = X->nxt)
  121. { if (--i == pno)
  122. break;
  123. }
  124. if (!X)
  125. { printf("%3d: no process %d ", depth, pno);
  126. printf("(state %d)\n", nst);
  127. lost_trail();
  128. }
  129. X->pc = dothis;
  130. lineno = dothis->n->ln;
  131. Fname = dothis->n->fn;
  132. oX = X; /* a rendezvous could change it */
  133. if (dothis->n->ntyp == D_STEP)
  134. { Element *g, *og = dothis;
  135. do {
  136. g = eval_sub(og);
  137. if (g && depth >= jumpsteps
  138. && ((verbose&32) || (verbose&4)))
  139. { if (columns != 2)
  140. { p_talk(og, 1);
  141. if (og->n->ntyp == D_STEP)
  142. og = og->n->sl->this->frst;
  143. printf("\t[");
  144. comment(stdout, og->n, 0);
  145. printf("]\n");
  146. }
  147. if (verbose&1) dumpglobals();
  148. if (verbose&2) dumplocal(X);
  149. if (xspin) printf("\n");
  150. }
  151. og = g;
  152. } while (g && g != dothis->nxt);
  153. X->pc = g?huntele(g, 0):g;
  154. } else
  155. {
  156. keepgoing: X->pc = eval_sub(dothis);
  157. X->pc = huntele(X->pc, 0);
  158. if (dothis->merge_start)
  159. a = dothis->merge_start;
  160. else
  161. a = dothis->merge;
  162. if (depth >= jumpsteps
  163. && ((verbose&32) || (verbose&4)))
  164. { if (columns != 2)
  165. { p_talk(dothis, 1);
  166. if (dothis->n->ntyp == D_STEP)
  167. dothis = dothis->n->sl->this->frst;
  168. printf("\t[");
  169. comment(stdout, dothis->n, 0);
  170. printf("]");
  171. if (a && (verbose&32))
  172. printf("\t<merge %d now @%d>",
  173. dothis->merge,
  174. X->pc?X->pc->seqno:-1);
  175. printf("\n");
  176. }
  177. if (verbose&1) dumpglobals();
  178. if (verbose&2) dumplocal(X);
  179. if (xspin) printf("\n");
  180. }
  181. if (a && X->pc && X->pc->seqno != a)
  182. { dothis = X->pc;
  183. goto keepgoing;
  184. }
  185. }
  186. if (Have_claim && X && X->pid == 0
  187. && dothis && dothis->n
  188. && lastclaim != dothis->n->ln)
  189. { lastclaim = dothis->n->ln;
  190. if (columns == 2)
  191. { char t[128];
  192. sprintf(t, "#%d", lastclaim);
  193. pstext(0, t);
  194. } else
  195. {
  196. printf("Never claim moves to line %d\t[", lastclaim);
  197. comment(stdout, dothis->n, 0);
  198. printf("]\n");
  199. }
  200. } }
  201. printf("spin: trail ends after %d steps\n", depth);
  202. wrapup(0);
  203. }
  204. static void
  205. lost_trail(void)
  206. { int d, p, n, l;
  207. while (fscanf(fd, "%d:%d:%d:%d\n", &d, &p, &n, &l) == 4)
  208. { printf("step %d: proc %d ", d, p); whichproc(p);
  209. printf("(state %d) - d %d\n", n, l);
  210. }
  211. wrapup(1); /* no return */
  212. }
  213. int
  214. pc_value(Lextok *n)
  215. { int i = nproc - nstop;
  216. int pid = eval(n);
  217. RunList *Y;
  218. for (Y = run; Y; Y = Y->nxt)
  219. { if (--i == pid)
  220. return Y->pc->seqno;
  221. }
  222. return 0;
  223. }