Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a "stack trace" that traces all async calls, with Dart?

Consider code like this:

import 'dart:async';

foo() {
  new Timer(onesec, bar);
}

bar() {
  throw "from bar";
}

const onesec = const Duration(seconds:1);

main() {
  runZoned(() {
  new Timer(onesec, foo);
  },
  onError: (e, stackTrace) => print(stackTrace));
}

How can I tell that bar was "called" by foo in the stackTrace that I print out?

I'd like to see something like:

bar
...
foo
...
main
like image 254
Seth Ladd Avatar asked Feb 27 '14 00:02

Seth Ladd


People also ask

What is async stack traces?

Asynchronous stack traces allow you to inspect function calls beyond the current event loop. This is particularly useful because you can examine the scope of previously executed frames that are no longer on the event loop. This feature is currently an experiment and needs to be enabled.

What is full stack trace?

A full stack trace allows you to see the chain of files from when your custom tag reached your set breakpoint. If you click on any of the pages in the caller chain then FusionDebug will show you that page and will highlight the line at which the next page in the chain was called.

How do I capture a stack trace?

Get a Stack Trace Using the Thread Class We can obtain a stack trace from a thread by calling the getStackTrace() method on the Thread instance. It returns an array of StackTraceElement, from which details about stack frames of the thread can be found.

What is stack trace in Dart?

A StackTrace is intended to convey information to the user about the call sequence that triggered an exception. These objects are created by the runtime, it is not possible to create them programmatically.


1 Answers

Have a look at the stack_trace package. It uses zones to keep track of asynchronous callbacks. Capturing stack traces for every asynchronous callback is expensive, but for debugging it is definitely worth it.

Example output from the package:

http://dartlang.org/foo/bar.dart 10:11  Foo.<fn>.bar
http://dartlang.org/foo/baz.dart        Foo.<fn>.bar
===== asynchronous gap ===========================
http://dartlang.org/foo/bang.dart 10:11  Foo.<fn>.bar
http://dartlang.org/foo/quux.dart        Foo.<fn>.bar

According to the doc, the easiest way to get these traces is to use Chain.capture.

like image 134
Florian Loitsch Avatar answered Oct 17 '22 04:10

Florian Loitsch