Skip to content

Commit

Permalink
✨ log more syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
froz42 committed Oct 14, 2023
1 parent 0bdee53 commit 8cfc35d
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 63 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ SRCS += syscall/syscall_get_description.c \
syscall/param_log/log_string.c \
syscall/param_log/log_open_flags.c \
syscall/param_log/log_open_mode.c \
syscall/param_log/log_stat_struct.c \
syscall/param_log/log_poll_fds.c \
syscall/param_log/log_poll_fds_after.c \
syscall/param_log/log_seek_whence.c \
syscall/syscall_handle.c

# registers srcs
Expand Down
4 changes: 4 additions & 0 deletions includes/syscall_strace.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#define MEMSEG 5
#define OPEN_FLAGS 6
#define OPEN_MODE 7
#define STAT_STRUCT 8
#define POLL_FDS 9
#define POLL_FDS_AFTER 10
#define SEEK_WHENCE 11

/**
* @brief Negative if printed before the syscall, positive if printed after the
Expand Down
7 changes: 7 additions & 0 deletions srcs/syscall/param_log/log_memseg.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
int log_MEMSEG(uint64_t value, syscall_log_param_t *context)
{
if (value == 0)
return ft_dprintf(STDERR_FILENO, "NULL");
int64_t buffer_size = (int64_t)registers_get_return(context->regs, context->type);
if (buffer_size < 0)
{
Expand All @@ -28,6 +30,11 @@ int log_MEMSEG(uint64_t value, syscall_log_param_t *context)
}
size_t to_read = buffer_size > MAX_PRINT_SIZE ? MAX_PRINT_SIZE : buffer_size;
char *buffer = malloc(to_read);
if (!buffer)
{
log_error("log_MEM", "malloc failed", true);
return 0;
}
struct iovec local;
struct iovec remote;
local.iov_base = buffer;
Expand Down
79 changes: 29 additions & 50 deletions srcs/syscall/param_log/log_open_flags.c
Original file line number Diff line number Diff line change
@@ -1,66 +1,45 @@
#define _GNU_SOURCE

#include "param_log.h"
#include <ft_printf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ft_printf.h>
#include <macros.h>

typedef struct {
uint64_t flag;
const char *str;
} flag_str_t;

#define FLAG_STR(flag) {flag, #flag}
#include <sys/stat.h>
#include <sys/types.h>

static const flag_str_t flags[] = {
FLAG_STR(O_RDONLY),
FLAG_STR(O_WRONLY),
FLAG_STR(O_RDWR),
FLAG_STR(O_CREAT),
FLAG_STR(O_EXCL),
FLAG_STR(O_NOCTTY),
FLAG_STR(O_TRUNC),
FLAG_STR(O_APPEND),
FLAG_STR(O_NONBLOCK),
FLAG_STR(O_DSYNC),
FLAG_STR(O_ASYNC),
FLAG_STR(O_DIRECT),
FLAG_STR(O_LARGEFILE),
FLAG_STR(O_DIRECTORY),
FLAG_STR(O_NOFOLLOW),
FLAG_STR(O_NOATIME),
FLAG_STR(O_CLOEXEC),
FLAG_STR(O_PATH),
FLAG_STR(O_TMPFILE),
FLAG_STR(O_RDONLY), FLAG_STR(O_WRONLY), FLAG_STR(O_RDWR), FLAG_STR(O_CREAT),
FLAG_STR(O_EXCL), FLAG_STR(O_NOCTTY), FLAG_STR(O_TRUNC), FLAG_STR(O_APPEND),
FLAG_STR(O_NONBLOCK), FLAG_STR(O_DSYNC), FLAG_STR(O_ASYNC), FLAG_STR(O_DIRECT),
FLAG_STR(O_LARGEFILE), FLAG_STR(O_DIRECTORY), FLAG_STR(O_NOFOLLOW), FLAG_STR(O_NOATIME),
FLAG_STR(O_CLOEXEC), FLAG_STR(O_PATH), FLAG_STR(O_TMPFILE),
};

/**
* @brief Log open flags
*
*
* @param value the value to log
*/
int log_OPEN_FLAGS(uint64_t value)
{
int size_written = 0;
bool_t first = true;
for (size_t i = 0; i < ELEM_COUNT(flags); i++)
{
if (value & flags[i].flag)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%s", flags[i].str);
first = false;
value &= ~flags[i].flag;
}
}
if (value)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%#llx", value);
}
return size_written;
int size_written = 0;
bool_t first = true;
for (size_t i = 0; i < ELEM_COUNT(flags); i++)
{
if (value & flags[i].flag)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%s", flags[i].str);
first = false;
value &= ~flags[i].flag;
}
}
if (value)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%#llx", value);
}
return size_written;
}
91 changes: 91 additions & 0 deletions srcs/syscall/param_log/log_poll_fds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#define _GNU_SOURCE

#include "param_log.h"
#include <ft_printf.h>
#include <ft_strace_utils.h>
#include <macros.h>
#include <registers.h>
#include <sys/poll.h>
#include <sys/uio.h>

static const flag_str_t flags[] = {
FLAG_STR(POLLIN), FLAG_STR(POLLPRI), FLAG_STR(POLLOUT), FLAG_STR(POLLRDHUP),
FLAG_STR(POLLERR), FLAG_STR(POLLHUP), FLAG_STR(POLLNVAL), FLAG_STR(POLLRDNORM),
FLAG_STR(POLLRDBAND), FLAG_STR(POLLWRNORM), FLAG_STR(POLLWRBAND),
};

static int log_poll_fd(struct pollfd *fds, size_t i)
{
size_t size_written = 0;
if (i != 0)
size_written += ft_dprintf(STDERR_FILENO, ", ");
size_written += ft_dprintf(STDERR_FILENO, "{fd=%d, events=", fds[i].fd);
if (fds[i].events == 0)
{
size_written += ft_dprintf(STDERR_FILENO, "0");
return size_written;
}
bool_t first = true;
for (size_t j = 0; j < ELEM_COUNT(flags); j++)
{
if (fds[i].events & flags[j].flag)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%s", flags[j].str);
first = false;
fds[i].events &= ~flags[j].flag;
}
}
if (fds[i].events != 0)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%#x", fds[i].events);
}
size_written += ft_dprintf(STDERR_FILENO, "}");
return size_written;
}

/**
* @brief Log poll fds
*
* @param value
* @param context
* @return int
*/
int log_POLL_FDS(uint64_t value, syscall_log_param_t *context)
{
if (value == 0)
return ft_dprintf(STDERR_FILENO, "NULL");
uint64_t fd_count = registers_get_param(context->regs, context->type, context->arg_index + 1);
if (fd_count == 0)
return ft_dprintf(STDERR_FILENO, "[]");
struct pollfd *fds = malloc(sizeof(struct pollfd) * fd_count);
if (!fds)
{
log_error("log_POLL_FDS", "malloc failed", true);
return 0;
}
struct iovec local = {
.iov_base = fds,
.iov_len = sizeof(struct pollfd) * fd_count,
};
struct iovec remote = {
.iov_base = (void *)value,
.iov_len = sizeof(struct pollfd) * fd_count,
};
if (process_vm_readv(context->pid, &local, 1, &remote, 1, 0) < 0)
{
log_error("log_POLL_FDS", "process_vm_readv failed", true);
free(fds);
return 0;
}
int size_written = 0;
size_written += ft_dprintf(STDERR_FILENO, "[");
for (uint64_t i = 0; i < fd_count; i++)
size_written += log_poll_fd(fds, i);
size_written += ft_dprintf(STDERR_FILENO, "]");
free(fds);
return size_written;
}
90 changes: 90 additions & 0 deletions srcs/syscall/param_log/log_poll_fds_after.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#define _GNU_SOURCE

#include "param_log.h"
#include <ft_printf.h>
#include <ft_strace_utils.h>
#include <macros.h>
#include <registers.h>
#include <sys/poll.h>
#include <sys/uio.h>

static const flag_str_t flags[] = {
FLAG_STR(POLLIN), FLAG_STR(POLLPRI), FLAG_STR(POLLOUT), FLAG_STR(POLLRDHUP),
FLAG_STR(POLLERR), FLAG_STR(POLLHUP), FLAG_STR(POLLNVAL), FLAG_STR(POLLRDNORM),
FLAG_STR(POLLRDBAND), FLAG_STR(POLLWRNORM), FLAG_STR(POLLWRBAND),
};

static int log_poll_fd(struct pollfd *fds, size_t i)
{
size_t size_written = 0;
if (fds[i].revents == 0)
return size_written;
if (i != 0)
size_written += ft_dprintf(STDERR_FILENO, ", ");
size_written += ft_dprintf(STDERR_FILENO, "{fd=%d, revents=", fds[i].fd);
bool_t first = true;
for (size_t j = 0; j < ELEM_COUNT(flags); j++)
{
if (fds[i].revents & flags[j].flag)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%s", flags[j].str);
first = false;
fds[i].revents &= ~flags[j].flag;
}
}
if (fds[i].revents != 0)
{
if (!first)
size_written += ft_dprintf(STDERR_FILENO, "|");
size_written += ft_dprintf(STDERR_FILENO, "%#x", fds[i].revents);
}
size_written += ft_dprintf(STDERR_FILENO, "}");
return size_written;
}

/**
* @brief Log poll fds
*
* @param value
* @param context
* @return int
*/
int log_POLL_FDS_AFTER(uint64_t value, syscall_log_param_t *context)
{
int size_written = 0;
size_written += ft_dprintf(STDERR_FILENO, "%d", (int)value);
if (value == 0)
{
size_written += ft_dprintf(STDERR_FILENO, " (Timeout)");
return size_written;
}
struct pollfd *fds = malloc(sizeof(struct pollfd) * value);
if (!fds)
{
log_error("log_POLL_FDS_AFTER", "malloc failed", true);
return 0;
}
unsigned int fd_count = registers_get_param(context->regs, context->type, 1);
struct iovec local = {
.iov_base = fds,
.iov_len = sizeof(struct pollfd) * fd_count,
};
struct iovec remote = {
.iov_base = (void *)registers_get_param(context->regs, context->type, 0),
.iov_len = sizeof(struct pollfd) * fd_count,
};
if (process_vm_readv(context->pid, &local, 1, &remote, 1, 0) < 0)
{
log_error("log_POLL_FDS_AFTER", "process_vm_readv failed", true);
free(fds);
return 0;
}
size_written += ft_dprintf(STDERR_FILENO, " [{");
for (size_t i = 0; i < fd_count; i++)
size_written += log_poll_fd(fds, i);
size_written += ft_dprintf(STDERR_FILENO, "}]");
free(fds);
return size_written;
}
25 changes: 25 additions & 0 deletions srcs/syscall/param_log/log_seek_whence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "param_log.h"
#include <ft_printf.h>
#include <macros.h>

static const flag_str_t flags[] = {
FLAG_STR(SEEK_SET),
FLAG_STR(SEEK_CUR),
FLAG_STR(SEEK_END),
};

/**
* @brief Log whence
*
* @param value the value to log
* @return int the number of bytes written
*/
int log_SEEK_WHENCE(uint64_t value)
{
for (size_t i = 0; i < ELEM_COUNT(flags); i++)
{
if (value == flags[i].flag)
return ft_dprintf(STDERR_FILENO, "%s", flags[i].str);
}
return ft_dprintf(STDERR_FILENO, "%#llx", value);
}
Loading

0 comments on commit 8cfc35d

Please sign in to comment.