guided.c 7.9 KB

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