11#include "irx_imports.h"
15IRX_ID(
"Temporarily_Thread_monitor", 1, 3);
20 volatile int m_start_stop_status;
31 struct thread *m_thread_ptr;
41 unsigned int m_alarm_id;
46static int CreateThmonThread(u32 attr,
void (*
thread)(
void *arg), u32 priority, u32 stacksize, u32 option);
47static int do_get_thread_count();
48static void ThmonMonitorThread(
void *arg);
49static void ThmonMainThread(
void *arg);
51static const char *g_help_msg[] =
53 "\nsimple thread monitor program =====================================\n",
54 "-- thlist [-v] [-a] [<thid>] -- thread list\n",
55 "-- rdlist [-v] -- ready thread list\n",
56 "-- sllist [-v] -- sleeping thread list\n",
57 "-- dllist [-v] -- delayed thread list\n",
58 "-- rtlist [-v] [<times>] -- thread running time list\n",
59 "-- semlist [-w] [-v] -- semaphore list\n",
60 "-- evlist [-w] [-v] -- eventflag list\n",
61 "-- msglist -- messagebox list\n",
62 "-- vpllist -- Vpool list\n",
63 "-- fpllist -- Fpool list\n",
64 "-- almlist -- Alarm list\n",
65 "-- cpuwatch [-b] [-br] [-v] [<samples>] -- cpu time watching <samples/sec>\n",
66 "-- thwatch [-b] [-br] [-v] <thid> [<samples>] -- thread time watching <samples/sec>\n",
67 "-- freemem -- report free memory size\n",
68 "-- vblank <on/off> -- vblank interrupt on/off\n",
69 "-- / -- repeat last command\n",
72static const char *g_th_status_short =
"000RunRdy333Wat555666777Sus999aaabbbWSudddeeefffDom???";
73static const char *g_th_wait_short =
" SlDlSeEvMbVpFp ";
75static int g_monitorThreadId;
78static char g_progspace1[52];
79static char g_progspace2[52];
81static char g_PreviousCommand[208];
82static char g_CommandBuffer[200];
84int _start(
int ac,
char **av)
90 if (ac >= 2 && !strncmp(av[1],
"-th=", 4)) {
91 margen_val = strtol(av[1] + 4, 0, 10) + 10;
92 printf(
"margen = %d\n", margen_val);
94 g_thcpuwatch_info.m_start_stop_status = 0;
95 g_tty9_fd = open(
"tty9:", 3);
97 main_thread_id = CreateThmonThread(
101 28 * (do_get_thread_count() + margen_val) + 3584,
103 g_monitorThreadId = CreateThmonThread(0x2000000u, ThmonMonitorThread, 4u, 0x800u, 0);
104 if (main_thread_id <= 0 || g_monitorThreadId <= 0)
106 StartThread(main_thread_id, 0);
110static int CreateThmonThread(u32 attr,
void (*
thread)(
void *arg), u32 priority, u32 stacksize, u32 option)
116 thparam.priority = priority;
117 thparam.stacksize = stacksize;
118 thparam.option = option;
119 return CreateThread(&thparam);
122static int do_get_thread_count()
130 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
137static void PrintMessage(
const char *msg)
139 fdprintf(g_tty9_fd,
"%s\n", msg);
142static char *RemoveLeadingWhitespaces(
char *str)
144 for (; *str ==
' ' || *str ==
'\t'; str += 1) {
150static char *GetEndOfString(
char *str)
152 for (; *str && *str !=
' ' && *str !=
'\t'; str += 1)
157static void DisplayHelpMessage()
161 for (i = 0; g_help_msg[i]; i += 1) {
162 fdprintf(g_tty9_fd, g_help_msg[i]);
163 if (i + 1 == 23 * ((i + 1) / 23) && g_help_msg[i + 1]) {
164 fdprintf(g_tty9_fd,
" more ? ");
165 if (fdgetc(g_tty9_fd) !=
'y') {
166 fdprintf(g_tty9_fd,
"\n");
169 fdprintf(g_tty9_fd,
"\r");
174static int do_parse_hex(
char *str)
179 for (; *str && isxdigit(*str); str += 1) {
181 retres += toupper(*str);
182 retres -= isdigit(*str) ?
'0' :
'7';
187static char *get_module_name_from_addr(
unsigned int entry_addr)
191 for (image_info = GetLoadcoreInternalData()->image_info; image_info; image_info = image_info->next) {
192 if (entry_addr >= image_info->text_start && entry_addr < image_info->text_start + image_info->text_size)
193 return image_info->name;
198static void DisplayThreadInformation(
struct thread *threadstruct,
int is_verbose,
int is_brief)
201 fdprintf(g_tty9_fd,
" ");
204 "%3x %07x %08x %07x %.3s ",
205 threadstruct->tag.id,
206 MAKE_HANDLE(threadstruct),
208 threadstruct->option,
209 &g_th_status_short[3 * (
char)threadstruct->status]);
212 "%06x %06x %04x %06x %3d %3d",
214 threadstruct->stack_top,
215 threadstruct->stack_size,
217 (s16)threadstruct->priority,
218 (s16)threadstruct->init_priority);
220 fdprintf(g_tty9_fd,
"\n");
225 g_th_wait_short[2 * (s16)threadstruct->wait_type],
226 g_th_wait_short[2 * (s16)threadstruct->wait_type + 1]);
227 switch ((s16)threadstruct->wait_type) {
229 fdprintf(g_tty9_fd,
"%7s%2x\n",
"", threadstruct->wakeup_count);
232 fdprintf(g_tty9_fd,
"%6x %2x\n", threadstruct->wait_usecs, threadstruct->wakeup_count);
239 MAKE_HANDLE(threadstruct->wait_usecs),
240 threadstruct->wakeup_count);
245 char *module_name_from_addr;
247 module_name_from_addr = get_module_name_from_addr((
unsigned int)threadstruct->entry);
248 if (module_name_from_addr) {
250 fdprintf(g_tty9_fd,
" ");
251 fdprintf(g_tty9_fd,
" Name=%s\n", module_name_from_addr);
254 if (threadstruct->saved_regs && threadstruct->status != 16) {
256 fdprintf(g_tty9_fd,
" ");
259 " PC=%06x RA=%06x SP=%06x Context_addr/mask=%08x/%08x\n",
260 threadstruct->saved_regs->pc,
261 threadstruct->saved_regs->ra,
262 threadstruct->saved_regs->sp,
263 threadstruct->saved_regs,
264 threadstruct->saved_regs->unk);
269 for (i = 0; i < (threadstruct->stack_size / 4); i += 1) {
270 if (((u32 *)threadstruct->stack_top)[i] != 0xFFFFFFFF && ((u32 *)threadstruct->stack_top)[i] != 0x11111111)
274 fdprintf(g_tty9_fd,
" ");
275 fdprintf(g_tty9_fd,
" Free_Stack_Size=%06x", i * 4);
278 " I-preempt/T-preenmpt/release counts=%d,%d,%d\n",
279 threadstruct->irq_preemption_count,
280 threadstruct->thread_preemption_count,
281 threadstruct->release_count);
282 if (is_brief && threadstruct->wait_type == 4) {
283 fdprintf(g_tty9_fd,
" ");
286 " Event bitpattern, mode=0x%08x, 0x%x\n",
287 threadstruct->event_bits,
288 (s16)threadstruct->event_mode);
293static void thlist_cmd_handler(
char *cmdparam)
308 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
309 EndOfString = GetEndOfString(cmdparam_cur);
312 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
314 if (!strcmp(cmdparam_cur,
"-v"))
316 if (!strcmp(cmdparam_cur,
"-a"))
318 if (!strcmp(cmdparam_cur,
"-i"))
320 if (*cmdparam_cur !=
'-')
321 is_hexval = do_parse_hex(cmdparam_cur);
324 if (is_hexval < g_ThbaseInternal->thread_id) {
325 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
326 if (
thread->tag.id == is_hexval) {
331 fdprintf(g_tty9_fd,
"thread #%d not found \n", is_hexval);
334 is_hexval = MAKE_HANDLE(
thread);
336 if (ReferThreadStatus(is_hexval, &thstatus) < 0) {
337 fdprintf(g_tty9_fd,
"thid:%x not found \n", is_hexval);
340 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP WT WID WUC\n");
341 DisplayThreadInformation(
342 (
struct thread *)HANDLE_PTR(is_hexval),
346 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP WT WID WUC\n");
347 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
348 if (ReferThreadStatus(MAKE_HANDLE(
thread), &thstatus) < 0) {
351 "thid = %x not found \n",
355 if ((is_flag_a || thstatus.status != 16) && (is_flag_i ||
thread != g_ThbaseInternal->idle_thread)) {
356 DisplayThreadInformation(
thread, is_flag_v, 0);
362static void do_rtlist_handle_clock(
int is_flag_v_or_c)
365 char *module_name_from_addr;
371 fdprintf(g_tty9_fd,
" THID CLOCK SEC\n");
372 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
373 if (ReferThreadStatus(MAKE_HANDLE(
thread), &thstatus) < 0 || thstatus.status == 16) {
376 "thid = %x not found \n",
377 (
unsigned int)MAKE_HANDLE(
thread));
380 clks.hi =
thread->run_clocks_hi;
381 clks.lo =
thread->run_clocks_lo;
382 SysClock2USec(&clks, &sec_val, &usec_val);
383 if (
thread == g_ThbaseInternal->idle_thread) {
386 " %08x_%08x %5d.%06d",
391 fdprintf(g_tty9_fd,
" System Idle time\n");
395 "%3x %07x %08x_%08x %5d.%06d",
397 (
unsigned int)MAKE_HANDLE(
thread),
402 if (is_flag_v_or_c) {
403 module_name_from_addr = get_module_name_from_addr((
unsigned int)thstatus.entry);
404 if (module_name_from_addr)
405 fdprintf(g_tty9_fd,
" Name=%s", module_name_from_addr);
407 fdprintf(g_tty9_fd,
"\n");
412 " Total thread switch count = %d, comes out of idle count = %d\n",
413 g_ThbaseInternal->thread_switch_count,
414 g_ThbaseInternal->idle_thread->irq_preemption_count);
417static void sys_clock_subtract(
424 diff.lo = pEnd->lo - pStart->lo;
425 diff.hi = pEnd->hi - pStart->hi - (pStart->lo > pEnd->lo);
426 memcpy(pRet, &diff,
sizeof(diff));
429static void do_rtlist_handle_priority(
int is_flag_v_or_c,
int int_value)
433 char *module_name_from_addr;
440 g_rtlist_nondormant = __builtin_alloca(
sizeof(
struct thmon_rtlist_item) * do_get_thread_count());
441 for (; int_value > 0; int_value -= 1) {
445 curelem1 = g_rtlist_nondormant;
446 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
447 curelem1->m_thread_ptr = 0;
448 if (
thread->status != 16) {
449 curelem1->m_thread_ptr =
thread;
450 curelem1->m_thread_id =
thread->tag.id;
451 curelem1->m_old_clocks.hi =
thread->run_clocks_hi;
452 curelem1->m_old_clocks.lo =
thread->run_clocks_lo;
454 curelem1->m_thread_ptr = 0;
457 GetSystemTime(&timebefore);
458 DelayThread(1000000);
459 GetSystemTime(&timeafter);
460 for (curelem2 = g_rtlist_nondormant; curelem2->m_thread_ptr; curelem2 += 1) {
461 if (curelem2->m_thread_id == curelem2->m_thread_ptr->tag.id) {
462 curelem2->m_flags = 0;
463 curelem2->m_new_clocks.hi = curelem2->m_thread_ptr->run_clocks_hi;
464 curelem2->m_new_clocks.lo = curelem2->m_thread_ptr->run_clocks_lo;
466 curelem2->m_flags = -1;
469 sys_clock_subtract(&timeafter, &timebefore, &timeafter);
470 fdprintf(g_tty9_fd,
" THID PRIO USE\n");
471 for (curelem2 = g_rtlist_nondormant; curelem2->m_thread_ptr; curelem2 += 1) {
472 if ((curelem2->m_flags & 0x80000000) != 0) {
475 curptr = curelem2->m_thread_ptr;
476 sys_clock_subtract(&curelem2->m_new_clocks, &curelem2->m_old_clocks, &timeres);
479 SysClock2USec(&timeafter, &sec, &usec);
480 if (curptr != g_ThbaseInternal->idle_thread) {
483 "%3x %07x %3d %3d.%03d %%",
486 (s16)curptr->priority,
489 if (is_flag_v_or_c) {
490 module_name_from_addr = get_module_name_from_addr((
unsigned int)curptr->entry);
491 if (module_name_from_addr)
492 fdprintf(g_tty9_fd,
" Name=%s", module_name_from_addr);
494 fdprintf(g_tty9_fd,
"\n");
499 " %6d/%6d/ %3d.%03d %%",
500 g_ThbaseInternal->thread_switch_count,
501 curptr->irq_preemption_count,
504 fdprintf(g_tty9_fd,
" Total switch / comes out of idle / System Idle time\n");
510static void rtlist_cmd_handler(
char *cmdparam)
519 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
520 EndOfString = GetEndOfString(cmdparam_cur);
523 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
525 if (!strcmp(cmdparam_cur,
"-v"))
526 is_flag_v_or_c |= 1u;
527 if (!strcmp(cmdparam_cur,
"-c"))
528 is_flag_v_or_c |= 2u;
529 if (*cmdparam_cur !=
'-')
530 int_value = strtol(cmdparam_cur, 0, 10);
533 do_rtlist_handle_priority(is_flag_v_or_c, int_value);
535 do_rtlist_handle_clock(is_flag_v_or_c);
538static char *do_output_progress_bar_newline(
int curlen)
542 curlen_div_2000 = curlen / 2000;
543 strncpy(g_progspace1,
"----+----+----+----+----+----+----+----+----+----+", curlen / 2000);
544 g_progspace1[curlen_div_2000] =
'\n';
545 g_progspace1[curlen_div_2000 + 1] =
'\0';
549static char *do_output_progress_bar_carriageret(
int curlen)
553 curlen_div_2000 = curlen / 2000;
554 strncpy(g_progspace2,
"----+----+----+----+----+----+----+----+----+----+", curlen / 2000);
555 strncpy(&g_progspace2[curlen_div_2000],
" ", 50 - curlen_div_2000);
556 strcpy(&g_progspace2[50],
"\r");
560static void ThmonMonitorThread(
void *arg)
564 struct thread *target_thread;
574 struct thread *this_thread;
581 delayval = 1000 * (1000 / usrptr->m_sample_count);
582 usrptr->m_start_stop_status = 1;
583 target_thread = usrptr->m_thread;
584 this_thread = (
struct thread *)HANDLE_PTR(GetThreadId());
585 this_thread_clks_2.lo = 0;
586 this_thread_clks_2.hi = 0;
587 while (usrptr->m_start_stop_status != 2) {
589 target_thread_clks_1.hi = target_thread->run_clocks_hi;
590 target_thread_clks_1.lo = target_thread->run_clocks_lo;
591 DelayThread(delayval);
592 GetSystemTime(&sys_time_clks_1);
597 sys_time_clks_2 = sys_time_clks_1;
598 target_thread_clks_2.hi = target_thread->run_clocks_hi;
599 target_thread_clks_2.lo = target_thread->run_clocks_lo;
600 this_thread_clks_1.hi = this_thread->run_clocks_hi;
601 this_thread_clks_1.lo = this_thread->run_clocks_lo;
605 &this_thread_clks_2);
610 this_thread_clks_2 = this_thread_clks_1;
612 &target_thread_clks_2,
613 &target_thread_clks_1,
617 SysClock2USec(&tmp_time_clks_1, &sec, &usec);
618 switch (usrptr->m_barchart) {
620 Kprintf(
"%3d.%03d %07x", sec, usec, target_thread->saved_regs->pc);
621 if (usrptr->m_verbose) {
624 target_thread->irq_preemption_count,
625 target_thread->thread_preemption_count,
626 target_thread->release_count);
627 Kprintf(
"%c", (chrcnt <= 0) ?
' ' :
'\n');
632 Kprintf(
"%c", (chrcnt < 4) ?
' ' :
'\n');
644 (usrptr->m_barchart == 1) ? do_output_progress_bar_newline(sec * 1000 + usec) : do_output_progress_bar_carriageret(sec * 1000 + usec));
650 target_thread_clks_1.hi = g_ThbaseInternal->idle_thread->run_clocks_hi;
651 target_thread_clks_1.lo = g_ThbaseInternal->idle_thread->run_clocks_lo;
652 GetSystemTime(&sys_time_clks_2);
653 DelayThread(delayval);
654 GetSystemTime(&sys_time_clks_1);
655 target_thread_clks_2.hi = g_ThbaseInternal->idle_thread->run_clocks_hi;
656 target_thread_clks_2.lo = g_ThbaseInternal->idle_thread->run_clocks_lo;
662 &target_thread_clks_2,
663 &target_thread_clks_1,
667 SysClock2USec(&tmp_time_clks_1, &sec, &usec);
668 math_magic_3 = 100000 - (sec * 1000 + usec);
669 switch (usrptr->m_barchart) {
671 Kprintf(
"%3d.%03d", math_magic_3 / 1000, math_magic_3 % 1000);
672 if (usrptr->m_verbose) {
675 g_ThbaseInternal->thread_switch_count,
676 g_ThbaseInternal->idle_thread->irq_preemption_count,
677 (chrcnt < 2) ?
" " :
"\n");
682 Kprintf((chrcnt < 8) ?
" " :
"\n");
694 (usrptr->m_barchart == 1) ? do_output_progress_bar_newline(math_magic_3) : do_output_progress_bar_carriageret(math_magic_3));
701 usrptr->m_start_stop_status = 0;
704static void do_stop_current_thcpuwatch()
706 if (!g_thcpuwatch_info.m_start_stop_status) {
709 g_thcpuwatch_info.m_start_stop_status = 2;
710 while (g_thcpuwatch_info.m_start_stop_status) {
715static void thwatch_cmd_handler(
char *cmdparam)
730 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
731 EndOfString = GetEndOfString(cmdparam_cur);
734 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
736 if (!strcmp(cmdparam_cur,
"-b"))
738 if (!strcmp(cmdparam_cur,
"-br"))
740 if (!strcmp(cmdparam_cur,
"-v"))
742 if (*cmdparam_cur !=
'-') {
744 integer_value = strtol(cmdparam_cur, 0, 10);
746 hex_value = do_parse_hex(cmdparam_cur);
749 do_stop_current_thcpuwatch();
753 if (hex_value < g_ThbaseInternal->thread_id) {
756 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
757 if (
thread->tag.id == hex_value)
761 fdprintf(g_tty9_fd,
"thread #%d not found \n", hex_value);
764 hex_value = MAKE_HANDLE(
thread);
766 g_thcpuwatch_info.m_barchart = is_flag_b_or_br;
767 g_thcpuwatch_info.m_sample_count = integer_value;
768 g_thcpuwatch_info.m_verbose = is_flag_v;
769 if (hex_value <= 0 || ReferThreadStatus(hex_value, &thstatus)) {
770 fdprintf(g_tty9_fd,
"thid:%x not found \n", hex_value);
771 }
else if (integer_value > 0) {
772 g_thcpuwatch_info.m_thread = (
struct thread *)HANDLE_PTR(hex_value);
773 fdprintf(g_tty9_fd,
"Thread %x time watching and print to ITTYK\n", hex_value);
774 StartThread(g_monitorThreadId, &g_thcpuwatch_info);
778static void cpuwatch_cmd_handler(
char *cmdparam)
789 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
790 EndOfString = GetEndOfString(cmdparam_cur);
793 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
795 if (!strcmp(cmdparam_cur,
"-b"))
797 if (!strcmp(cmdparam_cur,
"-br"))
799 if (!strcmp(cmdparam_cur,
"-v"))
801 if (*cmdparam_cur !=
'-')
802 int_value = strtol(cmdparam_cur, 0, 10);
804 do_stop_current_thcpuwatch();
806 g_thcpuwatch_info.m_barchart = is_flag_b_or_br;
807 g_thcpuwatch_info.m_sample_count = int_value;
808 g_thcpuwatch_info.m_verbose = is_flag_v;
809 g_thcpuwatch_info.m_thread = 0;
810 fdprintf(g_tty9_fd,
"CPU time watching and print to ITTYK\n");
811 StartThread(g_monitorThreadId, &g_thcpuwatch_info);
815static void rdlist_cmd_handler(
char *cmdparam)
827 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
828 EndOfString = GetEndOfString(cmdparam_cur);
831 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
833 if (!strcmp(cmdparam_cur,
"-v"))
835 if (!strcmp(cmdparam_cur,
"-i"))
838 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP WT WID WUC\n");
839 for (i = 1; i < (is_flag_i + 127); i += 1) {
840 if (list_empty(&g_ThbaseInternal->ready_queue[i])) {
843 list_for_each (
thread, &g_ThbaseInternal->ready_queue[i], queue) {
844 if (!ReferThreadStatus(MAKE_HANDLE(
thread), &thstatus))
845 DisplayThreadInformation(
thread, is_flag_v, 0);
850static void sllist_cmd_handler(
char *cmdparam)
859 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
860 EndOfString = GetEndOfString(cmdparam_cur);
863 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
865 if (!strcmp(cmdparam_cur,
"-v"))
868 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP WT WID WUC\n");
869 list_for_each (
thread, &g_ThbaseInternal->sleep_queue, queue) {
870 if (ReferThreadStatus(MAKE_HANDLE(
thread), &thstatus) < 0) {
871 fdprintf(g_tty9_fd,
"thid = %x not found \n", MAKE_HANDLE(
thread));
874 if (
thread != g_ThbaseInternal->idle_thread)
875 DisplayThreadInformation(
thread, is_flag_v, 0);
879static void dlist_print_thread_info(
struct thread *threadstruct,
int is_verbose)
883 "%3x %07x %08x %07x %.3s ",
884 threadstruct->tag.id,
885 MAKE_HANDLE(threadstruct),
887 threadstruct->option,
888 &g_th_status_short[3 * (
char)threadstruct->status]);
891 "%06x %06x %04x %06x %3d %3d %d\n",
893 threadstruct->stack_top,
894 threadstruct->stack_size,
896 (s16)threadstruct->priority,
897 (s16)threadstruct->init_priority,
898 threadstruct->wait_usecs);
900 char *module_name_from_addr;
902 module_name_from_addr = get_module_name_from_addr((
unsigned int)threadstruct->entry);
903 if (module_name_from_addr)
904 fdprintf(g_tty9_fd,
" Name=%s\n", module_name_from_addr);
906 if (threadstruct->saved_regs && threadstruct->status != 16)
909 " PC=%06x RA=%06x SP=%06x Context_addr/mask=%08x/%08x\n",
910 threadstruct->saved_regs->pc,
911 threadstruct->saved_regs->ra,
912 threadstruct->saved_regs->sp,
913 threadstruct->saved_regs,
914 threadstruct->saved_regs->unk);
918 for (i = 0; i < (threadstruct->stack_size / 4); i += 1) {
919 if (((u32 *)threadstruct->stack_top)[i] != 0xFFFFFFFF && ((u32 *)threadstruct->stack_top)[i] != 0x11111111)
922 fdprintf(g_tty9_fd,
" Free_Stack_Size=%06x\n", i * 4);
926static void dllist_cmd_handler(
char *cmdparam)
935 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
936 EndOfString = GetEndOfString(cmdparam_cur);
939 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
941 if (!strcmp(cmdparam_cur,
"-v"))
944 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP USEC\n");
945 list_for_each (
thread, &g_ThbaseInternal->delay_queue, queue) {
946 if (ReferThreadStatus(MAKE_HANDLE(
thread), &thstatus) < 0) {
947 fdprintf(g_tty9_fd,
"thid = %x not found \n", MAKE_HANDLE(
thread));
950 if (
thread != g_ThbaseInternal->idle_thread)
951 dlist_print_thread_info(
thread, is_flag_v);
955static void ShowSemList(
char *cmdparam)
968 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
969 EndOfString = GetEndOfString(cmdparam_cur);
972 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
974 if (!strcmp(cmdparam_cur,
"-v"))
976 if (!strcmp(cmdparam_cur,
"-w"))
979 if (list_empty(&g_ThbaseInternal->semaphore)) {
982 fdprintf(g_tty9_fd,
" SEMID ATTR OPTION iCnt cCnt mCnt waitThreads\n");
983 list_for_each (sema, &g_ThbaseInternal->semaphore, sema_list) {
984 if (ReferSemaStatus(MAKE_HANDLE(sema), &semastatus) < 0) {
987 if (semastatus.numWaitThreads > 0 || !is_flag_w_count) {
990 "%3x %07x %08x %08x %05d %5d %5d %5d\n",
998 semastatus.numWaitThreads);
1000 if (semastatus.numWaitThreads > 0 && is_flag_w_count) {
1001 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP\n");
1002 list_for_each_safe (waiter, &sema->event.waiters, queue) {
1003 ReferThreadStatus(MAKE_HANDLE(waiter), &thstatus);
1004 DisplayThreadInformation(waiter, is_flag_v_count, 1);
1010static void ShowEventFlagList(
char *cmdparam)
1013 int is_flag_v_count;
1014 int is_flag_w_count;
1021 is_flag_v_count = 0;
1022 is_flag_w_count = 0;
1023 for (cmdparam_cur = cmdparam; *cmdparam_cur; cmdparam_cur = EndOfString) {
1024 EndOfString = GetEndOfString(cmdparam_cur);
1027 EndOfString = RemoveLeadingWhitespaces(EndOfString + 1);
1029 if (!strcmp(cmdparam_cur,
"-v"))
1031 if (!strcmp(cmdparam_cur,
"-w"))
1034 if (list_empty(&g_ThbaseInternal->event_flag)) {
1037 fdprintf(g_tty9_fd,
" EVID ATTR OPTION iPattern cPattern waitThreads\n");
1038 list_for_each (evf, &g_ThbaseInternal->event_flag, evf_list) {
1039 if (ReferEventFlagStatus(MAKE_HANDLE(evf), &efstatus) < 0) {
1042 if (efstatus.
numThreads > 0 || !is_flag_w_count) {
1045 "%3x %07x %08x %08x %08x %08x %5d\n",
1054 if (efstatus.
numThreads > 0 && is_flag_w_count) {
1055 fdprintf(g_tty9_fd,
" THID ATTR OPTION STS ENTRY STACK SSIZE GP CP IP\n");
1056 list_for_each_safe (waiter, &evf->event.waiters, queue) {
1057 ReferThreadStatus(MAKE_HANDLE(waiter), &thstatus);
1058 DisplayThreadInformation(waiter, is_flag_v_count, 1);
1064static void ShowMsgbxList()
1069 if (list_empty(&g_ThbaseInternal->mbox)) {
1072 fdprintf(g_tty9_fd,
" MSGID ATTR OPTION waitThreads messages\n");
1073 list_for_each (mbx, &g_ThbaseInternal->mbox, mbox_list) {
1074 ReferMbxStatus(MAKE_HANDLE(mbx), &mbxstatus);
1077 "%3x %07x %08x %08x %5d %5d\n",
1082 mbxstatus.numWaitThreads,
1083 mbxstatus.numMessage);
1087static void ShowVplList()
1092 if (list_empty(&g_ThbaseInternal->vpool)) {
1095 fdprintf(g_tty9_fd,
" VPLID ATTR OPTION Size Free waitThreads\n");
1096 list_for_each (vpl, &g_ThbaseInternal->vpool, vpl_list) {
1097 ReferVplStatus(MAKE_HANDLE(vpl), &vplstatus);
1100 "%3x %07x %08x %08x %06x %06x %5d\n",
1107 vplstatus.numWaitThreads);
1111static void ShowFplList()
1116 if (list_empty(&g_ThbaseInternal->fpool)) {
1119 fdprintf(g_tty9_fd,
" FPLID ATTR OPTION BlkSIze AllBlocks FreeSize waitThreads\n");
1120 list_for_each (fpl, &g_ThbaseInternal->fpool, fpl_list) {
1121 ReferFplStatus(MAKE_HANDLE(fpl), &fplstatus);
1124 "%3x %07x %08x %08x %06x %06x %06x %5d\n",
1129 fplstatus.blockSize,
1130 fplstatus.numBlocks,
1131 fplstatus.freeBlocks,
1132 fplstatus.numWaitThreads);
1136static void DumpReadyQueue()
1142 fdprintf(g_tty9_fd,
"ready queue ----\n");
1143 for (i = 0; i < 127; i += 1) {
1144 if (list_empty(&g_ThbaseInternal->ready_queue[i])) {
1148 fdprintf(g_tty9_fd,
" %3d:%x ", i, &g_ThbaseInternal->ready_queue[i]);
1149 list_for_each (
thread, &g_ThbaseInternal->ready_queue[i], queue)
1150 fdprintf(g_tty9_fd,
" %d(%x) ",
thread->tag.id,
thread);
1151 fdprintf(g_tty9_fd,
"\n");
1154 fdprintf(g_tty9_fd,
"ready map = ");
1155 for (i = 0; i < 4; i += 1) {
1156 fdprintf(g_tty9_fd,
"%08x ", g_ThbaseInternal->queue_map[i]);
1158 fdprintf(g_tty9_fd,
"\n");
1161static void ToggleThswdisp(
const char *cmdparam)
1163 if (!strcmp(cmdparam,
"on"))
1164 g_ThbaseInternal->debug_flags |= 1u;
1165 if (!strcmp(cmdparam,
"off"))
1166 g_ThbaseInternal->debug_flags &= ~1u;
1167 if (isdigit(*cmdparam))
1168 g_ThbaseInternal->debug_flags = strtol(cmdparam, 0, 10);
1171static void ToggleThLED(
const char *cmdparam)
1173 if (!strcmp(cmdparam,
"on"))
1174 g_ThbaseInternal->debug_flags |= 0x20u;
1175 if (!strcmp(cmdparam,
"off"))
1176 g_ThbaseInternal->debug_flags &= ~0x20u;
1177 if (isdigit(*cmdparam))
1178 g_ThbaseInternal->debug_flags = strtol(cmdparam, 0, 10);
1181static void ToggleWarnDisp(
const char *cmdparam)
1183 if (!strcmp(cmdparam,
"on"))
1184 g_ThbaseInternal->debug_flags |= 8u;
1185 if (!strcmp(cmdparam,
"off"))
1186 g_ThbaseInternal->debug_flags &= ~8u;
1187 if (isdigit(*cmdparam))
1188 g_ThbaseInternal->debug_flags = strtol(cmdparam, 0, 10);
1191static void DumpThreads()
1195 fdprintf(g_tty9_fd,
"===============================\n");
1196 fdprintf(g_tty9_fd,
"tcb list ----\n");
1197 list_for_each (
thread, &g_ThbaseInternal->thread_list, thread_list) {
1200 " %d: tcb=%x sts=0x%x pri=%d ",
1205 if ((
char)
thread->status == 4)
1206 fdprintf(g_tty9_fd,
"wType = %x:%x ", (s16)
thread->wait_type,
thread->wait_usecs);
1207 if ((
char)
thread->status != 1)
1208 fdprintf(g_tty9_fd,
"contx=%x sp=%x",
thread->saved_regs,
thread->saved_regs->sp);
1209 fdprintf(g_tty9_fd,
"\n");
1211 fdprintf(g_tty9_fd,
"sleep list ----\n");
1212 if (list_empty(&g_ThbaseInternal->sleep_queue)) {
1215 fdprintf(g_tty9_fd,
" %x: ", &g_ThbaseInternal->sleep_queue);
1216 list_for_each (
thread, &g_ThbaseInternal->sleep_queue, queue) {
1217 fdprintf(g_tty9_fd,
" %d(%x) ",
thread->tag.id,
thread);
1219 fdprintf(g_tty9_fd,
"\n");
1222static void ShowAlarmList()
1233 if (!list_empty(&g_ThbaseInternal->alarm)) {
1234 list_for_each (
alarm, &g_ThbaseInternal->alarm, alarm_list) {
1239 list_for_each (
alarm, &g_ThbaseInternal->alarm, alarm_list) {
1240 alarm_ents[i].m_alarm_id =
alarm->tag.id;
1241 alarm_ents[i].m_target.lo =
alarm->target;
1242 alarm_ents[i].m_target.hi =
alarm->target >> 32;
1243 alarm_ents[i].m_cb =
alarm->cb;
1244 alarm_ents[i].m_userptr =
alarm->userptr;
1249 fdprintf(g_tty9_fd,
" NUM TIME HANDLER COMMON\n");
1250 for (i = 0; i < alarm_cnt; i += 1) {
1253 "%3x %08x_%08x %06x, %08x\n",
1254 alarm_ents[i].m_alarm_id,
1255 alarm_ents[i].m_target.hi,
1256 alarm_ents[i].m_target.lo,
1258 alarm_ents[i].m_userptr);
1262static void freemem_cmd_handler()
1264 u32 TotalFreeMemSize;
1266 TotalFreeMemSize = QueryTotalFreeMemSize();
1269 "IOP system memory 0x%x(%d) byte free, Max free block size 0x%x\n",
1272 QueryMaxFreeMemSize());
1275static void ThmonMainThread(
void *arg)
1284 fdprintf(g_tty9_fd,
"\n\n========= simple thread monitor program =========\n");
1285 fdprintf(g_tty9_fd,
"help command is 'help'\n");
1286 g_PreviousCommand[0] = 0;
1288 fdprintf(g_tty9_fd,
" > ");
1289 if (!fdgets(g_CommandBuffer, g_tty9_fd))
1291 PrintMessage(g_CommandBuffer);
1292 if (!strlen(g_CommandBuffer))
1294 if (!strcmp(g_CommandBuffer,
"/")) {
1295 cmdbufsrc = g_PreviousCommand;
1296 cmdbufdst = g_CommandBuffer;
1298 cmdbufsrc = g_CommandBuffer;
1299 cmdbufdst = g_PreviousCommand;
1301 strcpy(cmdbufdst, cmdbufsrc);
1302 cmdbuf_trim = RemoveLeadingWhitespaces(g_CommandBuffer);
1303 cmdparam = RemoveLeadingWhitespaces(GetEndOfString(cmdbuf_trim));
1304 if (*cmdbuf_trim ==
'#')
1306 if (!strncmp(
"quit", cmdbuf_trim, strlen(cmdbuf_trim)))
1308 else if (!strncmp(
"?", cmdbuf_trim, strlen(cmdbuf_trim)) || !strncmp(
"help", cmdbuf_trim, strlen(cmdbuf_trim)))
1309 DisplayHelpMessage();
1310 else if (!strncmp(
"thlist", cmdbuf_trim, strlen(cmdbuf_trim)))
1311 thlist_cmd_handler(cmdparam);
1312 else if (!strncmp(
"rdlist", cmdbuf_trim, strlen(cmdbuf_trim)))
1313 rdlist_cmd_handler(cmdparam);
1314 else if (!strncmp(
"sllist", cmdbuf_trim, strlen(cmdbuf_trim)))
1315 sllist_cmd_handler(cmdparam);
1316 else if (!strncmp(
"dllist", cmdbuf_trim, strlen(cmdbuf_trim)))
1317 dllist_cmd_handler(cmdparam);
1318 else if (!strncmp(
"rtlist", cmdbuf_trim, strlen(cmdbuf_trim)))
1319 rtlist_cmd_handler(cmdparam);
1320 else if (!strncmp(
"semlist", cmdbuf_trim, strlen(cmdbuf_trim)))
1321 ShowSemList(cmdparam);
1322 else if (!strncmp(
"evlist", cmdbuf_trim, strlen(cmdbuf_trim)))
1323 ShowEventFlagList(cmdparam);
1324 else if (!strncmp(
"msglist", cmdbuf_trim, strlen(cmdbuf_trim)))
1326 else if (!strncmp(
"vpllist", cmdbuf_trim, strlen(cmdbuf_trim)))
1328 else if (!strncmp(
"fpllist", cmdbuf_trim, strlen(cmdbuf_trim)))
1330 else if (!strncmp(
"almlist", cmdbuf_trim, strlen(cmdbuf_trim)))
1332 else if (!strncmp(
"freemem", cmdbuf_trim, strlen(cmdbuf_trim)))
1333 freemem_cmd_handler();
1334 else if (!strncmp(
"cpuwatch", cmdbuf_trim, strlen(cmdbuf_trim)))
1335 cpuwatch_cmd_handler(cmdparam);
1336 else if (!strncmp(
"thwatch", cmdbuf_trim, strlen(cmdbuf_trim)))
1337 thwatch_cmd_handler(cmdparam);
1338 else if (!strncmp(
"thswdisp", cmdbuf_trim, strlen(cmdbuf_trim)))
1339 ToggleThswdisp(cmdparam);
1340 else if (!strncmp(
"thled", cmdbuf_trim, strlen(cmdbuf_trim)))
1341 ToggleThLED(cmdparam);
1342 else if (!strncmp(
"warndisp", cmdbuf_trim, strlen(cmdbuf_trim)))
1343 ToggleWarnDisp(cmdparam);
1344 else if (!strncmp(
"dumpthread", cmdbuf_trim, strlen(cmdbuf_trim)))
1346 else if (!strncmp(
"dumpready", cmdbuf_trim, strlen(cmdbuf_trim)))
1349 fdprintf(g_tty9_fd,
" ?? \n");
int CpuResumeIntr(int state)
int CpuSuspendIntr(int *state)