Backtrace will now be printed when the program crashes

- Note that static functions are not included in the printed backtrace.
This commit is contained in:
Fufu Fang 2019-09-03 14:47:12 +01:00
parent e971f9ab05
commit c7dfa241d4
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
8 changed files with 55 additions and 27 deletions

View File

@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated Makefile, fixed issue #44
- When header files get changed, the relevant object will get recompiled.
- Added a progress indicator for LinkTable_fill().
- Backtrace will now be printed when the program crashes
- Note that static functions are not included in the printed backtrace!
## [1.1.9] - 2019-09-02
### Changed

View File

@ -1,6 +1,6 @@
VERSION=1.1.9
CFLAGS+= -g -O2 -Wall -Wextra -Wshadow\
CFLAGS+= -O2 -Wall -Wextra -Wshadow -rdynamic\
-D_FILE_OFFSET_BITS=64 -DVERSION=\"$(VERSION)\" \
`pkg-config --cflags-only-I gumbo libcurl fuse`
LDFLAGS+= -pthread -lgumbo -lcurl -lfuse -lcrypto \

View File

@ -122,7 +122,7 @@ void CacheSystem_init(const char *path, int url_supplied)
if (pthread_mutex_init(&cf_lock, NULL) != 0) {
fprintf(stderr,
"CacheSystem_init(): cf_lock initialisation failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
if (url_supplied) {
@ -136,7 +136,7 @@ void CacheSystem_init(const char *path, int url_supplied)
if (!dir) {
fprintf(stderr,
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
exit_failure();
}
/* Handle the case of missing '/' */
@ -482,7 +482,7 @@ static Cache *Cache_alloc()
Cache *cf = calloc(1, sizeof(Cache));
if (!cf) {
fprintf(stderr, "Cache_new(): calloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
}
if (pthread_mutex_init(&cf->seek_lock, NULL)) {
@ -689,7 +689,7 @@ int Cache_create(Link *this_link)
if (!cf->seg) {
fprintf(stderr, "Cache_create(): cf->seg calloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
}
if (Meta_create(cf)) {

View File

@ -11,6 +11,8 @@
#include <string.h>
#include <unistd.h>
#define STATUS_LEN 64
/* ---------------- External variables -----------------------*/
LinkTable *ROOT_LINK_TBL = NULL;
int ROOT_LINK_OFFSET = 0;
@ -29,7 +31,7 @@ void link_system_init()
if (pthread_mutex_init(&link_lock, NULL) != 0) {
fprintf(stderr,
"link_system_init(): link_lock initialisation failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
}
@ -39,7 +41,7 @@ static void LinkTable_add(LinkTable *linktbl, Link *link)
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) {
fprintf(stderr, "LinkTable_add(): realloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
}
linktbl->links[linktbl->num - 1] = link;
}
@ -49,7 +51,7 @@ static Link *Link_new(const char *linkname, LinkType type)
Link *link = calloc(1, sizeof(Link));
if (!link) {
fprintf(stderr, "Link_new(): calloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
}
strncpy(link->linkname, linkname, MAX_FILENAME_LEN);
link->type = type;
@ -167,7 +169,7 @@ void Link_get_stat(Link *this_link)
TransferStruct *transfer = malloc(sizeof(TransferStruct));
if (!transfer) {
fprintf(stderr, "Link_get_size(): malloc failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
transfer->link = this_link;
transfer->type = FILESTAT;
@ -225,15 +227,15 @@ static void LinkTable_fill(LinkTable *linktbl)
int n = curl_multi_perform_once();
int i = 0;
int j = 0;
char s[64];
char s[STATUS_LEN];
while ( (i = curl_multi_perform_once()) ) {
if (1) {
if (j) {
for (size_t k = 0; k < strnlen(s, 64); k++) {
for (size_t k = 0; k < strnlen(s, STATUS_LEN); k++) {
fprintf(stderr, "\b");
}
}
snprintf(s, 64, "... %d / %d", i, n);
snprintf(s, STATUS_LEN, "... %d / %d", i, n);
fprintf(stderr, "%s", s);
}
j++;
@ -313,7 +315,7 @@ LinkTable *LinkTable_new(const char *url)
LinkTable *linktbl = calloc(1, sizeof(LinkTable));
if (!linktbl) {
fprintf(stderr, "LinkTable_new(): calloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
}
/* populate the base URL */
@ -573,7 +575,7 @@ Link *path_to_Link(const char *path)
char *new_path = strndup(path, MAX_PATH_LEN);
if (!new_path) {
fprintf(stderr, "path_to_Link(): cannot allocate memory\n");
exit(EXIT_FAILURE);
exit_failure();
}
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
free(new_path);

View File

@ -20,7 +20,7 @@ int main(int argc, char **argv)
if (argc < 2) {
print_help(argv[0], 0);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
exit_failure();
}
/* These are passed into fuse initialiser */
@ -62,11 +62,11 @@ int main(int argc, char **argv)
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
fprintf(stderr, "Error: Please supply a valid URL.\n");
print_help(argv[0], 0);
exit(EXIT_FAILURE);
exit_failure();
} else {
if(!network_init(base_url)) {
fprintf(stderr, "Error: Network initialisation failed.\n");
exit(EXIT_FAILURE);
exit_failure();
}
}

View File

@ -250,14 +250,14 @@ LinkTable *network_init(const char *url)
/* ------- Global related ----------*/
if (curl_global_init(CURL_GLOBAL_ALL)) {
fprintf(stderr, "network_init(): curl_global_init() failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
/* -------- Share related ----------*/
CURL_SHARE = curl_share_init();
if (!(CURL_SHARE)) {
fprintf(stderr, "network_init(): curl_share_init() failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
@ -266,7 +266,7 @@ LinkTable *network_init(const char *url)
if (pthread_mutex_init(&curl_lock, NULL) != 0) {
fprintf(stderr, "network_init(): curl_lock initialisation failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
curl_share_setopt(CURL_SHARE, CURLSHOPT_UNLOCKFUNC, curl_callback_unlock);
@ -275,7 +275,7 @@ LinkTable *network_init(const char *url)
curl_multi = curl_multi_init();
if (!curl_multi) {
fprintf(stderr, "network_init(): curl_multi_init() failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
NETWORK_CONFIG.max_conns);
@ -286,7 +286,7 @@ LinkTable *network_init(const char *url)
if (pthread_mutex_init(&transfer_lock, NULL)) {
fprintf(stderr,
"network_init(): transfer_lock initialisation failed!\n");
exit(EXIT_FAILURE);
exit_failure();
}
/*
@ -354,7 +354,7 @@ void transfer_blocking(CURL *curl)
if(res > 0) {
fprintf(stderr, "transfer_blocking(): %d, %s\n",
res, curl_multi_strerror(res));
exit(EXIT_FAILURE);
exit_failure();
}
while (transfer.transferring) {
@ -394,7 +394,7 @@ size_t write_memory_callback(void *contents, size_t size, size_t nmemb,
if(!mem->memory) {
/* out of memory! */
fprintf(stderr, "write_memory_callback(): realloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
return 0;
}

View File

@ -1,9 +1,14 @@
#include "util.h"
#include <execinfo.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BT_BUF_SIZE 255
char *path_append(const char *path, const char *filename)
{
int needs_separator = 0;
@ -17,7 +22,7 @@ char *path_append(const char *path, const char *filename)
str = calloc(ul + sl + needs_separator + 1, sizeof(char));
if (!str) {
fprintf(stderr, "path_append(): calloc failure!\n");
exit(EXIT_FAILURE);
exit_failure();
}
strncpy(str, path, ul);
if (needs_separator) {
@ -39,7 +44,7 @@ void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x)
if (i) {
fprintf(stderr, "pthread_mutex_unlock failed, %d, %s\n", i,
strerror(i));
exit(EXIT_FAILURE);
exit_failure();
}
}
@ -50,6 +55,20 @@ void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
if (i) {
fprintf(stderr, "pthread_mutex_lock failed, %d, %s\n", i,
strerror(i));
exit(EXIT_FAILURE);
exit_failure();
}
}
void exit_failure()
{
int nptrs;
void *buffer[BT_BUF_SIZE];
nptrs = backtrace(buffer, BT_BUF_SIZE);
fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n");
fprintf(stderr, "backtrace() returned the following %d addresses:\n",
nptrs);
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
exit(EXIT_FAILURE);
}

View File

@ -27,6 +27,11 @@ void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x);
*/
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x);
/**
* \brief wrapper for exit(EXIT_FAILURE)
*/
void exit_failure();
/**
* \brief append a path
* \details This function appends a path with the next level, while taking the