diff --git a/lib/bt-exe.c b/lib/bt-exe.c
index 6c2d891..7ea5ae9 100644
--- a/lib/bt-exe.c
+++ b/lib/bt-exe.c
@@ -1,11 +1,11 @@
 /* ------------------------------------------------------------- */
-/* for linking rt_printline and the signal/exception handler
+/* for linking rt_getline and the signal/exception handler
    from tccrun.c into executables. */
 
 #define CONFIG_TCC_BACKTRACE_ONLY
 #include "../tccrun.c"
 
-int (*__rt_error)(void*, void*, const char *, va_list);
+int (*__rt_error)(void*, void*, const char *, va_list, char** out, int out_len);
 
 #ifndef _WIN32
 # define __declspec(n)
diff --git a/lib/bt-log.c b/lib/bt-log.c
index d767f08..477a90f 100644
--- a/lib/bt-log.c
+++ b/lib/bt-log.c
@@ -5,7 +5,7 @@
 #include <stdio.h>
 #include <string.h>
 
-int (*__rt_error)(void*, void*, const char *, va_list);
+int (*__rt_error)(void*, void*, const char *, va_list, char** out, int out_len);
 
 #ifdef _WIN32
 # define DLL_EXPORT __declspec(dllexport)
@@ -16,13 +16,18 @@ int (*__rt_error)(void*, void*, const char *, va_list);
 DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
 {
     va_list ap;
-    int ret;
+    int ret, i;
+    char *out[128];
 
     if (__rt_error) {
         void *fp = __builtin_frame_address(1);
         void *ip = __builtin_return_address(0);
         va_start(ap, fmt);
-        ret = __rt_error(fp, ip, fmt, ap);
+        ret = __rt_error(fp, ip, fmt, ap, out, 128);
+        for (i = 0; i < ret; i++)
+            if (out[i])
+                printf("%s\n", out[i]);
+
         va_end(ap);
     } else {
         const char *p;
@@ -35,3 +40,20 @@ DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
     }
     return ret;
 }
+
+DLL_EXPORT int tcc_backtrace_str(char** out, int out_len, const char *fmt, ...)
+{
+    va_list ap;
+    int ret;
+
+    if (__rt_error) {
+        void *fp = __builtin_frame_address(1);
+        void *ip = __builtin_return_address(0);
+        va_start(ap, fmt);
+        ret = __rt_error(fp, ip, fmt, ap, out, out_len);
+        va_end(ap);
+    } else
+        ret = 0;
+
+    return ret;
+}
diff --git a/tccelf.c b/tccelf.c
index d3ec823..a0885a2 100644
--- a/tccelf.c
+++ b/tccelf.c
@@ -183,7 +183,7 @@ ST_FUNC void tccelf_delete(TCCState *s1)
     dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
     tcc_free(s1->sym_attrs);
 
-    symtab_section = NULL; /* for tccrun.c:rt_printline() */
+    symtab_section = NULL; /* for tccrun.c:rt_getline() */
 }
 
 /* save section data state */
diff --git a/tccpp.c b/tccpp.c
index 2ff5d5e..8c59bd6 100644
--- a/tccpp.c
+++ b/tccpp.c
@@ -2038,9 +2038,12 @@ include_done:
             if (tok == TOK_STR) {
                 if (file->true_filename == file->filename)
                     file->true_filename = tcc_strdup(file->filename);
-                /* prepend directory from real file */
-                pstrcpy(buf, sizeof buf, file->true_filename);
-                *tcc_basename(buf) = 0;
+                if (tokc.str.data && *(char*)tokc.str.data != '/') {
+                    /* prepend directory from real file */
+                    pstrcpy(buf, sizeof buf, file->true_filename);
+                    *tcc_basename(buf) = 0;
+                } else
+                    *buf = 0;
                 pstrcat(buf, sizeof buf, (char *)tokc.str.data);
                 tcc_debug_putfile(s1, buf);
             } else if (parse_flags & PARSE_FLAG_ASM_FILE)
diff --git a/tccrun.c b/tccrun.c
index 4e529f5..a394dda 100644
--- a/tccrun.c
+++ b/tccrun.c
@@ -52,7 +52,8 @@ typedef struct rt_context
 
 static rt_context g_rtctxt;
 static void set_exception_handler(void);
-static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);
+static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap,
+    char** buf, int buf_size);
 static void rt_exit(int code);
 #endif /* CONFIG_TCC_BACKTRACE */
 
@@ -443,17 +444,37 @@ static int rt_printf(const char *fmt, ...)
     return r;
 }
 
+#define rt_snprintf(...) do { \
+    tmp = snprintf(buf, buf_size, __VA_ARGS__); \
+    if (tmp < 0 || tmp >= buf_size) goto err; \
+    buf += tmp; \
+    buf_size -= tmp; \
+} while (0)
+
+#define rt_vsnprintf(...) do { \
+    tmp = vsnprintf(buf, buf_size, __VA_ARGS__); \
+    if (tmp < 0 || tmp >= buf_size) goto err; \
+    buf += tmp; \
+    buf_size -= tmp; \
+} while (0)
+
+
 #define INCLUDE_STACK_SIZE 32
 
+#ifndef BACKTRACE_BUF_SIZE
+# define BACKTRACE_BUF_SIZE 32768
+#endif
+static char backtrace_buf[BACKTRACE_BUF_SIZE];
+
 /* print the position in the source file of PC value 'pc' by reading
    the stabs debug information */
-static addr_t rt_printline (rt_context *rc, addr_t wanted_pc,
-    const char *msg, const char *skip)
+static addr_t rt_getline (rt_context *rc, addr_t wanted_pc,
+    const char *msg, const char *skip, char* buf, int buf_size)
 {
     char func_name[128];
     addr_t func_addr, last_pc, pc;
     const char *incl_files[INCLUDE_STACK_SIZE];
-    int incl_index, last_incl_index, len, last_line_num, i;
+    int incl_index, last_incl_index, len, last_line_num, i, tmp;
     const char *str, *p;
     ElfW(Sym) *esym;
     Stab_Sym *sym;
@@ -569,23 +590,25 @@ found:
         str = incl_files[--i];
         if (skip[0] && strstr(str, skip))
             return (addr_t)-1;
-        rt_printf("%s:%d: ", str, last_line_num);
+        rt_snprintf("%s:%d: ", str, last_line_num);
     } else
-        rt_printf("%08llx : ", (long long)wanted_pc);
-    rt_printf("%s %s", msg, func_name[0] ? func_name : "???");
+        rt_snprintf("%08llx : ", (long long)wanted_pc);
+    rt_snprintf("%s %s", msg, func_name[0] ? func_name : "???");
 #if 0
     if (--i >= 0) {
-        rt_printf(" (included from ");
+        rt_snprintf(" (included from ");
         for (;;) {
-            rt_printf("%s", incl_files[i]);
+            rt_snprintf("%s", incl_files[i]);
             if (--i < 0)
                 break;
-            rt_printf(", ");
+            rt_snprintf(", ");
         }
-        rt_printf(")");
+        rt_snprintf(")");
     }
 #endif
     return func_addr;
+
+    err: return (addr_t)-2;
 }
 
 #define MAX_128	((8 * sizeof (long long) + 6) / 7)
@@ -653,8 +676,8 @@ dwarf_read_sleb128(unsigned char **ln, unsigned char *end)
 	default: goto next_line; \
 	}
 
-static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc,
-    const char *msg, const char *skip)
+static addr_t rt_getline_dwarf (rt_context *rc, addr_t wanted_pc,
+    const char *msg, const char *skip, char* buf, int buf_size)
 {
     unsigned char *ln;
     unsigned char *cp;
@@ -677,6 +700,7 @@ static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc,
 	unsigned int form;
     } entry_format[256];
     unsigned int dir_size;
+    int tmp;
 #if 0
     char *dirs[DIR_TABLE_SIZE];
 #endif
@@ -952,23 +976,27 @@ found:
     if (filename) {
 	if (skip[0] && strstr(filename, skip))
 	    return (addr_t)-1;
-	rt_printf("%s:%d: ", filename, line);
+	rt_snprintf("%s:%d: ", filename, line);
     }
     else
-	rt_printf("0x%08llx : ", (long long)wanted_pc);
-    rt_printf("%s %s", msg, function ? function : "???");
+	rt_snprintf("0x%08llx : ", (long long)wanted_pc);
+    rt_snprintf("%s %s", msg, function ? function : "???");
     return (addr_t)func_addr;
+
+    err: return (addr_t)-2;
 }
 
 static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level);
 
-static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap)
+static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap,
+    char** out, int out_len)
 {
     rt_context *rc = &g_rtctxt;
     addr_t pc = 0;
     char skip[100];
-    int i, level, ret, n;
+    int i, level, ret, n, tmp, buf_size = sizeof backtrace_buf;
     const char *a, *b, *msg;
+    char *buf = backtrace_buf;
 
     if (fp) {
         /* we're called from tcc_backtrace. */
@@ -988,41 +1016,55 @@ static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap)
     }
 
     n = rc->num_callers ? rc->num_callers : 6;
+    if (n > buf_size) n = buf_size;
     for (i = level = 0; level < n; i++) {
+        out[i] = buf;
         ret = rt_get_caller_pc(&pc, rc, i);
         a = "%s";
         if (ret != -1) {
-	    if ((addr_t)rc->dwarf_text)
-                pc = rt_printline_dwarf(rc, pc, level ? "by" : "at", skip);
-	    else
-                pc = rt_printline(rc, pc, level ? "by" : "at", skip);
+            if ((addr_t)rc->dwarf_text)
+                pc = rt_getline_dwarf(rc, pc, level ? "by" : "at", skip, buf, buf_size);
+            else
+                pc = rt_getline(rc, pc, level ? "by" : "at", skip, buf, buf_size);
             if (pc == (addr_t)-1)
                 continue;
+            else if (pc == (addr_t)-2)
+                // no more room in backtrace_buf
+                break;
             a = ": %s";
         }
+        tmp = strlen(buf) + 1;
+        buf += tmp;
+        buf_size -= tmp;
         if (level == 0) {
-            rt_printf(a, msg);
-            rt_vprintf(fmt, ap);
+            rt_snprintf(a, msg);
+            rt_vsnprintf(fmt, ap);
         } else if (ret == -1)
             break;
-        rt_printf("\n");
+        rt_snprintf("\n");
         if (ret == -1 || (pc == (addr_t)rc->top_func && pc))
             break;
         ++level;
     }
 
     rc->ip = rc->fp = 0;
-    return 0;
+    return i;
+
+    err: return i;
+
 }
 
 /* emit a run time error at position 'pc' */
 static int rt_error(const char *fmt, ...)
 {
     va_list ap;
-    int ret;
+    int i, ret;
+    char *out[128];
     va_start(ap, fmt);
-    ret = _rt_error(0, 0, fmt, ap);
+    ret = _rt_error(0, 0, fmt, ap, out, 128);
     va_end(ap);
+    for (i = 0; i < ret; i++)
+        rt_printf("%d %s\n", i, out[i]);
     return ret;
 }
 
