Why wrap functions? - linux

Why does the Linux kernel sometimes implement multiple versions of a function with very similar names that just wrap another function? For example, here:
static void clocksource_select(void)
{
__clocksource_select(false);
}
static void clocksource_select_fallback(void)
{
__clocksource_select(true);
}

The example you gave is not a very good example, because it has nothing to do with the Linux kernel. This is just basic software engineering.
When you have two functions that need to have very close functionality, there are several paths you can take.
You can implement the function twice. We don't like to do that, as it creates code duplication. It also means that if you need to change something in the common area of the code, you need to remember to change it at two places.
You can split the common code into its own function, and call that function from each of the functions. That is the best solution if it is possible. The problem is that it is not always possible. It might not be possible because the common code needs too much context, or because it needs to be spread out across the function. Which brings us right to:
Create an internal "common" function, with an argument telling which functionality to provide. Just write the code, and put an if where the two functions need to do something different. That is the path the kernel took in your example.
With that said, there is another case, specific to the Linux kernel, where two functions really do seem to be almost identical. On the i386 platform, the stat system call is implemented not twice, but three times:
oldstat syscall number 18
stat syscall number 106
stat64 syscall number 195
The reason for that is that the Linux kernel promises complete backwards compatibility over its user space kernel interface. When a function has to be superseded for some reason, as happened to stat not once, but twice (three times if you count fstatat), the old system call entry needs to be kept around and remain operational.
If you look at the actual implementation, however, you will notice that there is very little difference between them, and they all end up calling, pretty much, the same function.

Related

Identify heavily called function

I search for a performance problem with procmon. This tool lists a lot of operation sequences like
CreateFile
QueryInformationVolume
QueryAllInformationFile
CloseFile
All operations are performed on the same file somewhere in the ProgramData tree. The QueryAllInformationFile fails with BUFFER OVERFLOW, the others succeed.
My first thought was that it could be related to a call of the API function GetVolumeInformation. But this API function rejects any call with a RootPathName that is not a drive name but a file name. Therefore it can't be used to call QueryInformationVolume for the file.
I have a huge amount of source code and want to identify the reason for this repeated sequence. Involved packages are e.g. the MXE cross compiler suite, some g-libraries like glibmm, glibio and others. The actual problem occurs when a program "PulseView" is running, that has been compiled with MXE.
How can I identify the API function that is responsible for the operations?

RPG program error: Error MCH3601 was detected in file

We have been facing a very strange issue with one of our RPGLE programs that bombs intermittently with the subjected error.
This happens specifically at a line where a write operation is performed to a subfile record format. I have debugged and checked all the values assigned to variables during runtime and could not find absolutely no issues. As per the https://www.ibm.com/support/pages/node/644069 IBM page, I can only assume that this might be related to the parameter definitions of the programs called within the RPG. But I have checked the parameters of each and every prototyped program call and everything seems to be in sync.
Can some one please guide on the direction to go to find out the root cause of this problem?
But I have checked the parameters of each and every prototyped program
call
Assuming you're using prototypes properly, ie. there is one prototype defined in a separate source member and it is /INCLUDE into BOTH the caller and the callee...
Then prototype calls aren't the problem, as long as you're properly handling any *OMIT and *NOPASS parameters.
Look at any old style CALL or CALLB calls and anyplace you're not using prototypes properly...meaning there's a explicit PR coded in both caller & callee.
Note that you it's not just old-style calls made by the program that bombs, it's calls made anywhere down the call chain.
And if the program is repeatedly called with LR=*OFF or without reclaiming resources, then it could be any old style calls up the call chain also.
Lastly, old style calls include any made by CL or CLLE programs.
Good luck!

What is the difference between these methods to obtain resource usage?

In Linux, we can use two ways to find out resources used like time, page faults, page swaps, context switching. One of the ways is using the getrusage() function, the other method is using the command /usr/bin/time -v [command to check usage]. What is the difference between these ways of finding resource usage?
When you use a command like time(1) it must use a system call such as getrusage(2) by way of its system library wrapper. This is building a request with the right system call number and structure to indicate it wants rusage information for the processes' children.
For compatibility across UNIX/POSIX operating systems, which specific functions are chosen to build a command is done from a hierarchy of options to adequately cover the OSes the command runs on. (Some OSes may not implement everything or have various quirks.)
In time's case it will prefer to group waiting for the child and getting its usage into calling wait3 which in turn is implemented as a wrapper around the even more complex wait4, which has its own systemcall number.
Both wait3/4 and getrusage fill the same rusage structure with information, and since time only directly calls one child process, calling wait3() as it does or breaking this into less featured calls like wait();getrusage(RUSAGE_CHILDREN) is in essence the same. Therefore, time is effectively displaying the same data as getrusage provides (together with some more general data it assembles from the system like real time elapsed using calls to gettimeofday).
The real difference among the systemcall wrapper functions is:
getrusage has another argument allowing a process to look at itself so far.
wait4 could target just one direct child and that child's decendents.
wait3 is a simplification of either wait4 or using wait();getrusage() that is not as versatile as either but just good enough for the time(1) command as it is implemented. (Therefore wait3 is the simplest and safest option for time to use on OSes where it is available.)
To verify they are the same, one could change time to an alternate version, recompile and compare:
while ((caught = wait3 (&status, 0, NULL)) != pid)
{
if (caught == -1) {
getrusage(RUSAGE_CHILDREN, &resp->ru);
return 0;
}
}

How can I get a running thread's start address on linux?

Problem Statement
I'm trying to get the address of a running thread's start_routine as passed in the pthread_create() call.
Research so far
It is apparently not in /proc/[tid]/stat or /proc/[tid]/status.
I found that start_routine is a member of struct pthread and gets set by pthread_create.[1]
If I knew the address of this struct, I could read the start_routine address.
I also found td_thr_get_info defined in the debugging library thread_db.h.[2]
It fills a struct with information about the thread, including the start function.[3] But it needs a struct td_thragent as an argument and I don't know how to create it properly.
Links
[1] http://fxr.watson.org/fxr/source/nptl/pthread_create.c?v=GLIBC27;im=excerpts#L455
[2] http://fxr.watson.org/fxr/source/nptl_db/td_thr_get_info.c?v=GLIBC27#L27
[3] See comment, because I'm not allowed to post more than 2 links.
You probably can't, and I could even imagine a very wild scenario where it could not exist at the moment you are querying it.
Let's suppose that the initial thread start routine void*foo_start(void*) is in some dlopen-ed dynamic shared library  libfoo.so.
Let's imagine that foo_start is making a tail-recursive call to bar, and that bar function is dlclose-ing libfoo.so and later calling some of your routine querying that start. It is an wild address in some defunct segment (which has been munmap-ed by dlclose called by bar)!
So, even if you hack your libc to retrieve the start routine of a thread, that does not make much sense. BTW, you could look into MUSL libc, its src/thread/pthread_create.c file is quite readable.
NB: on some occasions, recent GCC (e.g. 4.8 or 4.9) when asked to optimize a lot (e.g. -O3) are able to generate tail recursive calls from C code.

How do you safely read memory in Unix (or at least Linux)?

I want to read a byte of memory but don't know if the memory is truly readable or not. You can do it under OS X with the vm_read function and under Windows with ReadProcessMemory or with _try/_catch. Under Linux I believe I can use ptrace, but only if not already being debugged.
FYI the reason I want to do this is I'm writing exception handler program state-dumping code, and it helps the user a lot if the user can see what various memory values are, or for that matter know if they were invalid.
If you don't have to be fast, which is typical in code handling state dump/exception handlers, then you can put your own signal handler in place before the access attempt, and restore it after. Incredibly painful and slow, but it is done.
Another approach is to parse the contents of /dev/proc//maps to build a map of the memory once, then on each access decide whether the address in inside the process or not.
If it were me, I'd try to find something that already does this and re-implement or copy the code directly if licence met my needs. It's painful to write from scratch, and nice to have something that can give support traces with symbol resolution.

Resources