|
This probe emulates a write error; a broken pipe in this
case. It demonstrates setting errno and returning -1 from write().
Main test: main.c
-----------------
#include <errno.h>
#include <string.h>
int main()
{
if( printf( "Hello World\n" ) < 1 )
{
/* printf failed - print error to stderr
*/
fprintf( stderr, "printf failed: %s\n",
strerror( errno ));
}
return 0;
}
The probe: write.apc
--------------------
#include <errno.h>
probe thread
{
probe extern:"write()" in "libc.so"
{
int stub_flag = 0;
on_entry
{
int fd = $1;
void *buf = (void*)$2;
size_t count = $3;
log( "--- write(", fd, ", ", buf, ", ",
count, ")" );
if ( fd == 1 )
{
/* only stdout */
stub_flag = 1;
ap_StubRoutine;
}
}
on_exit
{
if ( stub_flag )
{
errno = EPIPE;
$return = -1;
stub_flag = 0;
}
}
}
}
The result:
----------
>aprobe -u write main
printf failed: Broken pipe
>apformat main
--- write(1, 0xbfffbac0, 12)
--- write(2, 0xbfffbac0, 27) |