Counter for main() in Linux OS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Counter for main() in Linux OS



i want to implement a counter in Linux which keep a track of how many times
main() is called by any process.



when i start this counter thing as a process, from that time it will tell me how many times main() was called not by my program but in the entire OS system



example:
i start this as a daemon and then i create a simple code


#include <stdio.h>

int main()
//some code
return 0;



Now here main is called so the counter will increment by one.



Can anyone explain me how can this be done.?



thanks





You mean the main of a program you created or any main() function called by the kernel?
– Pierre Podevin
Aug 10 at 7:26





main called by all the programs in userspace not just created by me but even any daemons
– aditya
Aug 10 at 7:28






What would be the point? Basically, that's a program call counter. Since basically all programs contain a main as entry point.
– Pierre Podevin
Aug 10 at 7:31





so can i access this program call counter and keep a track of it? do i need to make the changes in kernel source or can be done in userspace??
– aditya
Aug 10 at 7:39




2 Answers
2



You might want to take a look at: Proc connector and socket filters



The Proc Connector and Socket Filters Posted on February 9, 2011 by scott



The proc connector is one of those interesting kernel features that most people rarely come across, and even more rarely find documentation on. Likewise the socket filter. This is a shame, because they’re both really quite useful interfaces that might serve a variety of purposes if they were better documented.



The proc connector allows you to receive notification of process events such fork and exec calls, as well as changes to a process’s uid, gid or sid (session id). These are provided through a socket-based interface by reading instances of struct proc_event defined in the kernel header....



main() is just a abstract code; assembler uses functions' name as a hint, and converts them into numbers. So you can't implement such counter.



main() is usually called once, what you mean may be a program which counts how many programs are executed.



There is popen in Linux which performs the given command, and yields the result as FILE *, so you can execute the ps command and parse it to get the list of the processes. You can continuously invoke popen and count the number of programs.


popen


ps


char Buffer[1024];
sprintf(Buffer,"ps ");
FILE *ptr = popen(Buffer, "r");
if(NULL != ptr)

while(fgets(Buffer, sizeof(Buffer),ptr));
pclose(ptr);

// Now the list of processes are in Buffer





I just want a counter exclusively for this specific function i.e main() . i know it seems useless but i am not interested in the use but instead the implementation of the counter
– aditya
Aug 10 at 7:51





I mean, because assembler converts code into machine code and optimizer could change the structure, it is impossible to know where main() exactly is.
– paxbun
Aug 10 at 7:58






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered