How to make the list of all treads in ChibiOS 21?

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

How to make the list of all treads in ChibiOS 21?

Post by alexblack »

Hi.
For the debug purposes I need to make a list of all threads in the system. In previous versions I used this code:

Code: Select all

  thread_t *ctp;
  /* Scanning registry.*/
  ctp = ch.rlist.newer;
  do {
    /* Exclude IDLE THREAD from list */
    if (ctp->hdr.pqueue.prio != IDLEPRIO)   {
      /* Fill elements with current task information */
      Info.TaskID     = (uint32_t) ctp;
      Info.sName      = ctp->name;
      Info.Prio       = ctp->hdr.pqueue.prio;
      Info.StackBase  = (uint32_t) ctp->wabase;
      Info.StackSize  = (uint32_t) 0;
    }
   
    /* search next thread */
    sts = chSysGetStatusAndLockX();
    ctp = ctp->newer;
    if (ctp == (thread_t *)&ch.rlist) {
      ctp = NULL;
    }
    chSysRestoreStatusX(sts);
  } while (ctp != NULL);

Now it does not compiling because all structures was changed.
Please help to make this.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How to make the list of all treads in ChibiOS 21?

Post by Giovanni »

Hi,

In order to scan the registry you need to use chRegFirstThread() and chRegNextThread() functions, it is not appropriate to use the pointers directly.

If you need an example then look at the "threads" shell command in /os/various/shell/shell_cmd.c

Giovanni
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

Re: How to make the list of all treads in ChibiOS 21?

Post by alexblack »

Thank you.
方悠然
Posts: 4
Joined: Mon Nov 14, 2022 2:43 am
Has thanked: 2 times

Re: How to make the list of all treads in ChibiOS 21?

Post by 方悠然 »

I've seen /os/various/shell/shell_cmd.c, I want to increase the unused ratio of the thread stack in the "threads" shell.
It seems that using chRegFirstThread() and chRegNextThread() does not get "wsp". I can only add the "wsp" parameter manually.
//my program:

#if CH_DBG_FILL_THREADS
float GetThdFreeStack(void *wsp, uint32_t size) {
uint32_t stack_unused = 0;
uint8_t *startp = (uint8_t *)wsp; //The start address of the thread stack
uint8_t *endp = (uint8_t *)wsp + size; //The address at the end of the thread stack
while (startp < endp)
if(*startp++ == CH_DBG_STACK_FILL_VALUE) ++stack_unused; //Unused thread stack space
return ((float)stack_unused - sizeof(thread_t))/((float)size - sizeof(thread_t)); //The ratio of unused stacks to the thread's total stack space
}
#endif
......
chThdCreateStatic(motor_test_thread_wa, sizeof(motor_test_thread_wa), NORMALPRIO-1, motor_test_thread, NULL);
......
float unused_thread1 = GetThdFreeStack(motor_test_thread_wa, sizeof(motor_test_thread_wa));
float unused_thread2 = GetThdFreeStack(......);
......

Is there any way to get the "wsp" list, or to add thread usage to the "threads" shell? Appreciate.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How to make the list of all treads in ChibiOS 21?

Post by Giovanni »

Hi,

The "wabase" field of the thread structure is the base of the working area, that is your "wsp".

Giovanni

Giovanni
Post Reply