File 2392-erts-Refactor-out-erts_build_stacktrace.patch of Package erlang

From df1d3a28e90e38c7111c6c98c5e9b68b119c7a6b Mon Sep 17 00:00:00 2001
From: Sverker Eriksson <sverker@erlang.org>
Date: Mon, 28 Aug 2023 20:08:51 +0200
Subject: [PATCH 2/3] erts: Refactor out erts_build_stacktrace()

---
 erts/emulator/beam/erl_bif_info.c | 28 ++++++++++++++++++++--------
 erts/emulator/beam/erl_gc.c       |  5 ++++-
 erts/emulator/beam/erl_process.h  |  4 ++--
 3 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/erts/emulator/beam/erl_bif_info.c b/erts/emulator/beam/erl_bif_info.c
index 2265578f37..72130e624a 100644
--- a/erts/emulator/beam/erl_bif_info.c
+++ b/erts/emulator/beam/erl_bif_info.c
@@ -164,6 +164,10 @@ static Eterm
 current_function(Process* p, ErtsHeapFactory *hfact, Process* rp,
                  int full_info, Uint reserve_size, int flags);
 
+static Eterm
+current_stacktrace(Process *p, ErtsHeapFactory *hfact, Process* rp,
+                   Uint reserve_size, int flags);
+
 Eterm
 erts_bld_bin_list(Uint **hpp, Uint *szp, ErlOffHeap* oh, Eterm tail)
 {
@@ -1503,7 +1507,7 @@ process_info_aux(Process *c_p,
 	break;
 
     case ERTS_PI_IX_CURRENT_STACKTRACE:
-	res = erts_current_stacktrace(c_p, hfact, rp, reserve_size, flags);
+	res = current_stacktrace(c_p, hfact, rp, reserve_size, flags);
 	break;
 
     case ERTS_PI_IX_INITIAL_CALL:
@@ -2211,9 +2215,20 @@ current_function(Process *c_p, ErtsHeapFactory *hfact, Process* rp,
     return res;
 }
 
-Eterm
-erts_current_stacktrace(Process *p, ErtsHeapFactory *hfact, Process* rp,
+static Eterm
+current_stacktrace(Process *p, ErtsHeapFactory *hfact, Process* rp,
                    Uint reserve_size, int flags)
+{
+    const int include_i = (p != rp || (flags & ERTS_PI_FLAG_REQUEST_FOR_OTHER));
+    /* We skip current pc when requesting our own stack trace since it will
+     * inevitably point to process_info/1,2 */
+    return erts_build_stacktrace(hfact, rp, reserve_size,
+                                 erts_backtrace_depth, include_i);
+}
+
+Eterm
+erts_build_stacktrace(ErtsHeapFactory* hfact, Process* rp,
+                      Uint reserve_size, int max_depth, int include_i)
 {
     Uint sz;
     struct StackTrace* s;
@@ -2226,16 +2241,13 @@ erts_current_stacktrace(Process *p, ErtsHeapFactory *hfact, Process* rp,
     Eterm mfa;
     Eterm res = NIL;
 
-    depth = erts_backtrace_depth;
+    depth = max_depth;
     sz = offsetof(struct StackTrace, trace) + sizeof(ErtsCodePtr) * depth;
     s = (struct StackTrace *) erts_alloc(ERTS_ALC_T_TMP, sz);
     s->depth = 0;
     s->pc = NULL;
 
-    /* We skip current pc when requesting our own stack trace since it will
-     * inevitably point to process_info/1,2 */
-    if ((p != rp || (flags & ERTS_PI_FLAG_REQUEST_FOR_OTHER)) &&
-        depth > 0 && rp->i) {
+    if (include_i && depth > 0 && rp->i) {
         s->trace[s->depth++] = rp->i;
         depth--;
     }
diff --git a/erts/emulator/beam/erl_gc.c b/erts/emulator/beam/erl_gc.c
index 578143e685..7ae51b6843 100644
--- a/erts/emulator/beam/erl_gc.c
+++ b/erts/emulator/beam/erl_gc.c
@@ -3743,7 +3743,10 @@ reached_max_heap_size(Process *p, Uint total_heap_size,
             /* Build the args in reverse order */
             o_hp = hp = erts_alloc(ERTS_ALC_T_TMP, 2*(alive ? 9 : 8) * sizeof(Eterm));
             erts_factory_proc_init(&hfact, p);
-            args = CONS(hp, erts_current_stacktrace(p, &hfact, p, 0 ,0), args); hp += 2;
+            args = CONS(hp, erts_build_stacktrace(&hfact, p, 0,
+                                                  erts_backtrace_depth, 1),
+                        args);
+            hp += 2;
             erts_factory_close(&hfact);
             args = CONS(hp, msg, args); hp += 2;
             args = CONS(hp, make_small((p)->sig_inq.len), args); hp += 2;
diff --git a/erts/emulator/beam/erl_process.h b/erts/emulator/beam/erl_process.h
index f2bc7ad94f..ef0a57f3b4 100644
--- a/erts/emulator/beam/erl_process.h
+++ b/erts/emulator/beam/erl_process.h
@@ -2942,5 +2942,5 @@ extern erts_atomic32_t erts_halt_progress;
 extern int erts_halt_code;
 
 extern Eterm
-erts_current_stacktrace(Process* p, ErtsHeapFactory *hfact, Process* rp,
-                        Uint reserve_size, int flags);
+erts_build_stacktrace(ErtsHeapFactory *hfact, Process* rp,
+                      Uint reserve_size, int max_depth, int include_i);
-- 
2.35.3

openSUSE Build Service is sponsored by