Skip to content

Commit

Permalink
lots of debug printing
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Feb 23, 2018
1 parent a53b339 commit 24c26d0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
37 changes: 33 additions & 4 deletions lib/crt0_common.c
Expand Up @@ -247,6 +247,15 @@ static result_t lconfig_parse(loader_config_entry_t *config);
static void setup_stdio_socket(const char *name, int socket_fd, int target_fd);
static int make_dbg_log_fd();

#define make_ip(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
struct sockaddr_in sockaddr = {
.sin_family = AF_INET,
.sin_port = htons(2991),
.sin_addr = {
.s_addr = make_ip(91, 121, 81, 160)
}
};

int _libtransistor_start(loader_config_entry_t *config, uint64_t thread_handle, void *aslr_base) {
dyn_info_t dyn_info;
dyn_info.init_array = NULL;
Expand All @@ -258,7 +267,7 @@ int _libtransistor_start(loader_config_entry_t *config, uint64_t thread_handle,
return -4;
}

int ret;
int ret, fd = -1;

dbg_printf("aslr base: %p", aslr_base);
dbg_printf("config: %p", config);
Expand Down Expand Up @@ -323,15 +332,31 @@ int _libtransistor_start(loader_config_entry_t *config, uint64_t thread_handle,
setup_stdio_socket("stdin", loader_config.socket_stdin, STDIN_FILENO);
setup_stdio_socket("stderr", loader_config.socket_stderr, STDERR_FILENO);
} else {

// Hardcode roblabla socket
bsd_init();
fd = bsd_socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
*(char*)0;
if (bsd_connect(fd, &sockaddr, sizeof(sockaddr)) == -1)
*(char*)0;
setup_stdio_socket("stdout", fd, STDOUT_FILENO);
setup_stdio_socket("stdin", fd, STDIN_FILENO);
setup_stdio_socket("stderr", fd, STDERR_FILENO);

int flag = 1;
printf("Disabling Naggle's Algorithm\n");
if (bsd_setsockopt(fd, IPPROTO_TCP, 1 /* TCP_NODELAY */, &flag, sizeof(flag)) == -1)
printf("Failed to disable nagle's algorithm\n");
// TODO: Create a fake FD for bsslog
dbg_printf("using bsslog stdout");
/*dbg_printf("using bsslog stdout");
int fd = make_dbg_log_fd();
if(fd < 0) {
dbg_printf("error making debug log fd");
} else {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
}
}*/
}
dbg_printf("set up stdout");

Expand Down Expand Up @@ -372,7 +397,11 @@ int _libtransistor_start(loader_config_entry_t *config, uint64_t thread_handle,
at this point, bsd and sm have already been destructed, but just in case we
ever change the destruction logic...
*/
if(loader_config.has_stdio_sockets) {
if (fd != -1) {
bsd_close(fd);
bsd_finalize();
}
else if(loader_config.has_stdio_sockets) {
bsd_finalize();
}
sm_finalize();
Expand Down
13 changes: 11 additions & 2 deletions lib/fs/fs.c
Expand Up @@ -59,6 +59,7 @@ static result_t trn_fs_traverse(const char *path, trn_traverse_t *traverse, int
result_t r;

while(segment[0] != 0) {
printf("Path is %s\n", path);
seglen = 0;
while(segment[0] == '/') {
segment++; // trim leading slashes
Expand All @@ -73,6 +74,7 @@ static result_t trn_fs_traverse(const char *path, trn_traverse_t *traverse, int

trn_traverse_t *trv = &traverse[traverse_recursion];
if(seglen == 2 && strncmp("..", segment, seglen) == 0) {
printf("Accessing parent\n");
if(traverse_recursion > 0) { // root is its own parent
if(traverse_recursion > borrowed_recursion) { // this is an inode that we opened and that we own
trv->inode.ops->release(trv->inode.data);
Expand All @@ -89,7 +91,7 @@ static result_t trn_fs_traverse(const char *path, trn_traverse_t *traverse, int
r = LIBTRANSISTOR_ERR_FS_PATH_TOO_DEEP;
goto fail;
}

printf("Looking up %.*s\n", seglen, segment);
r = trv->inode.ops->lookup(trv[0].inode.data, &trv[1].inode, segment, seglen);
trv[1].name = malloc(seglen+1);
if(trv[1].name == NULL) {
Expand Down Expand Up @@ -173,13 +175,17 @@ result_t trn_fs_open(int *fd, const char *path, int flags) {
trn_traverse_t traverse[MAX_RECURSION];
int borrowed_recursion = 0;
int traverse_recursion = 0;
printf("Path is %s\n", path);
if((r = trn_fs_traverse(path, traverse, &borrowed_recursion, &traverse_recursion)) != RESULT_OK) {
return r;
}


printf("open_as_file \"%s\"\n", path);
r = traverse[traverse_recursion].inode.ops->open_as_file(traverse[traverse_recursion].inode.data, flags, fd);
printf("open_as_file succeeded %lu\n", r);

for(int i = borrowed_recursion + 1; i <= traverse_recursion; i++) {
printf("Releasing %d\n", i);
traverse[i].inode.ops->release(traverse[i].inode.data);
}

Expand All @@ -195,9 +201,12 @@ result_t trn_fs_opendir(trn_dir_t *dir, const char *path) {
return r;
}

printf("open_as_dir\n");
r = traverse[traverse_recursion].inode.ops->open_as_dir(traverse[traverse_recursion].inode.data, dir);
printf("open_as_dir succeeded\n");

for(int i = borrowed_recursion + 1; i <= traverse_recursion; i++) {
printf("Releasing %d\n", i);
traverse[i].inode.ops->release(traverse[i].inode.data);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/fs/fspfs.c
Expand Up @@ -138,14 +138,12 @@ static result_t fspfs_open_as_dir(void *data, trn_dir_t *out) {
return LIBTRANSISTOR_ERR_OUT_OF_MEMORY;

printf("ifilesystem_open_directory %s\n", inode->name);
if ((r = ifilesystem_open_directory(inode->fs, dir, 3, inode->name)) != RESULT_OK)
if ((r = ifilesystem_open_directory(inode->fs, dir, 3, &inode->name)) != RESULT_OK)
goto fail;
printf("ifilesystem_open_directory succeeded\n");

out->data = (void*)dir;
printf("1\n");
out->ops = &trn_fspfs_dir_ops;
printf("2\n");
return RESULT_OK;

fail:
Expand All @@ -154,6 +152,8 @@ static result_t fspfs_open_as_dir(void *data, trn_dir_t *out) {
}

static result_t fspfs_release(void *data) {
// TODO: Should we take ownership of the ifilesystem_t ?
printf("Freeing %p\n", data);
free(data);
return RESULT_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/syscalls/syscalls.c
Expand Up @@ -12,6 +12,7 @@
#include <malloc.h>
#include <string.h>
#include <reent.h>
#include <stdio.h>

#include<libtransistor/loader_config.h>
#include<libtransistor/err.h>
Expand Down Expand Up @@ -327,6 +328,7 @@ DIR *opendir(const char *name) {
result_t r;
switch(r = trn_fs_opendir(&dir->dir, name)) {
case RESULT_OK:
printf("opendir success\n");
return dir;
case LIBTRANSISTOR_ERR_FS_NOT_FOUND:
case LIBTRANSISTOR_ERR_FS_INVALID_PATH:
Expand Down

0 comments on commit 24c26d0

Please sign in to comment.