Stack Location ("/proc/<tpid>/maps" vs. "/proc/thread-self/maps" vs. "pthread_attr_getstack()")

I_Love_Linux_2022

New Member
Joined
Nov 12, 2022
Messages
1
Reaction score
0
Credits
26
Dear "Linux Users",

Kindly, I am writing an application where I need to know exactly the start of the thread stack after setting it using "posix_memalign()" and "pthread_attr_setstack()".

The problem is that I am getting different values using 3 different methods as follows:

1)
tid = syscall(SYS_gettid);
sprintf(filename, "cat /proc/%d/maps", tid);
system(filename);

2)
system("cat /proc/thread-self/maps");

3)
pthread_attr_getstack();

The following code is my thread:

void* vidMainThread(void* ptr)
{
int fd;
pid_t tid;
char filename[128];

/* First Method */
tid = syscall(SYS_gettid);
printf("Tread ID. SYS_gettid() = %d\r\n", tid);
sprintf(filename, "cat /proc/%d/maps", tid);
system(filename);

printf("\r\n\n", tid);

/* Second Method */
system("cat /proc/thread-self/maps");

printf("\r\n\n", tid);

/* Third Method */
vidReserveStack();
}

void vidReserveStack(void)
{
uint8_t au8Test[1024];

au8Test[1023] = (uint8_t)0x5A;
printf("au8Test[1023] = 0x%X, Address = 0x%X\r\n", au8Test[1023], &au8Test[1023]);

au8Test[0] = (uint8_t)0x5A;
printf("au8Test[0] = 0x%X, Address = 0x%X\r\n", au8Test[0], &au8Test[0]);
}

The output is as follows:

/* First Method */
fffffacc9000-fffffacea000 rw-p 00000000 00:00 0 [stack]

/* Second Method */
ffffc4a41000-ffffc4a62000 rw-p 00000000 00:00 0 [stack]

/* Third Method */
au8Test[1024] = 0x00, Address = 0x8FEAF90F
au8Test[0] = 0x5A, Address = 0x8FEAF510

Note that:

The output of "pthread_attr_getstack()" is:

StackStart = 0x8FE91000
StackSize = 131072

which is considered logical when we compare it with the output of the "Third Method" as follows:

StackEnd = StackStart + StackSize = 0x8FEB1000 which is higher than and really close to "au8Test[1023]" however, is really far away from the output of the first two methods.


Do you know why I am getting these different values when using different methods?

Thank you very much.
Best regards.
 



Members online


Top