Valgrind wrapper with varargs? - valgrind

I think that the wrapper framework in valgrind is pretty nice. I am trying to track differences between different code paths (as to why one works and another does not). Instead of trying to look at a difference of an strace output (which will not give me all details I need because I want to trace lib and sys calls I decided to use wrappers around a handful of functions.
The one function that got me scratching my head is fcntl.
The fcntl manpage declares it as follows
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
However as this is C and overloading does not come naturally to C, it is prototyped as follows in fcntl.h
extern int fcntl (int __fd, int __cmd, ...);
The end-user wrapping facility in valgrind supports N number of WORD arguments, but carries a warning about using the wrong number of args and I did not see a mention of any varargs.
A more classical varargs function like printf is typically implemented with a lower fixed args function vprintf which takes va_list as a single parameter. In such a case I would have wrapped vprintf rather than printf, but alas AFAIK fcntl does not have such an intermediate function.
My question is - what would a "safe" wrapper for fcntl look like?
For example, the following seems to work, but is it safe:
int I_WRAP_SONAME_FNNAME_ZU(libcZdsoZa,fcntl)(int fd, int cmd, ...)
{
int result;
OrigFn fn;
void* arg;
va_list argp;
va_start(argp, cmd);
arg=va_arg(argp, void*);
VALGRIND_GET_ORIG_FN(fn);
printf("##fcntl wrapper: args fd=%d, cmd=%d, arg=%p ... ", fd, cmd, arg);
CALL_FN_W_WWW(result, fn, fd, cmd, arg);
printf("##fcntl wrapper: result %d\n", result);
return result;
}

Look syswrappers for sys_ioctl in valgrind sources in file coregrind/m_syswrap/syswrap-linux.c.
Look at http://valgrind.org/docs/manual/dist.readme-missing.html for details.
Maybe you will need to rebuild valgrind with your changes for syswrappers.

Related

printf implementation for embedded system

where can I find an almost complete implementation (float, width, etc.) suitable for embedded systems (few nested funtction call, low stack and ram usage, no heap, mo syscall)
Have you checked out ChibiOS's implementation of chvprintf? It's Apache-licensed and float-support (costly) is a compile-time option via define.
It's built with microcontrollers in mind. The macros are all pretty much only calls to a function pointer in the supplied BaseSequentialStream struct and you can replace them as you see fit. The var-args list is your regular stdarg.h implementation.
For transmitting on the serial port, the quickest thing is:
int32_t printfDebugSerial(const char *format, ...)
{
if (strlen(format) >200) return -1;
char tempBuff[256]; memset(tempBuff, 0, sizeof tempBuff);
va_list arg;
int32_t done;
va_start (arg, format);
done = (int32_t)vsprintf(tempBuff,format, arg);
va_end (arg);
pushMsgOnSerial((uint8_t*)tempBuff, done);
return done;
}
where pushMsgOnSerial() will be your board specific function for sending bytes on the serial port.

Reading dynamically growing file using NSInputStream

I should use Objective-C to read some slowly growing file (under Mac OS X).
"Slowly" means that I read to EOF before it grows bigger.
In means of POSIX code in plain syncronous C I can do it as following:
while(1)
{
res = select(fd+1,&fdset,NULL,&fdset,some_timeout);
if(res > 0)
{
len = read(fd,buf,sizeof(buf));
if (len>0)
{
printf("Could read %u bytes. Continue.\n", len);
}
else
{
sleep(some_timeout_in_sec);
}
}
}
Now I want to re-write this in some asynchronous manner, using NSInputSource or some other async Objective-C technique.
The problem with NSInputSource: If I use scheduleInRunLoop: method then once I get NSStreamEventEndEncountered event, I stop receiving any events.
Can I still use NSInputSource or should I pass to using NSFileHandle somehow or what would you recommend ?
I see a few problems.
1) some_Timeout, for select() needs to be a struct timeval *.
2) for sleep() some_timeout needs to be an integer number of seconds.
3) the value in some_timeout is decremented via select() (which is why the last parameter is a pointer to the struct timeval*. And that struct needs to be re-initialized before each call to select().
4) the parameters to select() are highest fd of interest+1, then three separate struct fd_set * objects. The first is for input files, the second is for output files, the third is for exceptions, however, the posted code is using the same struct fd_set for both the inputs and the exceptions, This probably will not be what is needed.
When the above problems are corrected, the code should work.

Static analysis: Passing pointer to uninitialized data

In the past, we've had some trouble with uninitialized data in C-code. I've tried two different static code analysis tools on the code example below. None of them complain about passing a pointer to uninitialized data. Are you aware of any tool that would catch this?
Thank you in advance!
static int useByVal(const int int_val)
{
return int_val + 1;
}
static void useByRef(int* const int_ptr)
{
if (int_ptr != (void*)0)
{
(*int_ptr)++;
}
}
int main(void)
{
int i;
int k;
/*** GOOD: The tool detects error: Using value of uninitialized automatic object 'i' */
i = useByVal(i);
/*** BAD: The tool does not catch uninitialized object 'k' when passed by reference */
useByRef(&k);
/*** BAD: Since call to 'useByRef(&k)', the tool now consider 'k' as initialized */
return i+k;
}
No I'm not aware if any tool that would catch that. The reason is that they usually analyze on a function-by-function basis. In other words, while analyzing main tools would not analyze useByVal or useByRef, but make reasonable assumptions about them (like they expect an uninitialized object).
Additionally, if they did generate messages in this case, then you would need to add comments / pragmas to remove them and say "this usage is OK; don't emit a message any more". Because of the nature and frequency of pointer-passing, your program would be full of them.
Better implement some dynamic technique and catch the problem during testing.

How to Marcshal a COM callback in C++/CLI

We have an out of process COM application that was written in C++ that implements are networking protocol. When a packet of data is received a callback is invoked into the application that registered for the packet.
The callback interface is defined as such:
[helpstring("method MessageHandler")] HRESULT MessageHandler([in,size_is(nSize)] char * szBuf, int nSize, DWORD dwTransCode, DWORD dwSenderID, BSTR bstrFromIP);
Using this in C++ has not been an issue. We now have a case where we have a C++/CLI application that needs to receive callbacks. After hacking away until the compiler was happy, I arrived at the following implementation:
ref class MessageHandlerClass : public MessageRouterCallback
{
public:
virtual void MessageHandler(signed char %buffer, int size, unsigned int msgId, unsigned int fromId, System::String^ fromIp)
{
switch (msgId)
{
case MaintenanceMsgs::maintenance_event_message::ID:
{
SomeStructure msg;
myHandler->HandleMaintenanceEvent(&msg);
}
}
}
This is my first foray into C++/CLI.
First Question: What they heck does '%' mean in 'signed char %buffer'?
Second Question: I can place a breakpoint and see that the callback is getting called. I can look at the 'buffer' argument in the memory debugger and it contains the data I expect. I have been VERY unsuccessful at pulling that data out and placing it into the variable 'msg'. I know I can't do a cast like in C++, but every example I've been tried (Mostly InteropServices::Marshal and some pin_ptr stuff) doesn't seem to get me anywhere.
SomeStructure is declared in a header file and is included by both the C++ and the C++/CLI application. SomeStructure contains 2 unsigned shorts followed by three character arrays.
Any direction on where to go from here would be greatly appreciated.
Ok. It just 'clicked'... So I'll answer my own question here.
First Question: What they heck does '%' mean in 'signed char %buffer'?
The '%' just means its 'tracked' and will be garbage collected, or at least that's what I think it means :)
Second Question: How to marshal.
First I had to get to the 'internal pointer' and C++/CLI provides the & operator for that. Then I was able to simply memcpy the data over.
pin_ptr<signed char> p = &buffer;
MaintenanceMsgs::maintenance_event_message msg;
memcpy((void*)&msg, (void*)p, sizeof(msg));
myHandler->HandleMaintenanceEvent(&msg);
Is this safe?

Offsetof macro with C++/CLI

The offsetof macro seems not to work under C++/CLI.
This works fine in unmanaged C++, but throws "error C2275: 'Entity' :illegal use of this type as an expression" error in CLI.
struct Property{
char* label;
PropertyTypes type;
unsigned int member_offset;
unsigned int position;
unsigned char bit_offset;
};
struct Entity{
...
bool transparent;
...
};
Property property = {"Transparent",
TYPE_BOOL,
offsetof(Entity, transparent),
0,
0}; // C2275 HERE
Does CLI have some replacement?
My guess would be that the compiler message boils down to: "offsetof" is not a known macro and if it was a function its parameters must not contain a typename.
Edit: As somebody pointed out in the comments, offsetof is actually part of the std lib. So what's missing is probably just
#include <cstddef>
Alternatively, you can use this macro implementation (taken from Win32/MFC headers):
#ifdef _WIN64
#define OFFSET_OF( s, m )\
(size_t)((ptrdiff_t)&reinterpret_cast<const volatile char&>((((s*)0)->m)) )
#else
#define OFFSET_OF( s, m )\
(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m))
#endif
Standard C++ already has an alternative; &Entity::transparent. You'll probably want to use templates when redesigning the Propery class. The type of a pointer-to-member is non-trivial.
You will need to provide the type of the object you are assigning to. Looks like there is some type-mismatch for the member in question.
See this for sample usage.
Just a shot in the dark and without a chance to double-check this - should
offsetof(Entity, transparent),
perhaps rather read
offsetof( struct Entity, transparent ),
???