├── test ├── test_helper.exs └── ex_doctor_test.exs ├── .formatter.exs ├── .gitignore ├── lib ├── ex_doctor │ └── example.ex └── ex_doctor.ex ├── mix.exs ├── mix.lock ├── LICENSE └── README.md /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /test/ex_doctor_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ExDoctorTest do 2 | use ExUnit.Case 3 | doctest ExDoctor 4 | 5 | test "greets the world" do 6 | assert ExDoctor.hello() == :world 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | ex_doctor-*.tar 24 | 25 | # Temporary files, for example, from tests. 26 | /tmp/ 27 | -------------------------------------------------------------------------------- /lib/ex_doctor/example.ex: -------------------------------------------------------------------------------- 1 | defmodule ExDoctor.Example do 2 | @moduledoc """ 3 | This module contains sample functions that you can trace with `ExDoctor`. 4 | """ 5 | 6 | @sleep 1 7 | 8 | @doc "Calculates factorial of `n` with a delay of 1 ms after each step" 9 | def sleepy_factorial(n) when n > 0 do 10 | :timer.sleep(@sleep) 11 | n * sleepy_factorial(n - 1) 12 | end 13 | 14 | def sleepy_factorial(0) do 15 | :timer.sleep(@sleep) 16 | 1 17 | end 18 | 19 | @doc """ 20 | Calculates the `n`-th term of the Fibonacci sequence. 21 | 22 | This recursive implementation is suboptimal on purpose. 23 | The goal is to have a function with extensive redundant branching. 24 | """ 25 | def fib(n) when n > 1 do 26 | fib(n - 1) + fib(n - 2) 27 | end 28 | 29 | def fib(1) do 30 | 1 31 | end 32 | 33 | def fib(0) do 34 | 0 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /lib/ex_doctor.ex: -------------------------------------------------------------------------------- 1 | defmodule ExDoctor do 2 | require Record 3 | 4 | @moduledoc """ 5 | This module defines records from the `:tr` module, allowing to use them in `iex`. 6 | 7 | The records are obtained by calling `Record.extract_all(from_lib: "erlang_doctor/src/tr.erl")` 8 | 9 | This module can be dynamically loaded without access to `tr.erl`, so they are listed literally. 10 | """ 11 | 12 | @records [ 13 | tr: [ 14 | index: :undefined, 15 | pid: :undefined, 16 | event: :undefined, 17 | mfa: :no_mfa, 18 | data: :undefined, 19 | ts: :undefined, 20 | info: :no_info 21 | ], 22 | node: [ 23 | module: :undefined, 24 | function: :undefined, 25 | args: :undefined, 26 | children: [], 27 | result: :undefined 28 | ] 29 | ] 30 | 31 | for {name, fields} <- @records do 32 | Record.defrecord(name, fields) 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ExDoctor.MixProject do 2 | use Mix.Project 3 | 4 | @version "0.3.2" 5 | 6 | def project do 7 | [ 8 | app: :ex_doctor, 9 | description: "Lightweight tracing, debugging and profiling utility", 10 | version: @version, 11 | elixir: "~> 1.11", 12 | start_permanent: Mix.env() == :prod, 13 | deps: deps(), 14 | package: package(), 15 | name: "ExDoctor", 16 | source_url: "https://github.com/chrzaszcz/ex_doctor", 17 | docs: docs() 18 | ] 19 | end 20 | 21 | # Run "mix help compile.app" to learn about applications. 22 | def application do 23 | [ 24 | extra_applications: [:logger] 25 | ] 26 | end 27 | 28 | # Run "mix help deps" to learn about dependencies. 29 | defp deps do 30 | [ 31 | {:erlang_doctor, @version}, 32 | {:ex_doc, "~> 0.31", only: :dev, runtime: false} 33 | # {:dep_from_hexpm, "~> 0.3.0"}, 34 | # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} 35 | ] 36 | end 37 | 38 | defp package do 39 | [ 40 | licenses: ["Apache-2.0"], 41 | links: %{"GitHub" => "https://github.com/chrzaszcz/ex_doctor"} 42 | ] 43 | end 44 | 45 | defp docs do 46 | [ 47 | main: "readme", 48 | extras: ["README.md"] 49 | ] 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, 3 | "erlang_doctor": {:hex, :erlang_doctor, "0.3.2", "49eb56c2efede6abb4ba65ae6b018f51128997a4981552f1a62475bcc091d457", [:rebar3], [], "hexpm", "d1d3fcd4ffae55925266c6700964544a51cbf4023a03fcb3824cad78257d6447"}, 4 | "ex_doc": {:hex, :ex_doc, "0.31.1", "8a2355ac42b1cc7b2379da9e40243f2670143721dd50748bf6c3b1184dae2089", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3178c3a407c557d8343479e1ff117a96fd31bafe52a039079593fb0524ef61b0"}, 5 | "makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"}, 6 | "makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"}, 7 | "makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"}, 8 | "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, 9 | } 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ExDoctor 2 | [![Hex.pm Version](https://img.shields.io/hexpm/v/ex_doctor)](https://hex.pm/packages/ex_doctor) 3 | [![Hex Docs](https://img.shields.io/badge/hex-docs-yellow.svg)](https://hexdocs.pm/ex_doctor/) 4 | 5 | Lightweight tracing, debugging and profiling tool powered by [Erlang Doctor](https://hex.pm/packages/erlang_doctor). 6 | It collects traces from your Elixir system in an ETS table, putting minimal impact on the system. 7 | After collecting the traces, you can query and analyse them. 8 | By separating data collection from analysis, this tool helps you limit unnecessary repetition and guesswork. 9 | 10 | ## Quick start 11 | 12 | To quickly try it out right now, copy & paste the following to your `iex`: 13 | 14 | ```elixir 15 | :ssl.start; :inets.start; for p <- ["erlang_doctor/master/src/tr.erl", "ex_doctor/main/lib/ex_doctor.ex"] do {:ok, {{_, 200, _}, _, src}} = :httpc.request("https://raw.githubusercontent.com/chrzaszcz/" <> p); tp = "/tmp/" <> Path.basename(p); File.write!(tp, src); c tp end; import ExDoctor; :tr.start 16 | ``` 17 | 18 | This snippet downloads, compiles and starts two modules: 19 | 20 | - [`:tr`](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html) is the main module of [Erlang Doctor](https://hex.pm/packages/erlang_doctor), which provides all the functionality. 21 | - `ExDoctor` is a small Elixir module, which allows using the Erlang records defined in `tr.hrl`. 22 | 23 | The Erlang records are used to allow quick and easy pattern-matching, which is used very frequently in ExDoctor. 24 | Maps are not used, because they can be a lot slower and consume more memory (this is verified by benchmarks). 25 | 26 | The easiest way to use it is the following: 27 | 28 | ```elixir 29 | :tr.trace([YourModule]) 30 | YourModule.some_function() 31 | :tr.select 32 | ``` 33 | 34 | You should see the collected traces for the call and return of `YourModule.some_function/0`. 35 | 36 | ### Include it as a dependency 37 | 38 | The [package](https://hex.pm/packages/ex_doctor) can be installed by adding `ex_doctor` to your list of dependencies in `mix.exs`: 39 | 40 | ```elixir 41 | def deps do 42 | [ 43 | {:ex_doctor, "~> 0.3.2"} 44 | ] 45 | end 46 | ``` 47 | 48 | ### Use it during development 49 | 50 | You can make Erlang Doctor available in `iex` by cloning it to `EX_DOCTOR_PATH`, 51 | compiling it with `mix`, and loading it in your `~/.iex.exs` file: 52 | 53 | ```elixir 54 | Code.append_path("EX_DOCTOR_PATH/_build/dev/lib/erlang_doctor/ebin") 55 | Code.append_path("EX_DOCTOR_PATH/_build/dev/lib/ex_doctor/ebin") 56 | import ExDoctor 57 | Code.ensure_loaded!(:tr) 58 | ``` 59 | 60 | ## Tracing: data collection 61 | 62 | You can follow the examples on your own - just call `iex -S mix` in `EX_DOCTOR_PATH`, 63 | and execute the numbered commands in the same order. 64 | 65 | ### Setting up: `start`, `start_link` 66 | 67 | In our case ExDoctor is automatically started by `mix`, but if you need to start it yourself, call `:tr.start/0`. 68 | 69 | There is also `:tr.start/1`, which accepts a [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:init_options/0), including: 70 | 71 | - `tab`: collected traces are stored in an ETS table with this name (default: `:trace`), 72 | - `limit`: maximum number of traces in the table - when it is reached, tracing is stopped (default: no limit). 73 | 74 | There are `:tr.start_link/0` and `:tr.start_link/1` as well, and they are intended for use with the whole application. 75 | 76 | ### Tracing with `trace` 77 | 78 | Let's set up an alias for the `Example` module, because it will be used very often: 79 | 80 | ```elixir 81 | iex(1)> alias ExDoctor.Example 82 | ExDoctor.Example 83 | ``` 84 | 85 | To trace function calls for given modules, use `:tr.trace/1`, providing a list of traced modules: 86 | 87 | ```elixir 88 | iex(2)> :tr.trace([Example]) 89 | :ok 90 | ``` 91 | 92 | You can provide `{module, function, arity}` tuples in the list as well. 93 | The function `:tr.trace_app/1` traces an application, and `:tr.trace_apps/1` traces multiple ones. 94 | 95 | If you need to trace an application and some additional modules, use `:tr.app_modules/1` to get the list of modules for an application: 96 | 97 | ```elixir 98 | :tr.trace([Module1, Module2 | :tr.app_modules(:your_app)]) 99 | ``` 100 | 101 | If you want to trace selected processes instead of all of them, you can use 102 | `:tr.trace/2`: 103 | 104 | ```elixir 105 | :tr.trace([Module1, Module2], [Pid1, Pid2]) 106 | ``` 107 | 108 | The `:tr.trace/1` function also accepts a [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:trace_options/0), which include: 109 | 110 | - `modules`: a list of module names or `{module, function, arity}` tuples. The list is empty by default. 111 | - `pids`: a list of Pids of processes to trace, or `:all` (default) to trace all processes. 112 | - `msg`: `:none` (default), `:all`, `:send` or `:recv`. Specifies which message events will be traced. By default no messages are traced. 113 | - `msg_trigger`: `:after_traced_call` (default) or `:always`. By default, traced messages in each process are stored after the first traced function call in that process. The goal is to limit the number of traced messages, which can be huge in the entire Erlang system. If you want all messages, set it to `:always`. 114 | 115 | This means that `:tr.trace(modules, pids)` is a shortcut for `:tr.trace(%{modules: modules, pids: pids})`, 116 | and `:tr.trace(modules)` is a shortcut for `:tr.trace(%{modules: modules})`. 117 | 118 | ### Calling the traced function 119 | 120 | Now we can call some functions - let's trace the following function call. 121 | It calculates the factorial recursively and sleeps 1 ms between each step. 122 | 123 | ```elixir 124 | iex(3)> Example.sleepy_factorial(3) 125 | 6 126 | ``` 127 | 128 | ### Stopping tracing 129 | 130 | You can stop tracing with `:tr.stop_tracing/0`: 131 | 132 | ```elixir 133 | iex(4)> :tr.stop_tracing() 134 | :ok 135 | ``` 136 | 137 | It's good to stop it as soon as possible to avoid accumulating too many traces in the ETS table. 138 | Usage of `tr` on production systems is risky, but if you have to do it, start and stop the tracer in the same command, 139 | e.g. for one second with: 140 | 141 | ```elixir 142 | :tr.trace(modules); :timer.sleep(1000); :tr.stop_tracing() 143 | ``` 144 | 145 | ## Debugging: data analysis 146 | 147 | The collected traces are stored in an ETS table (default name: `:trace`). 148 | They are stored as [`tr`](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:tr/0) records with the following fields: 149 | 150 | - `index`: trace identifier, auto-incremented for each received trace. 151 | - `pid`: process ID associated with the trace. 152 | - `event`: `:call`, `:return` or `:exception` for function traces; `:send` or `:recv` for messages. 153 | - `mfa`: `{module, function, arity}` for function traces; `:no_mfa` for messages. 154 | - `data`: argument list (for calls), returned value (for returns) or class and value (for exceptions). 155 | - `timestamp` in microseconds. 156 | - `info`: For `:send` events it is a `{to, exists}` tuple, where `to` is the recipient pid, and `exists` is a boolean indicating if the recipient process existed. For other events it is `:no_info`. 157 | 158 | You can load the record definitions with `import ExDoctor`: 159 | 160 | ```elixir 161 | iex(5)> import ExDoctor 162 | ExDoctor 163 | ``` 164 | 165 | ### Trace selection: `select` 166 | 167 | Use `:tr.select/0` to select all collected traces, which include a system call to `__info__/1` 168 | followed by the call to `sleepy_factorial/1`. 169 | 170 | ```elixir 171 | iex(6)> :tr.select() 172 | [ 173 | {:tr, 1, #PID<0.187.0>, :call, {ExDoctor.Example, :__info__, 1}, 174 | [:deprecated], 1705413018330494, :no_info}, 175 | {:tr, 2, #PID<0.187.0>, :return, {ExDoctor.Example, :__info__, 1}, [], 176 | 1705413018330501, :no_info}, 177 | {:tr, 3, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [3], 178 | 1705413018330532, :no_info}, 179 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [2], 180 | 1705413018332522, :no_info}, 181 | {:tr, 5, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [1], 182 | 1705413018334514, :no_info}, 183 | {:tr, 6, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [0], 184 | 1705413018336506, :no_info}, 185 | {:tr, 7, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 1, 186 | 1705413018338509, :no_info}, 187 | {:tr, 8, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 1, 188 | 1705413018338511, :no_info}, 189 | {:tr, 9, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 2, 190 | 1705413018338512, :no_info}, 191 | {:tr, 10, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 6, 192 | 1705413018338513, :no_info} 193 | ] 194 | ``` 195 | 196 | The `:tr.select/1` function accepts a fun that is passed to `:ets.fun2ms/1`. 197 | This way you can limit the selection to specific items and select only some fields from the [`tr`](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:tr/0) record: 198 | 199 | ```elixir 200 | iex(7)> :tr.select(fn tr(event: :call, data: [n]) when is_integer(n) -> n end) 201 | [3, 2, 1, 0] 202 | ``` 203 | 204 | Use `:tr.select/2` to further filter the results by searching for a term in the `data` field 205 | (recursively searching in lists, tuples and maps). 206 | 207 | ```elixir 208 | iex(8)> :tr.select(fn t -> t end, 2) 209 | [ 210 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [2], 211 | 1705413018332522, :no_info}, 212 | {:tr, 9, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 2, 213 | 1705413018338512, :no_info} 214 | ] 215 | ``` 216 | 217 | ### Trace filtering: `filter` 218 | 219 | Sometimes it might be easier to use `:tr.filter/1`, because it can accept any function as the argument. 220 | You can use `:tr.contains_data/2` to search for a term like in the example above. 221 | 222 | ```elixir 223 | iex(9)> traces = :tr.filter(fn t -> :tr.contains_data(2, t) end) 224 | [ 225 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [2], 226 | 1705413018332522, :no_info}, 227 | {:tr, 9, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 2, 228 | 1705413018338512, :no_info} 229 | ] 230 | ``` 231 | 232 | The provided function is a predicate, which has to return `true` for the matching traces. 233 | For other traces it can return another value, or even raise an exception: 234 | 235 | ```elixir 236 | iex(10)> :tr.filter(fn tr(data: [2]) -> true end) 237 | [ 238 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [2], 239 | 1705413018332522, :no_info} 240 | ] 241 | ``` 242 | 243 | There is also `:tr.filter/2`, which can be used to search in a different table than the current one - or in a list: 244 | 245 | ```elixir 246 | iex(11)> :tr.filter(fn tr(event: :call) -> true end, traces) 247 | [ 248 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [2], 249 | 1705413018332522, :no_info} 250 | ] 251 | ``` 252 | 253 | There are also additional ready-to-use predicates besides `:tr.contains_data/2`: 254 | 255 | 1. `:tr.match_data/2` performs a recursive check for terms like `:tr.contains_data/2`, 256 | but instead of checking for equality, it applies the predicate function 257 | provided as the first argument to each term, and returns `true` if the predicate returned `true` 258 | for any of them. The predicate can return any other value or fail for non-matching terms. 259 | 1. `:tr.contains_val/2` is like `:tr.contains_data/2`, but the second argument is the `data` term itself rather than a trace record with data. 260 | 2. `:tr.match_val/2` is like `:tr.match_data/2`, but the second argument is the `data` term itself rather than a trace record with data. 261 | 262 | By combining these predicates, you can search for complex terms, e.g. 263 | the following expression returns trace records that contain any (possibly nested in tuples/lists/maps) 264 | 3-element tuples with a map as the third element - and that map has to contain the atom `:error` 265 | (possibly nested in tuples/lists/maps). 266 | 267 | 268 | ```elixir 269 | :tr.filter(fn t -> :tr.match_data(fn {_, _, map = %{}} -> :tr.contains_val(:error, map) end, t) end) 270 | ``` 271 | 272 | ### Tracebacks for filtered traces: `tracebacks` 273 | 274 | To find the tracebacks (stack traces) for matching traces, use `:tr.tracebacks/1`: 275 | 276 | ```elixir 277 | iex(12)> :tr.tracebacks(fn tr(data: 1) -> true end) 278 | [ 279 | [ 280 | {:tr, 5, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 281 | [1], 1705413018334514, :no_info}, 282 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 283 | [2], 1705413018332522, :no_info}, 284 | {:tr, 3, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 285 | [3], 1705413018330532, :no_info} 286 | ] 287 | ] 288 | ``` 289 | 290 | Note, that by specifying `data: 1` we are only matching return traces, as call traces always have a list in `data`. 291 | Only one traceback is returned. It starts with a call that returned `1`. What follows is the stack trace for this call. 292 | 293 | One can notice that the call for 0 also returned 1, but the call tree got pruned - whenever two tracebacks overlap, only the shorter one is left. 294 | You can change this by returning tracebacks for all matching traces even if they overlap, setting the `output` option to `:all`. Options are specified in the second argument, which is a map: 295 | 296 | ```elixir 297 | iex(13)> :tr.tracebacks(fn tr(data: 1) -> true end, %{output: :all}) 298 | [ 299 | [ 300 | {:tr, 6, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 301 | [0], 1705413018336506, :no_info}, 302 | {:tr, 5, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 303 | [1], 1705413018334514, :no_info}, 304 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 305 | [2], 1705413018332522, :no_info}, 306 | {:tr, 3, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 307 | [3], 1705413018330532, :no_info} 308 | ], 309 | [ 310 | {:tr, 5, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 311 | [1], 1705413018334514, :no_info}, 312 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 313 | [2], 1705413018332522, :no_info}, 314 | {:tr, 3, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 315 | [3], 1705413018330532, :no_info} 316 | ] 317 | ] 318 | ``` 319 | 320 | The third possibility is `:longest`, which does the opposite of pruning, leaving only the longest tracabacks when they overlap: 321 | 322 | ```elixir 323 | iex(14)> :tr.tracebacks(fn tr(data: 1) -> true end, %{output: :longest}) 324 | [ 325 | [ 326 | {:tr, 6, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 327 | [0], 1705413018336506, :no_info}, 328 | {:tr, 5, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 329 | [1], 1705413018334514, :no_info}, 330 | {:tr, 4, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 331 | [2], 1705413018332522, :no_info}, 332 | {:tr, 3, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 333 | [3], 1705413018330532, :no_info} 334 | ] 335 | ] 336 | ``` 337 | 338 | Possible [options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:tb_options/0) for `:tr.tracebacks/2` include: 339 | 340 | - `tab` is the table or list, which is like the second argument of `:tr.filter/2`. 341 | - `output` - `:shortest` (default), `:all`, `:longest` - see above. 342 | - `format` - `:list` (default), `:tree` - returns a list of (possibly merged) call trees instead tracebacks, `:root` - returns a list of root calls. Trees don't distinguish between `:all` and `:longest` output formats. Using `:root` is equivalent to using `:tree`, and then calling `:tr.roots/1` on the results. There is also `:tr.root/1` for a single tree. 343 | - `order` - `:top_down` (default), `:bottom_up` - call order in each tracaback; only for the `:list` format. 344 | - `limit` - positive integer or `:infinity` (default) - limits the number of matched traces. The actual number of tracebacks returned can be smaller unless `output` is set ot `:all`. 345 | 346 | There are also functions `:tr.traceback/1` and `:tr.traceback/2`. They set `limit` to one and return only one trace if it exists. The options for `:tr.traceback/2` are the same as for `:tr.traceback/2` except `limit`. Additionally, it is possible to pass a [`tr`](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:tr/0) record (or an index) as the first argument to `:tr.traceback/1` or `:tr.traceback/2` to obtain the traceback for the provided trace event. 347 | 348 | ### Trace ranges for filtered traces: `ranges` 349 | 350 | To get a list of traces between each matching call and the corresponding return, use `:tr.ranges/1`: 351 | 352 | ```elixir 353 | iex(15)> :tr.ranges(fn tr(data: [1]) -> true end) 354 | [ 355 | [ 356 | {:tr, 5, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 357 | [1], 1705413018334514, :no_info}, 358 | {:tr, 6, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, 359 | [0], 1705413018336506, :no_info}, 360 | {:tr, 7, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 361 | 1, 1705413018338509, :no_info}, 362 | {:tr, 8, #PID<0.187.0>, :return, {ExDoctor.Example, :sleepy_factorial, 1}, 363 | 1, 1705413018338511, :no_info} 364 | ] 365 | ] 366 | ``` 367 | 368 | There is also `:tr.ranges/2` - it accepts a [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:range_options/0), including: 369 | 370 | - `tab` is the table or list which is like the second argument of `:tr.filter/2`, 371 | - `max_depth` is the maximum depth of nested calls. A message event also adds 1 to the depth. 372 | You can set it to 1 to get only the top-level call and the corresponding return. 373 | - `output` - `:all` (default), `:complete` or `:incomplete` - decides whether the output should contain 374 | complete and/or incomplete ranges. A range is complete if the root call has a return. 375 | For example, you can use `%{output: :incomplete}` to see only the traces with missing returns. 376 | 377 | When you combine the options into `%{output: :incomplete, max_depth: 1}`, 378 | you get all the calls which didn't return (they were still executing when tracing was stopped). 379 | 380 | There are two additional functions: `:tr.range/1` and `:tr.range/2`, which return only one range if it exists. It is possible to pass a [`tr`](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:tr/0) record or an index as the first argument to `:tr.range/1` or `:tr.range/2` as well. 381 | 382 | ### Calling function from a trace: `do` 383 | 384 | It is easy to replay a particular function call with `:tr.do/1`: 385 | 386 | ```elixir 387 | iex(16)> [t] = :tr.filter(fn tr(data: [3]) -> true end) 388 | [ 389 | {:tr, 3, #PID<0.187.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [3], 390 | 1705413018330532, :no_info} 391 | ] 392 | iex(17)> :tr.do(t) 393 | 6 394 | ``` 395 | 396 | This is useful e.g. for checking if a bug has been fixed without running the whole test suite. 397 | This function can be called with an index as the argument. 398 | 399 | ### Browsing traces: `lookup`, `prev`, `next` 400 | 401 | Use `:tr.lookup/1` to obtain the trace record for an index. Also, given a trace record or an index, 402 | you can obtain the next trace record with `:tr.next/1`, or the previous one with `:tr.prev/1`. 403 | 404 | ```elixir 405 | iex(18)> t = :tr.next(t) 406 | {:tr, 4, #PID<0.182.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [2], 407 | 1761670163922507, :no_info} 408 | iex(19)> t = :tr.prev(t) 409 | {:tr, 3, #PID<0.182.0>, :call, {ExDoctor.Example, :sleepy_factorial, 1}, [3], 410 | 1761670163920752, :no_info} 411 | ``` 412 | 413 | When there is no trace to return, the `:not_found` error is raised: 414 | 415 | ```elixir 416 | iex(20)> :tr.prev(1) 417 | ** (ErlangError) Erlang error: :not_found 418 | (erlang_doctor 0.2.9) /Users/pawelchrzaszcz/dev/erlang_doctor/src/tr.erl:629: :tr.prev(1, #Function<15.11954083/1 in :tr.prev/2>, :trace) 419 | iex:53: (file) 420 | ``` 421 | 422 | There are also more advanced variants of these fucntions: `:tr.next/2` and `:tr.prev/2`. 423 | As their second argument, they take a [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:prev_next_options/0), including: 424 | 425 | - `tab` is the table or list (like the second argument of `:tr.filter/2`), 426 | - `pred` is a predicate function that should return `true` for a matching trace record. 427 | For other arguments, it can return a different value or fail. 428 | When used, `tab` will be traversed until a matching trace is found. 429 | 430 | There are also functions `:tr.seq_next/1` and `:tr.seq_prev/1`. 431 | Given a trace record or an index, they first check the `pid` of the process for that trace record, 432 | and then they return next/previous trace record with the same `pid`. 433 | This effect could be achieved with a predicate function: `fn tr(pid: p) -> p == pid end`, 434 | but these utility functions are much more handy. 435 | 436 | ## Profiling 437 | 438 | You can quickly get a hint about possible bottlenecks and redundancies in your system with function call statistics. 439 | 440 | ### Call statistics: `call_stat` 441 | 442 | The argument of `:tr.call_stat/1` is a function that returns a key by which the traces are grouped. 443 | The simplest way to use this function is to look at the total number of calls and their time. 444 | To do this, we group all calls under one key, e.g. `total`: 445 | 446 | ```elixir 447 | iex(21)> :tr.call_stat(fn _ -> :total end) 448 | %{total: {5, 7988, 7988}} 449 | ``` 450 | 451 | Values of the returned map have the following format (time is in microseconds): 452 | 453 | ```{call_count, acc_time, own_time}``` 454 | 455 | In the example there are four calls, which took 7981 microseconds in total. 456 | For nested calls we only take into account the outermost call, so this means that the whole calculation took 7.981 ms. 457 | Let's see how this looks like for individual steps - we can group the stats by the function argument: 458 | 459 | ```elixir 460 | iex(22)> :tr.call_stat(fn tr(data: [n]) -> n end) 461 | %{ 462 | 0 => {1, 2003, 2003}, 463 | 1 => {1, 3997, 1994}, 464 | 2 => {1, 5990, 1993}, 465 | 3 => {1, 7981, 1991}, 466 | :deprecated => {1, 7, 7} 467 | } 468 | ``` 469 | 470 | You can use the provided function to do filtering as well - let's make the output cleaner 471 | by filtering out the unwanted call to `__info__(:deprecated)`: 472 | 473 | ```elixir 474 | iex(23)> :tr.call_stat(fn tr(data: [n]) when is_integer(n) -> n end) 475 | %{ 476 | 0 => {1, 2003, 2003}, 477 | 1 => {1, 3997, 1994}, 478 | 2 => {1, 5990, 1993}, 479 | 3 => {1, 7981, 1991} 480 | } 481 | ``` 482 | 483 | ### Sorted call statistics: `sorted_call_stat` 484 | 485 | You can sort the call stat by accumulated time (descending) with `:tr.sorted_call_stat/1`: 486 | 487 | ```elixir 488 | iex(24)> :tr.sorted_call_stat(fn tr(data: [n]) when is_integer(n) -> n end) 489 | [{3, 1, 7981, 1991}, {2, 1, 5990, 1993}, {1, 1, 3997, 1994}, {0, 1, 2003, 2003}] 490 | ``` 491 | 492 | The first element of each tuple is the key, the rest are the same as above. 493 | 494 | There is `:tr.sorted_call_stat/2` as well. 495 | The second argument of this function is a [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:sorted_call_stat_options/0), including: 496 | 497 | - `sort_by` is the field to sort by: `:count`, `:acc_time` (default) or `:own_time`. 498 | - `order` is `:asc` or `:desc` (default). 499 | - `limit` is the maximum number of rows returned: a positive integer or `:infinity` (default). 500 | 501 | To pretty-print the statistics, use `:tr.print_sorted_call_stat/1`. 502 | There is `:tr.print_sorted_call_stat/2` as well with the same [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:sorted_call_stat_options/0) as `:tr.sorted_call_stat/2`. 503 | 504 | For example, we can only print the top 3 items: 505 | 506 | ```elixir 507 | iex(25)> :tr.print_sorted_call_stat(fn tr(data: [n]) when is_integer(n) -> n end, %{limit: 3}) 508 | 3 1 7981 1991 509 | 2 1 5990 1993 510 | 1 1 3997 1994 511 | :ok 512 | ``` 513 | 514 | ### Call tree statistics: `top_call_trees` 515 | 516 | The function `:tr.top_call_trees/0` makes it possible to detect complete call trees that repeat several times, 517 | where corresponding function calls and returns have the same arguments and return values, respectively. 518 | When such functions take a lot of time and do not have useful side effects, they can be often optimized. 519 | 520 | As an example, let's trace the call to a function which calculates the 4th element of the Fibonacci Sequence 521 | in a recursive way. The `trace` table should be empty, so let's clean it up first: 522 | 523 | ```elixir 524 | iex(26)> :tr.clean() 525 | :ok 526 | iex(27)> :tr.trace([{Example, :fib, 1}]) 527 | ok 528 | iex(28)> Example.fib(4) 529 | 3 530 | iex(29)> :tr.stop_tracing() 531 | :ok 532 | ``` 533 | 534 | Now it is possible to print the most time consuming call trees that repeat at least twice: 535 | 536 | ```elixir 537 | iex(30)> :tr.top_call_trees() 538 | [ 539 | {13, 2, 540 | {:node, ExDoctor.Example, :fib, [2], 541 | [ 542 | {:node, ExDoctor.Example, :fib, [1], [], {:return, 1}}, 543 | {:node, ExDoctor.Example, :fib, [0], [], {:return, 0}} 544 | ], {:return, 1}}}, 545 | {5, 3, {:node, ExDoctor.Example, :fib, [1], [], {:return, 1}}} 546 | ] 547 | ``` 548 | 549 | The resulting list contains tuples `{time, count, tree}` where `time` is the accumulated time (in microseconds) spent in the tree, 550 | and `count` is the number of times the tree repeated. The list is sorted by `time`, descending. 551 | In the example above `fib(2)` was called twice and `fib(1)` was called 3 times, 552 | what already shows that the recursive implementation is suboptimal. 553 | 554 | There is also `:tr.top_call_trees/1`, which takes a [map of options](https://hexdocs.pm/erlang_doctor/0.3.2/tr.html#t:top_call_trees_options/0), including: 555 | - `output` - `:reduced` by default, but it can be set to `:complete` where subtrees of already listed trees are also listed. 556 | - `min_count` - minimum number of times a tree has to occur to be listed, the default is 2. 557 | - `min_time` - minimum accumulated time for a tree, by default there is no minimum. 558 | - `max_size` - maximum number of trees presented, the default is 10. 559 | 560 | As an exercise, try calling `:tr.top_call_trees(%{min_count: 1000})` for `fib(20)`. 561 | 562 | ## Exporting and importing traces 563 | 564 | To get the current table name, use `:tr.tab/0`: 565 | 566 | ```elixir 567 | iex(31)> :tr.tab() 568 | :trace 569 | ``` 570 | 571 | To switch to a new table, use `:tr.set_tab/1`. The table need not exist. 572 | 573 | ```elixir 574 | iex(32)> :tr.set_tab(:tmp) 575 | :ok 576 | ``` 577 | 578 | Now you can collect traces to the new table without changing the original one. 579 | 580 | ```elixir 581 | iex(33)> :tr.trace([Enum]); Enum.to_list(1..10); :tr.stop_tracing() 582 | :ok 583 | iex(34)> :tr.select() 584 | [ 585 | (...) 586 | ] 587 | ``` 588 | 589 | You can dump the current table to file: 590 | 591 | ```elixir 592 | iex(35)> :tr.dump("tmp.ets") 593 | :ok 594 | ``` 595 | 596 | In a new `iex` session we can load the data with `:tr.load/1`. This will set the current table name to `:tmp`. 597 | 598 | ```elixir 599 | iex(1)> :tr.load("tmp.ets") 600 | {:ok, :tmp} 601 | iex(2)> :tr.select() 602 | [ 603 | (...) 604 | ] 605 | iex(3)> :tr.tab() 606 | :tmp 607 | ``` 608 | 609 | Finally, you can remove all traces from the ETS table with `:tr.clean/0`. 610 | 611 | ```elixir 612 | iex(4)> :tr.clean() 613 | :ok 614 | ``` 615 | 616 | To stop `ExDoctor`, just call `:tr.stop/0`. 617 | --------------------------------------------------------------------------------