Skip to content

Commit

Permalink
Print most recent call last as CPython does.
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Jun 1, 2017
1 parent a893877 commit 90fb4dd
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ each_backtrace(mrb_state *mrb, mrb_int ciidx, mrb_code *pc0, each_backtrace_func
}
}

/* mrb_print_backtrace
function to retrieve backtrace information from the exception.
note that if you call method after the exception, call stack will be
overwritten. So invoke these functions just after detecting exceptions.
*/

#ifndef MRB_DISABLE_STDIO

static void
Expand All @@ -89,8 +82,8 @@ print_backtrace(mrb_state *mrb, mrb_value backtrace)
fprintf(stream, "trace:\n");

n = RARRAY_LEN(backtrace);
for (i = 0; i < n; i++) {
mrb_value entry = RARRAY_PTR(backtrace)[i];
for (i=0; n--; i++) {
mrb_value entry = RARRAY_PTR(backtrace)[n];

if (mrb_string_p(entry)) {
fprintf(stream, "\t[%d] %.*s\n", i, (int)RSTRING_LEN(entry), RSTRING_PTR(entry));
Expand All @@ -103,7 +96,7 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
{
FILE *stream = stderr;
struct backtrace_location *bt;
mrb_int n, i;
int n, i;

bt = (struct backtrace_location*)mrb_data_check_get_ptr(mrb, packed, &bt_type);
if (bt == NULL) {
Expand All @@ -112,9 +105,9 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
n = (mrb_int)RDATA(packed)->flags;

fprintf(stream, "trace:\n");
for (i = 0; i < n; i++) {
for (i = 0; n--; i++) {
int ai = mrb_gc_arena_save(mrb);
struct backtrace_location *entry = &bt[i];
struct backtrace_location *entry = &bt[n];
if (entry->filename == NULL) continue;
fprintf(stream, "\t[%d] %s:%d", (int)i, entry->filename, entry->lineno);
if (entry->method_id != 0) {
Expand All @@ -128,6 +121,11 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
}
}

/* mrb_print_backtrace
function to retrieve backtrace information from the last exception.
*/

MRB_API void
mrb_print_backtrace(mrb_state *mrb)
{
Expand Down

0 comments on commit 90fb4dd

Please sign in to comment.