using dlsym for tracking mmap - mmap

I am trying to measure how much memory does some program mmaps.
I am using the following code:
void * mmap (void * addr, size_t len, int prot, int flags, int fildes, off_t off) {
printf("in mmap1\n");
static void *(*realfn)(void*, size_t, int, int, int, off_t)
= (void *(*)(void*, size_t, int, int, int, off_t))dlsym(RTLD_NEXT, "mmap");
printf("in mmap2\n");
void * result = (*realfn)(addr, len, prot, flags, fildes, off);
if ((int) result != -1) {
stats.add (len);
}
return result;
}
stats is some global variable that saves the stats.
I turn this code into a shared object and link with it. For some programs it works, but for
some programs, in mmap1 is printed once, then in mmap1 is printed again, and then nothing
happens (the program gets stuck at this point until I kill it, never reaching in mmap2.
I read about dlsym, but I can't find the problem causing this.
I would love to here from someone more experienced.
Thanks.

Related

Print UTF-32 string with wprintf

I am migrating some code from using wchar_t to char32_t, and when compiling with the -Werror=pointer-sign flag set, I am getting the following issue:
// main.c
#include <uchar.h>
#include <wchar.h>
int main(void) {
wprintf(U"some data\n");
}
Compiling: gcc -std=c11 -Werror=pointer-sign main.c
Output:
main.c: In function ‘main’:
main.c:5:10: error: pointer targets in passing argument 1 of ‘wprintf’ differ in signedness [-Werror=pointer-sign]
wprintf(U"some data\n");
^~~~~~~~~~~~~~
In file included from main.c:2:
/usr/include/wchar.h:587:12: note: expected ‘const wchar_t * restrict’ {aka ‘const int * restrict’} but argument is of type ‘unsigned int *’
extern int wprintf (const wchar_t *__restrict __format, ...)
^~~~~~~
To remedy this, I can do:
wprintf((const int *)U"some data\n");
//or
printf("%ls\n", U"some data");
Although this is quite a pain. Is there a nice and easy way to do this? What is the real difference between const unsigned int* vs const signed int*, aside from the data type it points to? Is this possibly dangerous, or should I just disable the flag altogether?
char32_t is an unsigned type.
wchar_t is either signed or unsigned, depending on implementation. In your case, it is signed.
You can't pass a pointer-to-unsigned where a pointer-to-signed is expected. So yes, you need a type-cast, however you should be casting to const wchar_t *, since that is what wprintf() actually expects (wchar_t just happens to be implemented as an int on your compiler, but don't cast to that directly):
wprintf((const wchar_t *)U"some data\n");
It doesn't get much cleaner than that, unless you wrap it in your own function, eg:
int wprintf32(const char32_t *str, ...)
{
va_list args;
va_start(args, str);
int result = vwprintf((const wchar_t *)str, args);
va_end(args);
return result;
}
wprintf32(U"some data\n");
Note that this code will not work properly at all on platforms where sizeof(wchar_t) < sizeof(char32_t), such as Windows. On those platforms, where sizeof(wchar_t) is 2, you will have to actually convert your string data from UTF-32 to UTF-16 instead, eg:
int wprintf32(const char32_t *str, ...)
{
va_list args;
int result;
va_start(args, str);
if (sizeof(wchar_t) < sizeof(char32_t))
{
wchar_t *str = convert_to_utf16(str); // <-- for you to implement
result = vwprintf(str, args);
free(str);
}
else
result = vwprintf((const wchar_t *)str, args);
va_end(args);
return result;
}
wprintf32(U"some data\n");

C How can I write a value to an entire data structure, rather than just an element?

I am attempting to write a little C test program for reading data from a vending machine. I have the circuit hooked up, now the hard part is the code.
The machine uses UART that calls for 9 total data bits. Here is my code. Attempting to write directly to the full 9 bit data type does not work, but writing to an element of it does.
struct nineBit { //To make 9 bit character types for an array.
unsigned int data : 8; //8 data bits.
unsigned int mode : 1; //1 'mode' bit.
} data[35]; //Make an array of 9 bit data to store the incoming data block.
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
data[0] = Serial1.read(); //Works if 'data[0].data is entered instead.
//How can I transfer this all in one command?
}
Errors are
rx.cpp: In function 'void loop()':
rx.cpp:11:12: error: no match for 'operator=' (operand types are 'nineBit' and 'int')
void setup() {
^
rx.cpp:11:12: note: candidates are:
rx.cpp:1:8: note: nineBit& nineBit::operator=(const nineBit&)
^
rx.cpp:1:8: note: no known conversion for argument 1 from 'int' to 'const nineBit&'
rx.cpp:1:8: note: nineBit& nineBit::operator=(nineBit&&)
rx.cpp:1:8: note: no known conversion for argument 1 from 'int' to 'nineBit&&'
make[1]: *** [../build/target/user/platform-6rx.o] Error 1
make: *** [user] Error 2
Error: Could not compile. Please review your code.
I assume you are using arduino or something similar. So Serial1.read() returns char. char is a signed 1 byte (8 bits) field. And your struct nineBit has 9 bits. How do you expect of writing 8 bits to 9 bited structure?
A note about your structure: it doesn't have size equal 9 bits. Instance of any variable can have size evaluated in bytes only. So if you want to store 9 bits you have to create a two byted structure or more.
And in fact sizeof(nineBit) equals 4 because your bit field have unsigned int type. If you want to reduce size of your structure you have to change bit field type to either short or char.
Let's assume your serial transports two bytes per every structure. So you have to read two bytes and then assign them:
struct nineBit {
char data : 8; //8 data bits.
char mode : 1; //1 'mode' bit.
} data[35];
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
char byte1=Serial1.read();
char byte2=Serial1.read();
data[0].data=byte1;
data[0].mode=byte2;
}
If you want to use only a single line you have to write a C function or overload operator= if you use C++.
C way
struct nineBit {
char data : 8; //8 data bits.
char mode : 1; //1 'mode' bit.
} data[35];
void writeToNineBit(struct nineBit *value){
char byte1=Serial1.read();
char byte2=Serial1.read();
value->data=byte1;
value->mode=byte2;
}
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
writeToNineBit(data+0); // or &data[0].. 0 is an index in array..
}
C++ way
struct nineBit {
char data : 8; //8 data bits.
char mode : 1; //1 'mode' bit.
// assume you have to assign data without mode..
nineBit& operator=(char b){
this->data=b;
}
} data[35];
void setup() {
Serial1.begin(9600, SERIAL_9N1); //Start the UART.
}
void loop() {
data[0]=Serial1.read(); // now it works cause you have operator overloading in your structure..
}

Compilation error on blocks with return type

I have the following block code
typedef BOOL(^FieldValidationBlock)(NSString *);
FieldValidationBlock aBlock = ^(NSString *input){
return ([input length] == 10) ;
};
which throws me a compilation error thats tates the return type is int and should be BOOL.
when I add a cast it works just fine:
typedef BOOL(^FieldValidationBlock)(NSString *);
FieldValidationBlock aBlock = ^(NSString *input){
return (BOOL)([input length] == 10) ;
};
why this happen?
Because BOOL is an objective C type, and the logical comparison operators are standard C. In standard C the return type of comparison operators is an int. This is important to know sometimes, as when you negate a value that you assume to be boolean, but is in fact an int, it's not necessarily going to be what you expect.
In your example, casting to a BOOL is fine.

How to use Array of C language with Objective-C

I'm using Objective-C language. But I don't know how to use c with Objective-C.
Ex) This is Function method.
- ( ?? ) function{
unsigned int first = ..
unsigned int second = ..
unsigned int third = ..
int infoData = {first,second,third};
return infoData;
}
How to fill in parenthesis.
I don't use NSArray.
Please help me.
the answer is the same as it is in C. Objective-C is a strict superset of C.
Assuming you declared int[] infoData you could make the return int*, but you're still going to have problems because the array is allocated on the function's stack. You'll need to dynamically allocate space for it just like you would in C.
(You cannot use int[] as a return type)
The code below will compile, but gcc will warn about returning the address of a function local variable.
#interface test
- (int*) function;
#end
#implementation test
- (int*) function{
unsigned int first = 0;
unsigned int second = 1;
unsigned int third = 2;
int infoData[] = {first,second,third};
return infoData;
}
#end

Why we used double and triple pointer in objective-C or C language?

I confused when i want to take single pointer and when should i take double pointer?
In following structure what exactly did?
struct objc_class {
Class isa;
Class super_class;
const char *name;
long version;
long info;
long instance_size;
struct objc_ivar_list *ivars;
struct objc_method_list **methodLists;
struct objc_cache *cache;
struct objc_protocol_list *protocols;
};
Why we use the methodLists double pointer?
Edited
int sqlite3_get_table(
sqlite3 *db,
const char *zSql,
char ***pazResult,
int *pnRow,
int *pnColumn,
char **pzErrmsg
);
In above scenario what will be meaning of triple pointer char ***pazResult?
Well, in C at least, double-pointers are commonly used for 2D arrays. The most common 2D array is probably an array of C strings (char*'s). Double pointers are also sometimes employed to pass pointers to functions by reference, but this is unlikely to be the use in the code sample you posted.
According to the name methodLists I would guess that this is an array of lists. A (linked) list in C is commonly represented by a pointer to a node, which objc_method_list could be. An array of such lists is then implemented with a double pointer.
It's probably not the case in the code that you referenced, but you also need a double pointer any time you want to pass a pointer to a function and have changes to that pointer be reflected outside the scope of that function.
For example, if you were trying to rewrite the strcpy function so that the user did not have to allocate memory for the source string, you might try something like the following:
void MyStrcpy(char* dst, char* src){
dst = (char*)malloc(sizeof(char)*(strlen(src)+1));
for(int i=0;i<=strlen(src);i++)
dst[i] = src[i];
printf("src: %s ", src);
printf("dst: %s\n\n", dst);
}
If you were then to call that function,
int main() {
char *foo = "foo";
char *newPtr;
MyStrcpy(newPtr, foo);
printf("foo: %s ", foo);
printf("new: %s\n", newPtr);
}
your output would be as follows:
src: foo
dst: foo
foo: foo
new:
You might also get a seg fault when trying to print newPtr, depending your system. The reason for this behavior is the exact same as the reason you wouldn't expect a change to an int that was passed by value to a function to be reflected outside of that function: what you are passing to MyStrcpy is simply the memory address that newPtr references. When you malloc the space for dst inside the function, you are changing the address dst points to. This change will not be reflected outside of the scope of MyStrcpy!
Instead, if you wanted newPtr to point to the new allocated chunk of memory, you need to have dst be a pointer to a pointer, a char **.
void MyStrcpy(char** dst, char* src){
*dst = (char*)malloc(sizeof(char)*(strlen(src)+1));
for(int i=0;i<=strlen(src);i++)
(*dst)[i] = src[i];
printf("src: %s ", src);
printf("dst: %s\n\n", *dst);
}
Now, if you were to call that function:
int main() {
char *foo = "foo";
char *newPtr;
MyStrcpy(&newPtr, foo);
printf("foo: %s ", foo);
printf("new: %s\n", newPtr);
}
You would get your expected output:
src: foo
dst: foo
foo: foo
new: foo
Hope that helps!
See also these questions:
What is double star?
Why does NSError need double indirection? (pointer to a pointer)
In the most general case a double pointer is a pointer to a list of pointers.
In general pointer is used to hold the address of another variable. What if we need to hold the address of pointer ,in that case we use double pointer. When we want to hold the address of double pointer we use triple pointer.