In a parallel world, it's hard to do a single-step breakpoint debugging to locate the problem (too many messages flying around), and Erlang designers are also deeply aware of this and have introduced another trace mechanism. With this trace, you can:
1 . Specify the in-process function call input and output, send and receive messages; 2. Specify the input and output of the port and send and receive messages.
This way you can add no line of code (
abandon that kind of io:formatCumbersome and single method), do not affect the normal operation of the system to (in a server system, a single-step breakpoint is almost impossible), look at the control process you want to understand. Now the libraries used by Erlang for trace are:
•
SYSComes standard with OTP and allows to set custom tracing functions, log all kinds of events, and so on. It ' s generally complete and fine to use for development. It suffers a bit in production because it doesn ' t redirect IO to remote shells, and doesn ' t has rate-limiting capabilitie s for trace messages. It's still recommended to read the documentationfor the Module.
dbgAlso comes standard with ERLANG/OTP. Its interface are a bit clunky in terms of the usability, but it's entirely good enough to does what you need. The problem with it's so you had to know what's your ' re doing, because DBG can log absolutely everything on the node and Kill one in under Seconds.
Tracing
BIFsis available as part of the Erlang module. They ' re mostly the raw blocks used by all the applications mentioned in this list, but their lower level of abstraction Ma Kes them rather difficult to use.
Redbugis a production-safe tracing library, part of the Eper 5 suite. It has an internal rate-limiter, and a nice usable interface. To use it, you must however is willing to the add in all of Eper ' s dependencies. The toolkit is fairly comprehensive and can be a very interesting install.
Recon_traceIs Recon's take on tracing. The objective is to allow the same levels of safety as with Redbug, but without the dependencies. The interface is different, and the rate-limiting options aren ' t entirely identical. It can also only the trace function calls, and not messages.
•
SYSis a standard OTP that allows you to customize the trace function, record all types of events, and so on. It is very well developed and can be used in development. However, it will slightly affect the system in the production environment because it does not redirect IO to the remote shell, and he does not limit the speed of the trace message. However, it is recommended to read the documentation module. •
dbgis also a standard OTP. Its interface is a bit awkward in terms of usability. But it is perfectly enough to meet your needs. The problem is: you have to know what you're going to do, because DBG can record everything and crash the system within 2 seconds. •
tracing BIFsThe module is available as a erang. Most of them are called as primitive blocks (the raw blocks) by the application mentioned in this list, but because they are at the lower level, they are more abstract and difficult to use. •
Redbugis a trace library that can be used in a formal production run system and is part of the EPER, which has a speed limit switch and a nice available interface. In order to use it, you must add all the dependencies of the EPER. This toolkit is very comprehensive and you will experience a very interesting installation. •
Recon_traceIs the module that is responsible for trace in Recon. The goal is to have the same level of security as redbug, but not so many dependencies. Interfaces are not the same, and the speed limit options are not exactly the same. It can only trace the specified function call, there is no trace send/recv message (actually there is no need to support trace message This mechanism in the application using OTP) want to use the trace tool above, Basic concepts that must be mastered. We want to specify a target for trace:
1 . Specify the process set for trace; 2. Specify the specified entry for the function specified in the module.
The intersection of these two adds up is the collection of trace.
%%% _,--------,_ _,--------,_%%%,-'-,,-'-,%%%,-',--'-, '-,%%% | Matching-"-Matching |%%% | Pids | Getting | Trace |%%% | | traced | Patterns |%%% | -, ,- |%%% '-, '-,,-',-'%%% '-,_ _,-'-,_ _,-'%%% '--------'--------'%%%
The following two intersection rules are described in the most commonly used DBG library, and the other libraries are similar. 1. Start with the specified modules, functions, and parameters:
> Dbg:start (). % start dbg> Dbg:tracer (). % Start a simple tracer process> Dbg:tp (Module, Function, Arity, []). % Specify MFAyou is interested in > dbg:p (All, c). of this MFA for all processes.
.. trace here
> Dbg:stop_clear (). % Stop tracer and clear effect of TP and P calls.
You can use TP to trace multiple functions simultaneously, if you want the trace module to have no exported function, use TPL if you want to remove the trace using CTP or CTPL
> Dbg:tpl (Module, ' _ ', []). % all calls in module> Dbg:tpl (module, Function, ' _ ', []). % all calls to module:function with any arity. > Dbg:tpl (Module, Function, Arity, []). % all calls to module:function/Arity. > Dbg:tpl (M, F, A, [{' _ ', [], [{return_trace}]}]). % same as before, but also show return value.
You can use dbg:fun2ms to generate a matching pattern for function parameters.
1> dbg:fun2ms (fun whenend). [ {[' $ ', ' $ '],[{' > ', ' $3}],[{return_trace}]}]
2. You can use the DBG:P function to specify a specific process:
> Dbg:p (All, c). % trace calls to selected functionsby all functions > Dbg:p (New, c). % trace calls by processes spawnedfrom now on > dbg:p (Pid, c). % trace calls by given process> Dbg:p (Pid, [C, M]). and of a given process
DBG inside the functions are closed to the erlang:trace, erlang:trace too low, the interface is very difficult to use, so closed into a dbg library. Using DBGbe aware that: 1. Because trace generates a lot of logs, it is often necessary to put it into a file summary and then analyze it (instead of outputting it directly to the shell): Generate multiple files to save the log:
>dbg:tracer (Port,dbg:trace_port (file,{"/log/trace", Wrap,atom_to_list (Node ())}).
This also shows that dbg too indulgent users, accidentally generated a large number of logs, resulting in node anomalies. Use with caution in the production environment. 2. All tp,p will work after the dbg:start/0,dbg:trace/0 is turned on. As you can see from the above: DBG processes the filtering process, filtering {module,fun,arity} separately. Of course, this is more flexible, more precise control. But there is a group of people who feel that DBG is not perfect:
1. The interface is too complex to use the step complex: Start----"Custom trace-----" STOP; 2. No output log count control, improper use in a production environment can cause an exception: unsafe!
So we have the Redbug,recon_trace library described above. Here's a look at the Recon_trace library (his use is safe!) ):
1. The interface is simple to use, he only needs to call a function CALLS/2 calls/3 can trace; 2. Frequency limit and speed limit; 3. The output trace is more readable than the default trace.
He combines the filtering process set and the module function filter together! CALLS/2 equivalent to calls ({mods, fun, Args},max, []). CALLS/3 allows to set trace patterns and PID specifications to trace function calls.clear/0 Stops all tracing at once. The interface of this library is very simple, with a basic to get started, examples can be seen here: Reference 1.http://stackoverflow.com/questions/1954894/using-trace-and-dbg-in-erlang2.http://ferd.github.io/recon/ recon_trace.html when I show tracing in a production node to
[ERLANG_QUESTION31] Erlang Trace Summary