• Stars
    star
    128
  • Rank 271,415 (Top 6 %)
  • Language
    Dart
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A package for manipulating stack traces and printing them readably.

Dart CI pub package package publisher

This library provides the ability to parse, inspect, and manipulate stack traces produced by the underlying Dart implementation. It also provides functions to produce string representations of stack traces in a more readable format than the native StackTrace implementation.

Traces can be parsed from native StackTraces using Trace.from, or captured using Trace.current. Native StackTraces can also be directly converted to human-readable strings using Trace.format.

Here's an example native stack trace from debugging this library:

#0      Object.noSuchMethod (dart:core-patch:1884:25)
#1      Trace.terse.<anonymous closure> (file:///usr/local/google-old/home/goog/dart/dart/pkg/stack_trace/lib/src/trace.dart:47:21)
#2      IterableMixinWorkaround.reduce (dart:collection:29:29)
#3      List.reduce (dart:core-patch:1247:42)
#4      Trace.terse (file:///usr/local/google-old/home/goog/dart/dart/pkg/stack_trace/lib/src/trace.dart:40:35)
#5      format (file:///usr/local/google-old/home/goog/dart/dart/pkg/stack_trace/lib/stack_trace.dart:24:28)
#6      main.<anonymous closure> (file:///usr/local/google-old/home/goog/dart/dart/test.dart:21:29)
#7      _CatchErrorFuture._sendError (dart:async:525:24)
#8      _FutureImpl._setErrorWithoutAsyncTrace (dart:async:393:26)
#9      _FutureImpl._setError (dart:async:378:31)
#10     _ThenFuture._sendValue (dart:async:490:16)
#11     _FutureImpl._handleValue.<anonymous closure> (dart:async:349:28)
#12     Timer.run.<anonymous closure> (dart:async:2402:21)
#13     Timer.Timer.<anonymous closure> (dart:async-patch:15:15)

and its human-readable representation:

dart:core-patch 1884:25                     Object.noSuchMethod
pkg/stack_trace/lib/src/trace.dart 47:21    Trace.terse.<fn>
dart:collection 29:29                       IterableMixinWorkaround.reduce
dart:core-patch 1247:42                     List.reduce
pkg/stack_trace/lib/src/trace.dart 40:35    Trace.terse
pkg/stack_trace/lib/stack_trace.dart 24:28  format
test.dart 21:29                             main.<fn>
dart:async 525:24                           _CatchErrorFuture._sendError
dart:async 393:26                           _FutureImpl._setErrorWithoutAsyncTrace
dart:async 378:31                           _FutureImpl._setError
dart:async 490:16                           _ThenFuture._sendValue
dart:async 349:28                           _FutureImpl._handleValue.<fn>
dart:async 2402:21                          Timer.run.<fn>
dart:async-patch 15:15                      Timer.Timer.<fn>

You can further clean up the stack trace using Trace.terse. This folds together multiple stack frames from the Dart core libraries, so that only the core library method that was directly called from user code is visible. For example:

dart:core                                   Object.noSuchMethod
pkg/stack_trace/lib/src/trace.dart 47:21    Trace.terse.<fn>
dart:core                                   List.reduce
pkg/stack_trace/lib/src/trace.dart 40:35    Trace.terse
pkg/stack_trace/lib/stack_trace.dart 24:28  format
test.dart 21:29                             main.<fn>

Stack Chains

This library also provides the ability to capture "stack chains" with the Chain class. When writing asynchronous code, a single stack trace isn't very useful, since the call stack is unwound every time something async happens. A stack chain tracks stack traces through asynchronous calls, so that you can see the full path from main down to the error.

To use stack chains, just wrap the code that you want to track in Chain.capture. This will create a new Zone in which stack traces are recorded and woven into chains every time an asynchronous call occurs. Zones are sticky, too, so any asynchronous operations started in the Chain.capture callback will have their chains tracked, as will asynchronous operations they start and so on.

Here's an example of some code that doesn't capture its stack chains:

import 'dart:async';

void main() {
  _scheduleAsync();
}

void _scheduleAsync() {
  Future.delayed(Duration(seconds: 1)).then((_) => _runAsync());
}

void _runAsync() {
  throw 'oh no!';
}

If we run this, it prints the following:

Unhandled exception:
oh no!
#0      _runAsync (file:///Users/kevmoo/github/stack_trace/example/example.dart:12:3)
#1      _scheduleAsync.<anonymous closure> (file:///Users/kevmoo/github/stack_trace/example/example.dart:8:52)
<asynchronous suspension>

Notice how there's no mention of main in that stack trace. All we know is that the error was in runAsync; we don't know why runAsync was called.

Now let's look at the same code with stack chains captured:

import 'dart:async';

import 'package:stack_trace/stack_trace.dart';

void main() {
  Chain.capture(_scheduleAsync);
}

void _scheduleAsync() {
  Future.delayed(Duration(seconds: 1)).then((_) => _runAsync());
}

void _runAsync() {
  throw 'oh no!';
}

Now if we run it, it prints this:

Unhandled exception:
oh no!
example/example.dart 14:3                                     _runAsync
example/example.dart 10:52                                    _scheduleAsync.<fn>
package:stack_trace/src/stack_zone_specification.dart 126:26  StackZoneSpecification._registerUnaryCallback.<fn>.<fn>
package:stack_trace/src/stack_zone_specification.dart 208:15  StackZoneSpecification._run
package:stack_trace/src/stack_zone_specification.dart 126:14  StackZoneSpecification._registerUnaryCallback.<fn>
dart:async/zone.dart 1406:47                                  _rootRunUnary
dart:async/zone.dart 1307:19                                  _CustomZone.runUnary
===== asynchronous gap ===========================
dart:async/zone.dart 1328:19                                  _CustomZone.registerUnaryCallback
dart:async/future_impl.dart 315:23                            Future.then
example/example.dart 10:40                                    _scheduleAsync
package:stack_trace/src/chain.dart 97:24                      Chain.capture.<fn>
dart:async/zone.dart 1398:13                                  _rootRun
dart:async/zone.dart 1300:19                                  _CustomZone.run
dart:async/zone.dart 1803:10                                  _runZoned
dart:async/zone.dart 1746:10                                  runZoned
package:stack_trace/src/chain.dart 95:12                      Chain.capture
example/example.dart 6:9                                      main
dart:isolate-patch/isolate_patch.dart 297:19                  _delayEntrypointInvocation.<fn>
dart:isolate-patch/isolate_patch.dart 192:12                  _RawReceivePortImpl._handleMessage

That's a lot of text! If you look closely, though, you can see that main is listed in the first trace in the chain.

Thankfully, you can call Chain.terse just like Trace.terse to get rid of all the frames you don't care about. The terse version of the stack chain above is this:

test.dart 17:3       runAsync
test.dart 13:28      scheduleAsync.<fn>
===== asynchronous gap ===========================
dart:async           _Future.then
test.dart 13:12      scheduleAsync
test.dart 7:18       main.<fn>
package:stack_trace  Chain.capture
test.dart 6:16       main

That's a lot easier to understand!

More Repositories

1

sdk

The Dart SDK, including the VM, dart2js, core libraries, and more.
Dart
9,763
star
2

language

Design of the Dart language
TeX
2,536
star
3

dart-pad

An online Dart editor with support for console, web, and Flutter apps
Dart
1,637
star
4

pub

The pub command line tool
Dart
1,024
star
5

http

A composable API for making HTTP requests in Dart.
Dart
994
star
6

site-www

Source for Dart website
Dart
922
star
7

shelf

Web server middleware for Dart
Dart
870
star
8

build

A build system for Dart written in Dart
Dart
763
star
9

pub-dev

The pub.dev website
Dart
762
star
10

dart-vim-plugin

Syntax highlighting for Dart in Vim
Vim Script
635
star
11

dart_style

An opinionated formatter/linter for Dart code
Dart
629
star
12

linter

Linter for Dart.
Dart
626
star
13

mockito

Mockito-inspired mock library for Dart
Dart
618
star
14

samples

A collection of Dart code samples by Dart DevRel
Dart
576
star
15

test

A library for writing unit tests in Dart.
Dart
486
star
16

source_gen

Automatic source code generation for Dart
Dart
479
star
17

dartdoc

API documentation tool for Dart.
Dart
459
star
18

markdown

A Dart markdown library
Dart
428
star
19

code_builder

A fluent API for generating valid Dart source code
Dart
416
star
20

web_socket_channel

StreamChannel wrappers for WebSockets.
Dart
405
star
21

leak_tracker

A framework for memory leak tracking for Dart and Flutter applications.
Dart
369
star
22

ffigen

FFI binding generator
Dart
364
star
23

collection

The collection package for Dart contains a number of separate libraries with utility functions and classes that makes working with collections easier.
Dart
359
star
24

logging

A Dart package for debug and error logging.
Dart
311
star
25

wasm

Utilities for loading and running WASM modules from Dart code
Dart
309
star
26

async

A Dart package that contains tools to work with asynchronous computations.
Dart
300
star
27

html

Dart port of html5lib. For parsing HTML/HTML5 with Dart. Works in the client and on the server.
Dart
268
star
28

crypto

A set of cryptographic functions implemented in pure Dart.
Dart
261
star
29

path

A string-based path manipulation library.
Dart
215
star
30

webdev

A CLI for Dart web development.
Dart
210
star
31

oauth2

An OAuth2 client library for Dart.
Dart
208
star
32

args

A command-line argument parsing library for Dart.
Dart
202
star
33

pana

Package ANAlysis for Dart
Dart
190
star
34

setup-dart

A GitHub Action to install and setup a Dart SDK.
Dart
176
star
35

yaml

A Dart YAML parser.
Dart
163
star
36

jnigen

Experimental bindings generator for Java bindings through dart:ffi and JNI.
Dart
154
star
37

http2

A HTTP/2 implementation for dart.
Dart
150
star
38

ffi

Utilities for working with Foreign Function Interface (FFI) code
Dart
149
star
39

homebrew-dart

Dart team's official tap for homebrew.
Ruby
147
star
40

sample-pop_pop_win

"Pop, Pop, Win!" is an implementation of Minesweeper in Dart.
Dart
146
star
41

usage

A Google Analytics wrapper for command-line, web, and Flutter apps.
Dart
143
star
42

watcher

A file system watcher library for Dart.
Dart
135
star
43

gcloud

High-level interfaces to Google Cloud Platform APIs
Dart
127
star
44

mime

Dart package for working with MIME type definitions and for processing streams of MIME multipart media types.
Dart
124
star
45

stream_transform

Dart utility methods to create StreamTransfomer instances to manipulate Streams
Dart
118
star
46

lints

Official Dart lint rules; the core and recommended set of lints suggested by the Dart team.
Dart
106
star
47

coverage

Dart coverage data manipulation and formatting
Dart
102
star
48

appengine

Dart support for App Engine managed VMs
Dart
93
star
49

csslib

A library for parsing CSS.
Dart
92
star
50

benchmark_harness

The official benchmark harness for Dart
Dart
92
star
51

web

Lightweight browser API bindings built around JS static interop.
Dart
89
star
52

sse

Dart Server Sent Events package
Dart
89
star
53

fake_async

Fake asynchronous events for deterministic testing.
Dart
87
star
54

native

Dart packages related to FFI and native assets bundling.
Dart
74
star
55

matcher

A declarative API for specifying expectations.
Dart
70
star
56

json_rpc_2

A Dart implementation of the JSON-RPC 2.0 spec.
Dart
70
star
57

convert

Conversion utilities
Dart
68
star
58

characters

A package for characters represented as unicode extended grapheme clusters
Dart
67
star
59

dart-docker

Docker images for the Dart programming language (https://dart.dev)
Dart
65
star
60

pub_semver

A package for working with Pub/semver-style versions and version constraints
Dart
62
star
61

string_scanner

A class for parsing strings using a sequence of patterns.
Dart
59
star
62

cli_util

A library to help in building Dart command-line apps
Dart
59
star
63

glob

Bash-style filename globbing for Dart.
Dart
55
star
64

pubspec_parse

Simple package for parsing pubspec.yaml files with a type-safe API and rich error reporting
Dart
50
star
65

i18n

A general mono-repo for Dart i18n and l10n packages.
Dart
50
star
66

pool

A class for managing a finite pool of resources.
Dart
48
star
67

stream_channel

An abstraction for two-way communication channels.
Dart
47
star
68

io

Utilities for the Dart VM's dart:io.
Dart
47
star
69

fixnum

Fixed-width integer library for Dart.
Dart
44
star
70

clock

A fakeable wrapper for dart:core clock APIs.
Dart
40
star
71

site-shared

Content shared across Dart websites
Dart
40
star
72

co19

A Dart language and library conformance test suite
Dart
36
star
73

http_parser

A platform-independent Dart package for parsing and serializing HTTP formats.
Dart
35
star
74

ecosystem

This repository is home to general Dart Ecosystem tools and packages.
Dart
35
star
75

typed_data

Utility functions and classes that makes working with typed data lists easier in Dart
Dart
35
star
76

source_span

A library for identifying source spans and locations.
Dart
28
star
77

http_multi_server

A dart:io HttpServer wrapper that handles requests from multiple servers.
Dart
27
star
78

native_synchronization

Low-level synchronization primitives built using dart:ffi.
Dart
26
star
79

dartbug.com

The redirect service for Dart issues and bugs.
Dart
23
star
80

bazel_worker

Dart integration for Bazel build system
Dart
23
star
81

boolean_selector

A flexible syntax for boolean expressions.
Dart
23
star
82

os_detect

Dart multi-platform operating system identification
Dart
22
star
83

dart-syntax-highlight

Tools and documentation for how Dart code is formatted
Dart
21
star
84

yaml_edit

A library for YAML manipulation with comment and whitespace preservation.
Dart
21
star
85

tools

This repository is home to tooling related Dart packages.
Dart
21
star
86

api.dart.dev

Dart API docs
Python
19
star
87

dart_ci

Tools used by Dart's continuous integration (CI) testing that aren't needed by Dart SDK contributors. Mirrored from dart.googlesource.com/dart_ci. Do not land pull requests on Github.
Dart
18
star
88

test_process

A Dart package for testing subprocesses
Dart
17
star
89

term_glyph

Useful glyphs and Windows-safe equivalents
Dart
16
star
90

browser_launcher

Provides a standardized way to launch web browsers.
Dart
16
star
91

source_maps

A package to programmatically manipulate source maps.
Dart
15
star
92

grpc_cronet

Flutter dart:grpc implementation that uses the Cronet native library.
Dart
14
star
93

timing

A Dart package for tracking time spent in child operations
Dart
14
star
94

package_config

Support for working with Package Resolution Configuration files
Dart
14
star
95

root_certificates

The set of root certificates trusted by dart:io's default SecurityContext. Taken from Mozilla's NSS library.
C++
11
star
96

test_descriptor

Provides a convenient, easy-to-read API for defining and verifying directory structures in tests
Dart
10
star
97

dartlang_project_templates

Project templates for new repos under the dart-lang organization
Dart
8
star
98

source_map_stack_trace

Convert stack traces generated by dart2js-compiled code into readable native Dart stack traces
Dart
7
star
99

.github

GitHub default community health file for dart-lang repos
7
star
100

test_reflective_loader

Discover tests and test suites using reflection
Dart
5
star