How to get the struct info of a struct - c

Is it possible to get the struct info (that is, the keys) of any struct? Or is required that you go to the manual page to read up what the actual structure is for that object. Take the following example:
struct stat stats;
stat(filepath, &stats);
printf("Size: %lld\n", stats.st_size);
Is it possible to do something like stats.keys(), or whatever a potentially equivalent operation would be to see the inner structure of a struct ?

You can read the man page, or you can read the header; there are no built-in introspection facilities in the C language that will inspect structs for their attributes in any convenient way.
In theory, if you compile the executable with debugging symbols a debugger might be able to tell you some of this (after loading and parsing the executable and its symbols), but that's generally going to be less convenient than just reading the docs.

Related

Is there any way to print all nested struct size in C?

I have a case where there are multiple structures contained in one structure.
Small example:
typedef struct data {
int x;
} data_t;
typedef struct info {
data_t data_element[10];
int y;
} info_t
Is there any way to print all struct sizes which are members of info_t?
Not without knowing what those types are. C does not have reflection (the way that java or c# do, for example).
If you do know the types it's simply a matter of using sizeof and a print function.
This is impossible using the C language alone, since it does not provide for such introspection. All you can do is iterate over the struct members and print their sizes.
That said, a decade ago, I had to do just this for large complicated struct of several hundred members. I finally got the required information from parsing the debugging section of the object file. I don't have details anymore, but you will need to compile with the -g flag and then dump debug output with an appropriate tool. Try looking at the readelf and objdump utilities and manual pages.

How to know Darwin kernel scheduler time slice?

On Linux, sched.h contains the definition of
int sched_rr_get_interval(pid_t pid, struct timespec * tp);
to get the time slice of a process. However the file shipping with OS X El Capitan doesn't hold that definition.
Is there an alternative for this on OS X?
The API's related to this stuff are pretty byzantine and poorly documented, but here's what I've found.
First, the datatypes related to RR scheduling seem to be in /usr/include/mach/policy.h, around line 155. There's this struct:
struct policy_rr_info {
...
integer_t quantum;
....
};
The quantum is, I think, the timeslice (not sure of units.) Then grepping around for this or related types defined in the same place, I found the file /usr/include/mach/mach_types.def, which says that the type struct thread_policy_t contains a field policy_rr_info_t on line 203.
Next, I found in /usr/include/mach/thread_act.h the public function thread_policy_get, which can retrieve information about a thread's policy into a struct thread_policy_t *.
So, working backwards. I think (but haven't tried at all) that you can
Use the thread_policy_get() routine to return information about the thread's scheduling state into a thread_policy_t
That struct seems to have a policy_rr_info_t sub-substructure
That sub-structure should have a quantum field.
That field appears to be the timeslice, but I don't know about the units.
There are no man pages for this part of the API, but this Apple Developer page explains at least a little bit about how to use this API.
Note that this is all gleaned from just grepping the various kernel headers, and I've definitely not tried to use any of these APIs in any actual code.

How to "look into" a struct?

Take the FILE type defined in stdio.h for example: Is there any way to get the information about its fields(name, size, offset, etc) without taking a look at the header? Sometimes it'll be convenient to have such a function/macro to check the components of a struct.
No.
There's no meta data associated with data structures in C, all of that is lost when compiling.
And it's perfectly possible, since FILE is opaque, that no public header actually has the definition. It could just be typedef struct __FILE FILE; in the library header, and then all the details be kept on the inside, possibly in code you don't even have the source to.
The simple answer is no. You have to have the source code.
In a C based structure, the data is stored in a way that is not "self defining" - you must know the structure definition to interpret the data. This reduces the size of the data to its bare minimum, and makes access faster, provided that your program understands the structure.
The answer is no.
Whenever, i needed to find information about a struct's data member, Header File and comments over there were sufficient for me.
And you can't have a function/macro to check the components of a struct because there is no meta data associated with the variables and procedures in C.
The only way to see the information about it's fields would be to have the actual source, or an api/software document that explains how the struct is organized.
It is not possible. C does not retain this kind of information, likely due to the "you don't pay for what you don't use" principle; it also keeps the language simple and portable.
What you can do at most is querying the position of a given member in the struct, through the offsetof standard macro. But you have to know the name of the field.

What do FILE struct members mean exactly in C?

typedef struct _iobuf{
char* _ptr;
int _cnt;
char* _base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
} FILE;
I try to print them but I don't understand what they mean.
Here What exactly is the FILE keyword in C? He said "Some believe that nobody in their right mind should make use of the internals of this structure." but he didn't explain what they mean.
Look at the source code for your system's run-time libraries that implement the FILE-based IO calls if you want to know what those fields mean.
If you write code that depends on using those fields, it will be non-portable at best, utterly wrong at worst, and definitely easy to break. For example, on Solaris there are at least three different implementations of the FILE structure in just the normal libc runtime libraries, and one of those implementations (the 64-bit one) is opaque and you can't access any of the fields. Simply changing compiler flags changes which FILE structure your code uses.
And that's just one one version of a single OS.
_iobuf::_file can be used to get the internal file number, useful for functions that require the file no. Ex: _fstat().

How does a compiled definition of struct look like in C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How does a compiled C++ class look like?
Hi all,
bash$cat struct.c
struct test
{
int i;
float f;
};
bash$gcc -c struct.c
The object file struct.o is of elf format. I am trying to understand what does this object file contain. The source code is just a definition of a struct. There is nothing executable here so there should be nothing in text, and there is no data really either.
So where does the definition of struct go really?
I tried using;
readelf -a struct.o
objdump -s struct.o
but don't quite understand this.
Thanks,
Jagrati
So where does the definition of struct
go really?
Struct definition usually goes to /dev/null. C does not have any introspection features, so struct definition is not needed at run time. During compilation, calls to struct fields are converted to numeric offsets, eg. x->f would be compiled to equivalent of *((void*)x + sizeof(int)). That's why you need to include headers every time you use struct.
There is nothing. It does not exist. You have created nothing and used nothing.
The definition of the struct is used at compile time. That definition would normally be placed in a non-compiled header file. It is when a struct is used that some code is generated. The definition affects what the compiler produces at that point.
This, among other reasons, is why compiling against one version of a library and then using another version at runtime can crash programs.
structs are not compiled, they are declared. Functions get compiled though.
I'm not an expert and I can't actually answer the question... But I thought of this.
Memory is memory: if you use 1 byte as integer or char, it is still one byte. The results depends only on the compiler.
So, why can't be the same for structs? I mean, the compiler probably will calculate the memory to allocate (as your computer probably will allocate WORDS of memory, not bytes, if your struct is 1 byte long, probably 3 bytes will be added allowing the allocation of 4 bytes word), and then struct will just be a "reference" for you when accessing data.
I think that there is no need to actually HAVE something underneath: it's sufficient for the compiler to know that, in compile time, if you refer to field "name" of your struct, it shall treat is as an array of chars of length X.
As I said, I'm not expert in such internals, but as I see it, there is no need for a struct to be converted in "real code"... It's just an annotation for the compiler, which can be destroyed after the compilation is done.

Resources