Skip to content

Commit 9033fe1

Browse files
committedOct 22, 2021
libguile-ssh/log (log_backtrace): New internal procedure
* libguile-ssh/log.c (log_backtrace): New internal procedure. * libguile-ssh/log.h: Likewise.
1 parent 7c6a1e7 commit 9033fe1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎libguile-ssh/log.c

+52
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include <stdio.h> /* DEBUG */
3030
#include <unistd.h> /* DEBUG */
3131

32+
#include <execinfo.h>
33+
#include <stdlib.h>
34+
#include <stdio.h>
35+
3236
#include "log.h"
3337
#include "error.h"
3438
#include "common.h"
@@ -75,6 +79,54 @@ libssh_logging_callback (int c_priority,
7579
scm_remember_upto_here_1 (userdata);
7680
}
7781

82+
/**
83+
* Log a backtrace, with a FUNCTION_NAME attached.
84+
*/
85+
void
86+
log_backtrace (const char* function_name)
87+
{
88+
enum {
89+
/* Maximum number of stack frames that can be obtained. */
90+
STACK_BUF_SZ = 20,
91+
/* Maximum log message length. */
92+
MESSAGE_BUF_SZ = 120
93+
};
94+
void *array[STACK_BUF_SZ];
95+
char message[MESSAGE_BUF_SZ];
96+
char **strings;
97+
int32_t size, i;
98+
99+
size = backtrace (array, STACK_BUF_SZ);
100+
strings = backtrace_symbols (array, size);
101+
if (strings != NULL)
102+
{
103+
snprintf (message,
104+
MESSAGE_BUF_SZ,
105+
"Obtained %d stack frames:",
106+
size);
107+
libssh_logging_callback (SSH_LOG_NOLOG,
108+
function_name,
109+
message,
110+
NULL);
111+
fflush (stderr);
112+
for (i = 0; i < size; i++)
113+
{
114+
snprintf (message,
115+
MESSAGE_BUF_SZ,
116+
"#%-2d %s",
117+
i,
118+
strings[i]);
119+
libssh_logging_callback (SSH_LOG_NOLOG,
120+
function_name,
121+
message,
122+
NULL);
123+
fflush (stderr);
124+
}
125+
}
126+
127+
free (strings);
128+
}
129+
78130

79131
#define TBUF_SZ 64
80132
#define DATE_BUF_SZ 128

‎libguile-ssh/log.h

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ extern void _gssh_log_debug_format(const char* function_name, SCM args,
4343
# define _gssh_log_debug_format(function_name, args, fmt, ...)
4444
#endif /* ifdef DEBUG */
4545

46+
void log_backtrace (const char* function_name);
47+
4648
extern void init_log_func (void);
4749

4850
#endif /* ifndef __LOG_H__ */

0 commit comments

Comments
 (0)