Skip to content

Instantly share code, notes, and snippets.

@michaeleisel

michaeleisel/replacer.c

Last active Feb 15, 2019
Embed
What would you like to do?
#import "fishhook.h" // from https://github.com/facebook/fishhook
// Replace write and writev, the two "chokepoint" functions that writes to stderr and stdout will probably pass through
static int (*originalWritev)(int fd, const struct iovec *v, int n);
static int (*originalWrite)(int fd, const void *buf, size_t size);
void checkForBadConstraintsMessage(int fd, const char *string, size_t size) {
if (strnstr(string, "UIViewAlertForUnsatisfiableConstraints", size)) {
// Write it to the console anyways before crashing
originalWrite(fd, string, size);
abort();
}
}
void writeWillOccur(int fd, const char *string, size_t size) {
if (fd != STDERR_FILENO && fd != STDOUT_FILENO) {
return;
}
// #########################################
// #########################################
// ######### Insert code at this point. for example, you could call checkForBadConstraintsMessage(string, size)
// #########################################
// #########################################
}
int myWritev(int fd, const struct iovec *v, int n) {
for (int i = 0; i < n; i++) {
writeWillOccur(fd, v[i].iov_base, v[i].iov_len);
}
// Keep this as the last line, or else make sure to restore errno right before this function returns
return originalWritev(fd, v, n);
}
int myWrite(int fd, const void *buf, size_t size) {
writeWillOccur(fd, buf, size);
// Keep this as the last line, or else make sure to restore errno right before this function returns
return originalWrite(fd, buf, size);
}
...
// This does the function replacement. Any calls before this will call the original functions and not ours
rebind_symbols((struct rebinding[2]){{"write", myWrite, (void *)&originalWrite}, {"writev", myWritev, (void *)&originalWritev}}, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.