Skip to content

Commit

Permalink
kernel: Update k_thread_state_str()
Browse files Browse the repository at this point in the history
Updates k_thread_state_str() to interpret the halting bits
correctly.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
  • Loading branch information
peter-mitsis committed Aug 16, 2023
1 parent cf169f7 commit 54533ad
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,34 @@ static size_t copy_bytes(char *dest, size_t dest_size, const char *src, size_t s
return bytes_to_copy;
}

#define Z_STATE_STR_DUMMY "dummy"
#define Z_STATE_STR_PENDING "pending"
#define Z_STATE_STR_PRESTART "prestart"
#define Z_STATE_STR_DEAD "dead"
#define Z_STATE_STR_SUSPENDED "suspended"
#define Z_STATE_STR_QUEUED "queued"
#define Z_STATE_STR_ABORTING "aborting"
#define Z_STATE_STR_SUSPENDING "suspending"
#define Z_STATE_STR_SKIP ""

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;
static const char *states_str[8] = {"dummy", "pending", "prestart",
"dead", "suspended", "aborting",
"", "queued"};
static const size_t states_sz[8] = {5, 7, 8, 4, 9, 8, 0, 6};
static const struct {
const char *str;
size_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_SKIP, sizeof(Z_STATE_STR_SKIP) - 1},
{ Z_STATE_STR_SKIP, sizeof(Z_STATE_STR_SKIP) - 1},
{ Z_STATE_STR_QUEUED, sizeof(Z_STATE_STR_QUEUED) - 1},
};

if ((buf == NULL) || (buf_size == 0)) {
return "";
Expand All @@ -292,14 +311,19 @@ const char *k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size)
* separate the descriptive strings with a '+'.
*/

for (uint8_t index = 0; thread_state != 0; index++) {

for (unsigned int index = 0;
index < 8 * sizeof(thread_id->base.thread_state);
index++) {
bit = BIT(index);
if ((thread_state & bit) == 0) {
if (((thread_state & bit) == 0) ||
(state_string[index].len == 0)) {
continue;
}

off += copy_bytes(buf + off, buf_size - off,
states_str[index], states_sz[index]);
state_string[index].str,
state_string[index].len);

thread_state &= ~bit;

Expand All @@ -308,6 +332,18 @@ const char *k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size)
}
}

/* Only the _THREAD_HALTING_MASK bits might still be set */

if (thread_state == _THREAD_ABORTING) {
off += copy_bytes(buf + off, buf_size - off,
Z_STATE_STR_ABORTING,
sizeof(Z_STATE_STR_ABORTING) - 1);
} else if (thread_state == _THREAD_SUSPENDING) {
off += copy_bytes(buf + off, buf_size - off,
Z_STATE_STR_SUSPENDING,
sizeof(Z_STATE_STR_SUSPENDING) - 1);
}

buf[off] = '\0';

return (const char *)buf;
Expand Down

0 comments on commit 54533ad

Please sign in to comment.