kernel/thread: Fix assumptions in k_thread_state_str()

This table just blindly assumed that the values of _THREAD_xxx state
bits wouldn't change.  That's dangerous.  (That's right, I tried to
change them and broke it.)

Signed-off-by: Andy Ross <andyross@google.com>
This commit is contained in:
Andy Ross 2024-11-25 11:58:49 -08:00 committed by Anas Nashif
commit 6877b6d8e5

View file

@ -228,19 +228,22 @@ const char *k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size)
size_t off = 0;
uint8_t bit;
uint8_t thread_state = thread_id->base.thread_state;
#define SS_ENT(s) { Z_STATE_STR_##s, _THREAD_##s, sizeof(Z_STATE_STR_##s) - 1 }
static const struct {
const char *str;
size_t len;
uint16_t bit;
uint16_t len;
} state_string[] = {
{ Z_STATE_STR_DUMMY, sizeof(Z_STATE_STR_DUMMY) - 1},
{ Z_STATE_STR_PENDING, sizeof(Z_STATE_STR_PENDING) - 1},
{ Z_STATE_STR_PRESTART, sizeof(Z_STATE_STR_PRESTART) - 1},
{ Z_STATE_STR_DEAD, sizeof(Z_STATE_STR_DEAD) - 1},
{ Z_STATE_STR_SUSPENDED, sizeof(Z_STATE_STR_SUSPENDED) - 1},
{ Z_STATE_STR_ABORTING, sizeof(Z_STATE_STR_ABORTING) - 1},
{ Z_STATE_STR_SUSPENDING, sizeof(Z_STATE_STR_SUSPENDING) - 1},
{ Z_STATE_STR_QUEUED, sizeof(Z_STATE_STR_QUEUED) - 1},
SS_ENT(DUMMY),
SS_ENT(PENDING),
SS_ENT(PRESTART),
SS_ENT(DEAD),
SS_ENT(SUSPENDED),
SS_ENT(ABORTING),
SS_ENT(SUSPENDING),
SS_ENT(QUEUED),
};
#undef SS_ENT
if ((buf == NULL) || (buf_size == 0)) {
return "";
@ -256,7 +259,7 @@ const char *k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size)
for (unsigned int index = 0; thread_state != 0; index++) {
bit = BIT(index);
bit = state_string[index].bit;
if ((thread_state & bit) == 0) {
continue;
}