File 6161-dbg-Add-dbg-tracer-file-Filename.patch of Package erlang

From 43266f371fc43e5a4bface5ef2d5adc11b92a38d Mon Sep 17 00:00:00 2001
From: Lukas Larsson <lukas@erlang.org>
Date: Fri, 4 Feb 2022 09:32:52 +0100
Subject: [PATCH 1/2] dbg: Add dbg:tracer(file, Filename)

This function can be used to trace to a file in clear text
---
 lib/runtime_tools/doc/src/dbg.xml    |  5 ++++-
 lib/runtime_tools/src/dbg.erl        | 13 +++++++++++--
 lib/runtime_tools/test/dbg_SUITE.erl | 29 ++++++++++++++++++++++++++--
 3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/lib/runtime_tools/doc/src/dbg.xml b/lib/runtime_tools/doc/src/dbg.xml
index ba7641ffa9..5d9d7486d4 100644
--- a/lib/runtime_tools/doc/src/dbg.xml
+++ b/lib/runtime_tools/doc/src/dbg.xml
@@ -821,7 +821,7 @@ Error: fun containing local erlang function calls ('is_atomm' called in guard)\
       <name since="">tracer(Type, Data) -> {ok, pid()} | {error, Error}</name>
       <fsummary>Start a tracer server with additional parameters</fsummary>
       <type>
-        <v>Type = port | process | module</v>
+        <v>Type = port | process | module | file</v>
         <v>Data = PortGenerator | HandlerSpec | ModuleSpec</v>
         <v>PortGenerator = fun() (no arguments)</v>
         <v>Error = term()</v>
@@ -862,6 +862,9 @@ Error: fun containing local erlang function calls ('is_atomm' called in guard)\
             be either a tuple describing the <seeerl marker="erts:erl_tracer"><c>erl_tracer</c></seeerl>
           module to be used for tracing and the state to be used for
           that tracer module or a fun returning the same tuple.</p>
+        <p>if <c>Type</c> is <c>file</c>, then the second parameter
+          should be a filename specifying a file where all the traces
+          are printed.</p>
         <p>If an error is returned, it can either be due to a tracer
           server already running (<c>{error,already_started}</c>) or
           due to the <c>HandlerFun</c> throwing an exception.
diff --git a/lib/runtime_tools/src/dbg.erl b/lib/runtime_tools/src/dbg.erl
index d5be9ac6a9..e35ba820be 100644
--- a/lib/runtime_tools/src/dbg.erl
+++ b/lib/runtime_tools/src/dbg.erl
@@ -317,8 +317,17 @@ tracer(process, {Handler,HandlerData}) ->
 tracer(module, Fun) when is_function(Fun) ->
     start(Fun);
 tracer(module, {Module, State}) ->
-    start(fun() -> {Module, State} end).
-
+    start(fun() -> {Module, State} end);
+
+tracer(file, Filename) ->
+    tracer(process,
+           {fun F(E, undefined) ->
+                    {ok, D} = file:open(Filename, [write]),
+                    F(E, D);
+                F(E, D) ->
+                    dhandler(E, D),
+                    D
+            end, undefined}).
 
 remote_tracer(port, Fun) when is_function(Fun) ->
     remote_start(Fun);
diff --git a/lib/runtime_tools/test/dbg_SUITE.erl b/lib/runtime_tools/test/dbg_SUITE.erl
index 209af7be19..ce4e122107 100644
--- a/lib/runtime_tools/test/dbg_SUITE.erl
+++ b/lib/runtime_tools/test/dbg_SUITE.erl
@@ -23,7 +23,7 @@
 -export([all/0, suite/0, init_per_suite/1, end_per_suite/1,
          big/1, tiny/1, simple/1, message/1, distributed/1, port/1,
 	 send/1, recv/1,
-         ip_port/1, file_port/1, file_port2/1,
+         ip_port/1, file_port/1, file_port2/1, file_tracer/1,
          ip_port_busy/1, wrap_port/1, wrap_port_time/1,
          with_seq_trace/1, dead_suspend/1, local_trace/1,
          saved_patterns/1, tracer_exit_on_stop/1,
@@ -41,7 +41,7 @@ suite() ->
 all() -> 
     [big, tiny, simple, message, distributed, port, ip_port,
      send, recv,
-     file_port, file_port2, ip_port_busy,
+     file_port, file_port2, file_tracer, ip_port_busy,
      wrap_port, wrap_port_time, with_seq_trace, dead_suspend,
      local_trace, saved_patterns, tracer_exit_on_stop,
      erl_tracer, distributed_erl_tracer].
@@ -621,6 +621,31 @@ file_port2(Config) when is_list(Config) ->
     end,
     ok.
 
+%% Test tracing to file
+file_tracer(Config) when is_list(Config) ->
+    stop(),
+    FName = make_temp_name(Config),
+    %% Ok, lets try with flush and follow_file.
+    {ok, _} = dbg:tracer(file, FName),
+    try
+        {ok, [{matched, _node, 1}]} = dbg:p(self(),call),
+        {ok, _} = dbg:tp(dbg, ltp,[{'_',[],[{message, {self}}]}]),
+        {ok, _} = dbg:tp(dbg, ln, [{'_',[],[{message, hej}]}]),
+        ok = dbg:ltp(),
+        timer:sleep(100),
+        {ok, LTP} = file:read_file(FName),
+        <<"dbg:ltp()",_/binary>> = string:find(LTP, "dbg:ltp() ("++pid_to_list(self())++")"),
+        ok = dbg:ln(),
+        timer:sleep(100),
+        {ok, LN} = file:read_file(FName),
+        <<"dbg:ln()",_/binary>> = string:find(LN, "dbg:ln() (hej)"),
+        stop()
+    after
+        dbg:stop_clear(),
+        file:delete(FName)
+    end,
+    ok.
+
 %% Test tracing to wrapping file port
 wrap_port(Config) when is_list(Config) ->
     Self = self(),
-- 
2.35.3

openSUSE Build Service is sponsored by