You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/debugging.asciidoc
+27-23Lines changed: 27 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@
4
4
=== Introduction
5
5
This chapter goes into the various methods for finding and fixing bugs without disrupting the services in progress. We will explore testing techniques, tools, and frameworks that aid in testing and debugging your code. We'll also shed light on some common bug sources, such as deadlocks, message overflow, and memory issues, providing guidance on identifying and resolving these problems.
6
6
7
-
Debugging is the process of identifying and eliminating errors, or "bugs," from software. While Erlang offers step-by-step debugging tools like the link:http://erlang.org/doc/apps/debugger/debugger_chapter.html[_Debugger_], the most effective debugging methods often rely on Erlang's tracing facilities. These facilities will be thoroughly discussed in Chapter xref:CH-Tracing[]. In this chapter We will touch on system level tracing with dtrace and systemtap.
7
+
Debugging is the process of identifying and eliminating errors, or "bugs," from software. While Erlang offers step-by-step debugging tools like the link:http://erlang.org/doc/apps/debugger/debugger_chapter.html[_Debugger_], the most effective debugging methods often rely on Erlang's tracing facilities. These facilities will be thoroughly discussed in xref:CH-Tracing[]. In this chapter We will touch on system level tracing with DTrace and SystemTap.
8
8
9
9
This chapter also explores the concept of "Crash Dumps," which are human-readable text files generated by the Erlang Runtime System when an unrecoverable error occurs, such as running out of memory or reaching an emulator limit. Crash Dumps are invaluable for post-mortem analysis of Erlang nodes, and you will learn how to interpret and understand them.
10
10
11
11
In addition to these topics, this chapter will also discuss different testing methodologies, including EUnit and Common Test, which are crucial for ensuring the reliability and robustness of your code. The importance of mocking in testing will be examined, along with its best practices.
12
12
13
13
You will become acquainted with the "let it crash" principle and the ways to effectively implement it within your system. You'll gain insights into the workings of exceptions and supervisor tree design.
14
14
15
-
By the end of this chapter, you'll be equipped with the knowledge to systematically test your system and its individual components. You will be able to identify common mistakes and problems, and possibly even picking up some debugging philosophy along the way.
15
+
By the end of this chapter, you'll be equipped with the knowledge to systematically test your system and its individual components. You will be able to identify common mistakes and problems, and possibly even pick up some debugging philosophy along the way.
This allows you to track how values change throughout execution.
514
514
515
-
=== The next-genation debugger: EDB
515
+
=== The Next-Generation Debugger: EDB
516
516
517
-
The Erlang Debugger (EDB) is a modern, feature-rich debugger for Erlang applications. It provides a language server interface for setting breakpoints, inspecting variables, and stepping through code execution. See https://whatsapp.github.io/edb/
517
+
The Erlang Debugger (EDB) is a modern, feature-rich debugger for Erlang applications. It provides a language server interface for setting breakpoints, inspecting variables, and stepping through code execution. See https://github.com/WhatsApp/edb
518
518
519
-
In order to use EDB, you need to build Erlang from source with EDB support.
520
-
Future versions of Erlang OTP might be shipped with EDB support.
519
+
In order to use EDB prior to OTP 28, you need to build Erlang from source with EDB support.
521
520
Here is a guide on how to build Erlang from source with EDB support:
522
521
523
522
```bash
@@ -540,6 +539,7 @@ _build/default/bin/edb dap
540
539
This command launches EDB, allowing it to interface with your development environment through the DAP, providing a robust debugging experience.
541
540
542
541
Current State and Stability
542
+
543
543
At the time of writing, EDB is an early-stage, rapidly evolving tool. Its integration and usability, while promising, can still be challenging, particularly regarding IDE setup, node connections, and environment compatibility. Stability can vary significantly depending on OTP versions and developer tooling choices.
544
544
545
545
=== Crash Dumps in Erlang
@@ -599,18 +599,21 @@ This dump suggests that the system crashed due to a memory allocation failure (`
599
599
===== Key Sections in a Crash Dump
600
600
601
601
1. Slogan
602
+
602
603
Indicates the reason for the crash. Common slogans include:
603
604
- `eheap_alloc: Cannot allocate X bytes of memory` (Memory exhaustion)
604
-
- `Init terminating in do_boot ()` (Pobably an error in the boot script)
605
+
- `Init terminating in do_boot ()` (Probably an error in the boot script)
605
606
- `Could not start kernel pid` (Probably a bad argument in config)
606
607
607
608
2. System Information
609
+
608
610
Contains details about the runtime:
609
611
- `System version`: The Erlang/OTP version and build details
610
612
- `Compiled`: When the system was built
611
613
- `Taints`: Whether external native code (NIFs) are running
612
614
613
615
3. Memory Usage
616
+
614
617
Displays the memory distribution:
615
618
616
619
- `Total`: Total memory usage
@@ -620,16 +623,19 @@ This dump suggests that the system crashed due to a memory allocation failure (`
620
623
- `Code`: Loaded code memory footprint
621
624
622
625
4. Process List
626
+
623
627
Provides details about active processes, this section is crucial for identifying:
- Processes stuck in an infinite loop (`Reductions` count abnormally high)
627
631
- Message queue overload (`Messages` field growing indefinitely)
628
632
629
633
5. Ports and Drivers
634
+
630
635
This lists open ports and drivers, which can be useful if external system interactions (files, sockets, databases) are suspected as crash causes.
631
636
632
637
6. Loaded Modules
638
+
633
639
This helps determine if dynamically loaded code (e.g., via `code:load_file/1`) caused the crash.
634
640
635
641
@@ -704,14 +710,14 @@ help you inspect the state of the system at the point of the crash.
704
710
705
711
=== Debugging the Runtime System
706
712
707
-
Understanding and diagnosing issues within the Erlang runtime system (BEAM) can be challenging due to its complexity. However, utilizing tools like the GNU Debugger (GDB) can significantly aid in this process. This section provides an overview of using GDB to debug the BEAM, including setting up the environment and employing GDB macros to streamline the debugging workflow.
713
+
Understanding and diagnosing issues within the Erlang runtime system (ERTS) can be challenging due to its complexity. However, utilizing tools like the GNU Debugger (GDB) can significantly aid in this process. This section provides an overview of using GDB to debug the BEAM, including setting up the environment and employing GDB macros to streamline the debugging workflow.
708
714
709
715
[[Using-GDB]]
710
716
==== Using GDB
711
717
712
718
GDB is a powerful tool for debugging applications at the machine level, offering insights into the execution of compiled programs. When applied to the BEAM, GDB allows developers to inspect the state of the Erlang virtual machine during execution or after a crash.
713
719
714
-
To effectively use GDB with the BEAM, it's beneficial to compile the Erlang runtime system with debugging symbols. This compilation provides detailed information during debugging sessions.
720
+
To effectively use GDB with the BEAM, it's beneficial to compile the Erlang runtime system with debugging symbols. This compilation provides detailed information during debugging sessions.
715
721
716
722
See <<Alternative Beam emulator builds>> for instructions on compiling and running
717
723
a version of Erlang with debugging information.
@@ -743,10 +749,10 @@ bin/cerl -rcore <core file>
743
749
```
744
750
745
751
NOTE: If you are comfortable with using the Emacs editor, you can use
746
-
`cerl` with the flags `-gdb` and `-core` (no leading `r`), which launch an
752
+
`cerl` with the flags `-gdb` and `-core` (no leading `r`), which launches an
747
753
Emacs instance to work as an IDE for the debugging session. (By setting
748
754
`EMACS=emacsclient` first, you can even make it run in an existing Emacs if
Replace `<process_pointer>` with the actual pointer to the process control block (PCB) you're interested in. These macros simplify the process of extracting meaningful data from the BEAM's internal structures.
795
801
796
-
For a comprehensive guide on debugging the BEAM using GDB and employing these macros, refer to link:https://max-au.com/2022/03/29/debugging-the-beam/[Debugging the BEAM] and link:https://www.erlang.org/doc/system/debugging.html#debug-emulator[Debug emulator documentation]. These resources provide in-depth instructions and examples to assist you in effectively diagnosing and resolving issues within the Erlang runtime system.
802
+
For a comprehensive guide on debugging the BEAM using GDB and employing these macros, refer to link:https://max-au.com/2022/03/29/debugging-the-beam/[Debugging the BEAM] and link:https://www.erlang.org/doc/system/debugging.html#debug-emulator[Debug emulator] documentation. These resources provide in-depth instructions and examples to assist you in effectively diagnosing and resolving issues within the Erlang runtime system.
797
803
798
804
799
805
==== SystemTap and DTrace
@@ -807,7 +813,7 @@ Using these tools with Erlang can provide deep insights into the behavior of the
807
813
SystemTap and DTrace operate by inserting dynamically generated probes into running kernel and user-space applications. These probes capture real-time data, allowing developers to inspect and analyze program execution without stopping or modifying the application.
808
814
809
815
- **SystemTap**: Developed for Linux, SystemTap enables monitoring of kernel events, user-space programs, and runtime behavior using scripting. It is commonly used for profiling, fault detection, and system introspection.
810
-
816
+
811
817
- **DTrace**: Originally developed by Sun Microsystems for Solaris, DTrace provides similar tracing capabilities with a robust scripting language. It is widely used on macOS, FreeBSD, and SmartOS.
812
818
813
819
Both tools allow developers to measure function execution times, trace system calls, inspect memory usage, and capture event-based data critical for optimizing performance and debugging complex applications.
@@ -821,7 +827,7 @@ To use SystemTap and DTrace with Erlang, you need to enable the necessary tracin
821
827
SystemTap scripts rely on user-space markers embedded in the BEAM emulator. These markers allow SystemTap to hook into various internal events. To use SystemTap with Erlang:
822
828
823
829
- **Ensure SystemTap is installed** (on Linux distributions such as Ubuntu, Fedora, or CentOS):
824
-
830
+
825
831
```sh
826
832
sudo apt-get install systemtap systemtap-sdt-dev
827
833
```
@@ -865,7 +871,7 @@ This allows developers to observe function calls, detect bottlenecks, and debug
865
871
866
872
DTrace integrates directly with the BEAM runtime, offering deep visibility into system operations. It allows tracing function calls, memory allocation, garbage collection, and inter-process communication.
867
873
868
-
Dtrace works best on Solaris. There is a Linux version bundled with systemtap, but it is not as powerful as the Solaris version.
874
+
Dtrace works best on Solaris. There is a Linux version bundled with SystemTap, but it is not as powerful as the Solaris version.
869
875
870
876
On macOS, DTrace is pre-installed. On Ubuntu, it can be installed via:
871
877
@@ -897,5 +903,3 @@ sudo dtrace -s my_script.d
897
903
```
898
904
899
905
This provides a non-intrusive way to monitor the internal behavior of the BEAM virtual machine in real-time.
0 commit comments