Skip to content

Commit

Permalink
Merge pull request #695 from cole-miller/sm-fixes
Browse files Browse the repository at this point in the history
Fixes for sm.c
  • Loading branch information
cole-miller authored Sep 2, 2024
2 parents a39b294 + c65caf8 commit e8e28b0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ CC_CHECK_FLAGS_APPEND([AM_CFLAGS],[CFLAGS],[ \
-Wdate-time \
-Wnested-externs \
-Wconversion \
-Wno-format-nonliteral \
-Werror \
])
# To enable:
Expand Down
19 changes: 16 additions & 3 deletions src/lib/sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ int sm_state(const struct sm *m)

static inline void sm_obs(const struct sm *m)
{
tracef("%s pid: %d sm_id: %" PRIu64 " %s |\n",
tracef("%s pid: %d sm_id: %" PRIu64 " %s |",
m->name, m->pid, m->id, m->conf[sm_state(m)].name);
}

void sm_relate(const struct sm *from, const struct sm *to)
{
tracef("%s-to-%s opid: %d dpid: %d id: %" PRIu64 " id: %" PRIu64 " |\n",
tracef("%s-to-%s opid: %d dpid: %d id: %" PRIu64 " id: %" PRIu64 " |",
from->name, to->name, from->pid, to->pid, from->id, to->id);
}

void sm_attr(const struct sm *m, const char *k, const char *fmt, ...)
{
char v[SM_MAX_ATTR_LENGTH];
va_list ap;
va_start(ap, fmt);
vsnprintf(v, sizeof(v), fmt, ap);
va_end(ap);
tracef("%s-attr pid: %d sm_id: %" PRIu64 " %s %s |",
m->name, m->pid, m->id, k, v);
}

void sm_init(struct sm *m,
bool (*invariant)(const struct sm *, int),
bool (*is_locked)(const struct sm *),
Expand All @@ -49,6 +60,7 @@ void sm_init(struct sm *m,
m->is_locked = is_locked;
m->id = ++id;
m->pid = getpid();
m->rc = 0;
snprintf(m->name, SM_MAX_NAME_LENGTH, "%s", name);
sm_obs(m);

Expand Down Expand Up @@ -82,12 +94,13 @@ void sm_fail(struct sm *m, int fail_state, int rc)

m->rc = rc;
m->state = fail_state;
sm_obs(m);
POST(m->invariant != NULL && m->invariant(m, prev));
}

static __attribute__((noinline)) bool check_failed(const char *f, int n, const char *s)
{
tracef("%s:%d check failed: %s\n", f, n, s);
tracef("%s:%d check failed: %s", f, n, s);
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib/sm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define CHECK(cond) sm_check((cond), __FILE__, __LINE__, #cond)

#define SM_MAX_NAME_LENGTH 50
#define SM_MAX_ATTR_LENGTH 100

enum {
SM_PREV_NONE = -1,
Expand Down Expand Up @@ -54,5 +55,9 @@ int sm_state(const struct sm *m);
bool sm_check(bool b, const char *f, int n, const char *s);
/* Relates one state machine to another for observability. */
void sm_relate(const struct sm *from, const struct sm *to);
/**
* Records an attribute of a state machine for observability.
*/
void sm_attr(const struct sm *m, const char *k, const char *fmt, ...);

#endif /* __LIB_SM__ */
3 changes: 0 additions & 3 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ void loggerDefaultEmit(void *data, int level, const char *fmt, va_list args)

/* Then render the message, possibly truncating it. */
n = EMIT_BUF_LEN - strlen(buf) - 1;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
vsnprintf(cursor, n, fmt, args);
#pragma GCC diagnostic pop

fprintf(stderr, "%s\n", buf);
}
3 changes: 0 additions & 3 deletions test/lib/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ void test_logger_emit(void *data, int level, const char *format, va_list args)

sprintf(buf + strlen(buf), "%2d -> [%s] ", t->id, level_name);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
vsnprintf(buf + strlen(buf), 1024 - strlen(buf), format, args);
#pragma GCC diagnostic pop
munit_log(MUNIT_LOG_INFO, buf);
return;

Expand Down

0 comments on commit e8e28b0

Please sign in to comment.