1 #include "snapshot-interface.h"
17 #define MYBINARYNAME "model"
18 #define MYLIBRARYNAME "libmodel.so"
19 #define MAPFILE_FORMAT "/proc/%d/maps"
21 SnapshotStack * snapshotObject;
24 /** The SnapshotGlobalSegments function computes the memory regions
25 * that may contain globals and then configures the snapshotting
26 * library to snapshot them.
28 static void SnapshotGlobalSegments(){
30 char buf[9000], execname[100];
33 sprintf(execname, "vmmap -interleaved %d", pid);
34 map=popen(execname, "r");
41 /* Wait for correct part */
42 while (fgets(buf, sizeof(buf), map)) {
43 if (strstr(buf, "==== regions for process"))
47 while (fgets(buf, sizeof(buf), map)) {
48 char regionname[200] = "";
56 //Skip out at the end of the section
60 sscanf(buf, "%22s %p-%p [%5dK] %c%c%c/%c%c%c SM=%3s %200s\n", &type, &begin, &end, &size, &r, &w, &x, &mr, &mw, &mx, smstr, regionname);
62 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
63 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
65 addMemoryRegionToSnapShot(begin, len);
67 DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
73 /** The SnapshotGlobalSegments function computes the memory regions
74 * that may contain globals and then configures the snapshotting
75 * library to snapshot them.
77 static void SnapshotGlobalSegments(){
79 char buf[9000], filename[100];
82 sprintf(filename, MAPFILE_FORMAT, pid);
83 map = fopen(filename, "r");
88 while (fgets(buf, sizeof(buf), map)) {
89 char regionname[200] = "";
93 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
94 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
95 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
97 addMemoryRegionToSnapShot(begin, len);
98 DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
105 SnapshotStack::SnapshotStack(){
106 SnapshotGlobalSegments();
110 SnapshotStack::~SnapshotStack(){
114 /** This method returns to the last snapshot before the inputted
115 * sequence number. This function must be called from the model
116 * checking thread and not from a snapshotted stack.
117 * @param seqindex is the sequence number to rollback before.
118 * @return is the sequence number we actually rolled back to.
121 int SnapshotStack::backTrackBeforeStep(int seqindex) {
123 if (stack->index<=seqindex) {
125 rollBack(stack->snapshotid);
128 struct stackEntry *tmp=stack;
134 /** This method takes a snapshot at the given sequence number.
137 void SnapshotStack::snapshotStep(int seqindex) {
138 struct stackEntry *tmp=(struct stackEntry *)MYMALLOC(sizeof(struct stackEntry));
141 tmp->snapshotid=takeSnapshot();