Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #272 from mcarbonneaux/master
add pr_log_stacktrace api function to trace in debug the stacktrace
  • Loading branch information
Castaglia committed Aug 18, 2016
2 parents 1567299 + ab4f890 commit 8350c0d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/log.h
Expand Up @@ -83,6 +83,10 @@
# include <login.h>
#endif

#ifdef HAVE_EXECINFO_H
# include <execinfo.h>
#endif

#ifdef HAVE_PATHS_H
# include <paths.h>
#endif
Expand All @@ -99,10 +103,13 @@
# endif
#endif


int log_lastlog(uid_t uid, const char *user_name, const char *tty,
const pr_netaddr_t *remote_addr);
#endif /* PR_USE_LASTLOG */

void pr_log_stacktrace(module *module, int debuglevel, const char*file, int line);

/* Note: Like lastlog.h, it would be tempting to split out the declaration of
* this function, and its necessary system headers, into a proftpd-specific
* wtmp.h file. But that would collide with the system wtmp.h file on
Expand Down
34 changes: 34 additions & 0 deletions src/log.c
Expand Up @@ -56,6 +56,40 @@ static int fd_set_block(int fd) {
}
#endif /* PR_USE_NONBLOCKING_LOG_OPEN */

void pr_log_stacktrace(module *module, int debuglevel, const char*file, int line) {
#if defined(HAVE_EXECINFO_H) && \
defined(HAVE_BACKTRACE) && \
defined(HAVE_BACKTRACE_SYMBOLS)
void *trace[PR_TUNABLE_CALLER_DEPTH];
char **strings;
int tracesz;

(void) pr_log_debug(debuglevel, "%s/%s: -----BEGIN STACK TRACE from %s/%d -----",module->name,module->module_version, file, line);

tracesz = backtrace(trace, PR_TUNABLE_CALLER_DEPTH);
if (tracesz < 0) {
(void) pr_log_debug(debuglevel, "%s/%s: backtrace(3) error: %s", module->name, module->module_version, strerror(errno));
}

strings = backtrace_symbols(trace, tracesz);
if (strings != NULL) {
register unsigned int i;

for (i = 1; i < tracesz; i++) {
(void) pr_log_debug(debuglevel, "%s/%s: [%u] %s", module->name, module->module_version, i-1, strings[i]);
}

/* Prevent memory leaks. */
free(strings);

} else {
(void) pr_log_debug(debuglevel, "%s/%s: error obtaining stacktrace symbols: %s", module->name, module->module_version, strerror(errno));
}

(void) pr_log_debug(debuglevel, "%s/%s: -----END STACK TRACE-----",module->name, module->module_version);
#endif
}

int pr_log_openfile(const char *log_file, int *log_fd, mode_t log_mode) {
int res;
pool *tmp_pool = NULL;
Expand Down

0 comments on commit 8350c0d

Please sign in to comment.