[En-Nut-Discussion] Can a thread determine its stack space?

Pedro Duarte PDuarte at milenio3.pt
Thu Sep 23 11:34:37 CEST 2010


Hi,

I have used the following code to determine the max stack usage, bay be you can use parts of it

void DebugThreadInfo (void)
{
	NUTTHREADINFO *tdp;
 	uint32_t *ptr32;
 	uint8_t *ptr8;
 	char valid_signature;
 	uint32_t free_stack;
 	
 	printf ("\nid         name     stack      memory     sp-mem Valid free_stack");
 	//         1234567890 12345678 1234567890 1234567890 123456 12345 1234567890
	for (tdp = nutThreadList; tdp; tdp = tdp->td_next)
	{
		printf ("\n%10p %8s %10p %10p %6lu",
			(void *) tdp,
			tdp->td_name,
			(void *) tdp->td_sp,
			(void *) tdp->td_memory,
			(uint8_t *) tdp->td_sp - (uint8_t *) tdp->td_memory
			);
		
		ptr32 = (uint32_t *) tdp->td_memory;
		if ((ptr32[0] == DEADBEEF) && (ptr32[1] == DEADBEEF) && (ptr32[2] == DEADBEEF) && (ptr32[3] == DEADBEEF))
			valid_signature = 'V';
		else
			valid_signature = 'E';
		
		ptr8 = (uint8_t *) tdp->td_memory + 16;
		free_stack = 0;
		while (!*ptr8)
		{
			free_stack++;
			ptr8++;
		}
		printf (" %5c %10lu", valid_signature, free_stack);
	}
}

Best regards
Pedro

-----Mensagem original-----
De: en-nut-discussion-bounces at egnite.de [mailto:en-nut-discussion-bounces at egnite.de] Em nome de Ulrich Prinz
Enviada: quarta-feira, 22 de Setembro de 2010 21:13
Para: Ethernut User Chat (English)
Assunto: [En-Nut-Discussion] Can a thread determine its stack space?

Hi!

Can a stack determine its own stack space? I.e. I want a thread to 
monitor its own left free stack space. But I cannot find out how to do that.

There are functions that are only available if some debug settings are 
enabled. And some of them are never available as the #defines are never 
set and not available through nutconf.

But enabling heap debugging doesn't solve the problem if a thread 
silently hangs up while the rest of the system is still running fine. 
And the output of NutHeapAvailable() and NutHeapRootRegionAvailable()
in the main thread still show plenty of memory.

Is there a mechanism to find a mutex where a thread is blocked on?

Enabling debugging has another problem too as it multiplies some stacks 
for init and main and others with a factor what has a general impact on 
the system. So it is difficult to find the problem in a surrounding that 
is different from what you target on.
This might also get difficult if your application needs most of FLASH 
and RAM.

Best regards
Ulrich
_______________________________________________
http://lists.egnite.de/mailman/listinfo/en-nut-discussion



More information about the En-Nut-Discussion mailing list