Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2009 Red Hat, Inc.
4 : :
5 : : #include <config.h>
6 : :
7 : : #include <stdio.h> // for stderr
8 : :
9 : : #include <algorithm> // for min
10 : : #include <string>
11 : :
12 : : #include <glib-object.h>
13 : : #include <glib.h>
14 : :
15 : : #include <js/Printer.h>
16 : : #include <js/TypeDecls.h>
17 : : #include <js/Utility.h> // for UniqueChars
18 : : #include <js/friend/DumpFunctions.h>
19 : :
20 : : #include "gjs/auto.h"
21 : : #include "gjs/context-private.h"
22 : : #include "gjs/context.h"
23 : :
24 : 0 : void gjs_context_print_stack_stderr(GjsContext* self) {
25 : 0 : auto* cx = static_cast<JSContext*>(gjs_context_get_native_context(self));
26 : :
27 : 0 : g_printerr("== Stack trace for context %p ==\n", self);
28 : 0 : js::DumpBacktrace(cx, stderr);
29 : 0 : }
30 : :
31 : 0 : void gjs_dumpstack() {
32 : 0 : Gjs::SmartPointer<GList> contexts{gjs_context_get_all()};
33 : :
34 [ # # ]: 0 : for (GList* iter = contexts; iter; iter = iter->next) {
35 : 0 : Gjs::AutoUnref<GjsContext> gjs_context{GJS_CONTEXT(iter->data)};
36 : 0 : gjs_context_print_stack_stderr(gjs_context);
37 : 0 : }
38 : 0 : }
39 : :
40 : 46 : std::string gjs_dumpstack_string() {
41 : 46 : Gjs::SmartPointer<GList> contexts{gjs_context_get_all()};
42 : 46 : js::Sprinter printer;
43 [ - + ]: 46 : if (!printer.init())
44 : 0 : return "No stack trace: out of memory";
45 : :
46 [ + + ]: 91 : for (GList* iter = contexts; iter; iter = iter->next) {
47 : 45 : Gjs::AutoUnref<GjsContext> gjs_context{GJS_CONTEXT(iter->data)};
48 : : auto* cx = static_cast<JSContext*>(
49 : 45 : gjs_context_get_native_context(gjs_context));
50 : :
51 : 45 : printer.printf("== Stack trace for context 0x%p ==\n",
52 : : gjs_context.get());
53 : 45 : js::DumpBacktrace(cx, printer);
54 : 45 : printer.putChar('\n');
55 : 45 : }
56 : :
57 : 46 : size_t len = printer.length();
58 : 46 : JS::UniqueChars all_traces = printer.release();
59 [ - + ]: 46 : if (!all_traces)
60 : 0 : return "No stack trace: out of memory";
61 : :
62 : : // COMPAT: 2zu in C++23
63 : 138 : return {all_traces.get(), len - std::min(len, size_t{2})};
64 : 46 : }
|