Skip to content

Commit 12e25df

Browse files
committed
Refactor ErlangDBG documentation and code structure
- Changed layout of BasicScenario.md and Cleanup.md to use 'erldbg' layout. - Removed TypeScript files for BasicScenario and Cleanup as they are no longer needed. - Added new markdown files for Cleanup, Conclusion, CustomHandler, CustomHandlerMessageFilter, CustomHandlerSignals, FindCaller, RegisteredProcessesTracking, Setup, Timestamps, and TraceFunction to enhance documentation. - Updated Svelte configuration to correctly map the 'erldbg' layout. - Improved examples and explanations in the new markdown files for better clarity and usability.
1 parent d0ed23a commit 12e25df

25 files changed

Lines changed: 378 additions & 430 deletions

src/lib/assets/mdlayouts/erldbg.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script lang="ts">
2-
import Erldbg from './erldbg.svelte';
32
export let children: () => any;
43
</script>
54

src/routes/posts/ErlangDBG/+page.svelte

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<script lang="ts">
2-
import { CustomHandler } from "./CustomHandler";
3-
import { Timestamps } from "./Timestamps";
4-
import { TraceFunction } from "./TraceFunction";
5-
import { BasicScenario } from "./BasicScenario";
6-
import { FindCaller } from "./FindCaller";
7-
import { Setup } from "./Setup";
8-
import { CustomHandlerMessageFilter } from "./CustomHandlerMessageFilter";
9-
import { CustomHandlerMessageSignals } from "./CustomHandlerSignals";
10-
import { RegisteredProcessesTracking } from "./RegisteredProcessesTracking";
11-
import { Cleanup } from "./Cleanup";
2+
import CustomHandler from "./CustomHandler.md";
3+
import Timestamps from "./Timestamps.md";
4+
import TraceFunction from "./TraceFunction.md";
5+
import FindCaller from "./FindCaller.md";
6+
import Setup from "./Setup.md";
7+
import CustomHandlerMessageFilter from "./CustomHandlerMessageFilter.md";
8+
import CustomHandlerMessageSignals from "./CustomHandlerSignals.md";
9+
import RegisteredProcessesTracking from "./RegisteredProcessesTracking.md";
10+
import Cleanup from "./Cleanup.md";
1211
import HeaderShort from "./HeaderShort.svelte";
1312
import Header from "./Header.svelte";
14-
import BS from "./BasicScenario.md";
15-
import { Conclusion } from "./Conclusion";
16-
13+
import BasicScenario from "./BasicScenario.md";
14+
import Conclusion from "./Conclusion.md";
15+
1716
</script>
1817

1918
<div class="container">
@@ -38,28 +37,30 @@
3837
<Header>Setup</Header>
3938

4039
<div class="glass">
41-
<BS></BS>
40+
<Setup> </Setup>
4241
</div>
4342

4443
</section>
45-
<!-- <section id="Basic Scenario">
44+
<section id="Basic Scenario">
4645
<Header>Basic Scenario</Header>
4746

48-
<div class="glass"><SvelteMarkdown source={BasicScenario} /></div>
47+
<div class="glass">
48+
<BasicScenario> </BasicScenario>
49+
</div>
4950
</section>
5051

5152
<section id="Trace Function">
5253
<Header>Trace Function</Header>
53-
<div class="glass"><SvelteMarkdown source={TraceFunction} /></div>
54+
<div class="glass"><TraceFunction /></div>
5455
</section>
5556

5657
<section id="Function Caller Finding">
5758
<Header>Function Caller Finding</Header>
58-
<div class="glass"><SvelteMarkdown source={FindCaller} /></div>
59+
<div class="glass"><FindCaller /></div>
5960
</section>
6061
<section id="Timestamps">
6162
<Header>Timestamps</Header>
62-
<div class="glass"><SvelteMarkdown source={Timestamps} /></div>
63+
<div class="glass"><Timestamps /></div>
6364
</section>
6465

6566
<div class="glass"></div>
@@ -69,35 +70,35 @@
6970

7071
<section id="Custom Handler: Introduction">
7172
<HeaderShort>Introduction</HeaderShort>
72-
<div class="glass"><SvelteMarkdown source={CustomHandler} /></div>
73+
<div class="glass"><CustomHandler /></div>
7374
</section>
7475
<section id="CustomHandlerMessageFilter">
7576
<HeaderShort>Message Filter</HeaderShort>
7677
<div class="glass">
77-
<SvelteMarkdown source={CustomHandlerMessageFilter} />
78+
<CustomHandlerMessageFilter />
7879
</div>
7980
</section>
8081
<section id="Custom Handler: Signals">
8182
<HeaderShort>Signals</HeaderShort>
8283
<div class="glass">
83-
<SvelteMarkdown source={CustomHandlerMessageSignals} />
84+
<CustomHandlerMessageSignals />
8485
</div>
8586
</section>
8687
<section id="Registered Process Tracking">
8788
<Header>Registered Process Tracking</Header>
8889
<div class="glass">
89-
<SvelteMarkdown source={RegisteredProcessesTracking} />
90+
<RegisteredProcessesTracking />
9091
</div>
9192
</section>
9293
<section id="Cleanup">
9394
<Header>Cleanup</Header>
94-
<div class="glass"><SvelteMarkdown source={Cleanup} /></div>
95+
<div class="glass"><Cleanup /></div>
9596
</section>
9697

9798
<section id="Conclusion">
9899
<Header>Conclusion</Header>
99-
<div class="glass"><SvelteMarkdown source={Conclusion} /></div>
100-
</section> -->
100+
<div class="glass"><Conclusion /></div>
101+
</section>
101102
</div>
102103

103104
<style>
@@ -106,7 +107,6 @@
106107
margin: 0;
107108
/* -webkit-backdrop-filter: blur(3px);
108109
backdrop-filter: blur(3px); */
109-
110110
overflow-x: clip;
111111
}
112112

src/routes/posts/ErlangDBG/BasicScenario.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
layout: blog
2+
layout: erldbg
33
---
44
Let us start with a basic scenario. I will omit some of the messages printed out by the shell for clarity.
55
```erlang

src/routes/posts/ErlangDBG/BasicScenario.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: erldbg
3+
---
4+
Let us purge the tracing from the system.
5+
First, clean up the process flags.
6+
```erlang
7+
> dbg:p(all,clear).
8+
```
9+
Remove function tracing.
10+
```erlang
11+
> dbg:ctp(\'_\').
12+
```
13+
Shutdown the debug tracer.
14+
```erlang
15+
> dbg:stop().
16+
```
17+

src/routes/posts/ErlangDBG/Cleanup.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: erldbg
3+
---
4+
First, try to check other sources of information, in most cases, the erlang dump file should be enough.
5+
6+
If it is possible to install some debug/test framework instead, for example:
7+
- https://massemanet.github.io/eper/redbug\n
8+
- https://ferd.github.io/recon/\n
9+
10+
If you are a consultant check your insurance.
11+
12+
Since the <bold>return_trace</bold> (and <bold>exception_trace</bold>) flag is potentially behavior-changing we'd better avoid it.
13+
14+
I have omitted <bold>seq_trace</bold> utilities, on purpose, I do not feel well when a single press of the tab key can hang your shell.
15+
16+
But if you are comfortable with sequence tracing on production nodes, then why have you read through this document in the first place?
17+
18+
If you still see need to use dbg on live node start from writing
19+
a processing function that has message counter as an accumulator and
20+
safeguards to <bold>dbg:stop/0</bold> after a specific number of printed messages.
21+
22+
It is good when the number of messages is safeguarded in the first header of the defined function (see [<bold>Fun25</bold>](#CustomHandlerMessageFilter)).
23+
24+
Alternatively just run <bold>dbg:stop/0</bold> after the first match if you don\'t need to catch more than one message.
25+
26+
You can mail me: flmathematic at gmail.com.
27+

src/routes/posts/ErlangDBG/Conclusion.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
layout: erldbg
3+
---
4+
The problem with all of the commands above is, ' +
5+
that all the messages are printed out to the shell. What ' +
6+
in case of tracing very active function ends up blocking the shell by spamming it with a continuous unreadable amount of printouts.
7+
8+
The classic example provided on https://massemanet.github.io/eper/redbug
9+
is:
10+
(Caution: it will hang the shell, run it in separate only to see how it looks like).
11+
```erlang
12+
> dbg:tracer(),dbg:p(all,[m]).
13+
{ok,[{matched,nonode@nohost,71}]}
14+
```
15+
To deal with that we need to customize the way we are reading our messages.
16+
Instead of the <bold>dbg:trace/0</bold> we use the <bold>dbg:trace/2</bold> which takes as first argument an atom. Possible values are:
17+
port - the trace messages are sent to port
18+
process - we handle trace messages with custom function
19+
module - we implement trace module
20+
21+
Since we consider here scenario with limited options ' +
22+
(otherwise it is just better to install one of the mentioned frameworks), we will use the process case. ' +
23+
In that scenario, the second argument of the <bold>dbg:trace/2</bold> has a form of a tuple: <bold>{Fun, InitData}</bold>. ' +
24+
The Fun is a function that takes two arguments. ' +
25+
The first is the received message and the second is the accumulator returned from the previous iteration. ' +
26+
The <bold>InitData</bold> is a starting value of the accumulator. ' +
27+
In other words, it works like <bold>lists:foldl/3</bold> with the difference that instead input list we process received trace messages.
28+
29+
Let us start with a Fun that will assure that dbg will stop after receiving exactly one message, ' +
30+
eliminating the possibility that we hang the shell by flooding it with a continuous stream of messages.
31+
32+
```erlang
33+
> Fun = fun(X, Acc) ->
34+
> io:format("Value X:~p Value Acc: ~p~n", [X, Acc]),
35+
> dbg:stop(),
36+
> [X|Acc]
37+
> end.
38+
#Fun<erl_eval.12.128620087>
39+
```
40+
41+
```erlang
42+
> dbg:ctp(\'_\').
43+
{ok,[]}
44+
```
45+
46+
```erlang
47+
> dbg:tracer(process,{Fun,[]}),
48+
> dbg:p(all,[c]),
49+
> dbg:tpl({tracing_experiments, traced_function, 2},[{\'_\', [], []}]).
50+
{ok,[{matched,nonode@nohost,1},{saved,1}]}
51+
```
52+
53+
```erlang
54+
> tracing_experiments:switch_state().
55+
tracing_experiments.erl:86:<0.134.0>: io:format called from state enter_heavy_state number 56
56+
Value X:{trace,<0.134.0>,call,
57+
{tracing_experiments,traced_function,[enter_heavy_state,56]}} Value Acc: []
58+
ok
59+
60+
> tracing_experiments.erl:86:<0.134.0>: io:format called from state keep_heavy_state number 57
61+
62+
> tracing_experiments.erl:86:<0.134.0>: io:format called from state keep_heavy_state number 58
63+
...
64+
```
65+
66+
```erlang
67+
> tracing_experiments:switch_state().
68+
ok
69+
```
70+
From hard-coded debug messages, you can see that <bold>traced_function/2</bold> is called continuously, ' +
71+
but only the traces for state 56 are printed out. So we have a working safe switch.
72+
Lets see what will happened if we go for classical example:
73+
```erlang
74+
> dbg:tracer(process,{Fun,[]}). {ok,<0.655.0>}
75+
> dbg:p(all,[m]).
76+
... long but finite output ...
77+
```
78+
The shell is operational.

0 commit comments

Comments
 (0)