File size: 7,820 Bytes
9e075cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<script>
  import { onMount } from "svelte";

  let fileInput;
  let backtraceData = "";
  let threads = [];
  let filteredThreads = [];
  let filterText = "";
  let visibleThreads = new Set();

  // Parse the backtrace file format
  function parseBacktraceFile(content) {
    const lines = content.split("\n");
    const parsedThreads = [];
    let currentThread = null;
    let currentBacktrace = [];

    for (let i = 0; i < lines.length; i++) {
      const line = lines[i];

      // Simpler approach: look for lines containing "Thread_" with numbers at the start
      if (line.includes("Thread_") && /^\s*\d+\s+Thread_\d+/.test(line)) {
        // Save previous thread if exists
        if (currentThread) {
          currentThread.backtrace = currentBacktrace;
          currentThread.collapsibleBacktrace =
            createCollapsibleBacktrace(currentBacktrace);
          parsedThreads.push(currentThread);
        }

        // Extract sample count and thread ID
        const sampleMatch = line.match(/^\s*(\d+)\s+(Thread_\d+)/);
        const sampleCount = sampleMatch[1];
        const threadId = sampleMatch[2];

        // Extract thread name (everything after the first colon, before parentheses)
        let threadName = "";
        const colonIndex = line.indexOf(":");
        if (colonIndex !== -1) {
          const afterColon = line.substring(colonIndex + 1);
          const parenIndex = afterColon.indexOf("(");
          threadName = (
            parenIndex !== -1 ? afterColon.substring(0, parenIndex) : afterColon
          ).trim();
        }

        currentThread = {
          id: threadId,
          name: threadName,
          fullName: threadName || threadId,
          sampleCount: parseInt(sampleCount),
          rawHeader: line.trim(),
          backtrace: [],
          expanded: false,
          collapsibleBacktrace: null, // Will be created after backtrace is complete
        };
        currentBacktrace = [];
      } else if (currentThread && line.trim()) {
        // This is part of the backtrace
        currentBacktrace.push(line);
      }
      // Skip empty lines
    }

    // Don't forget the last thread
    if (currentThread) {
      currentThread.backtrace = currentBacktrace;
      currentThread.collapsibleBacktrace =
        createCollapsibleBacktrace(currentBacktrace);
      parsedThreads.push(currentThread);
    }

    return parsedThreads;
  }

  // Toggle thread visibility
  function toggleThread(threadId) {
    console.log("πŸ”„ Toggling thread:", threadId);
    // Create a new array with updated thread to trigger Svelte reactivity
    threads = threads.map((thread) => {
      if (thread.id === threadId) {
        const newThread = { ...thread, expanded: !thread.expanded };
        console.log(
          "πŸ“Š Thread expanded:",
          newThread.expanded,
          "backtrace lines:",
          newThread.backtrace.length
        );
        return newThread;
      }
      return thread;
    });
  }

  // Handle file upload
  function handleFileUpload(event) {
    const file = event.target.files[0];
    if (file) {
      const reader = new FileReader();
      reader.onload = (e) => {
        backtraceData = e.target.result;
        threads = parseBacktraceFile(backtraceData);
      };
      reader.readAsText(file);
    }
  }

  // Load sample file on mount
  onMount(async () => {
    try {
      const response = await fetch("/sample.txt");
      if (response.ok) {
        backtraceData = await response.text();
        threads = parseBacktraceFile(backtraceData);
        console.log("βœ… Loaded", threads.length, "threads successfully");
      }
    } catch (error) {
      console.error("Could not load sample.txt:", error);
    }
  });

  // Parse individual backtrace lines for better display
  function parseBacktraceLine(line) {
    // Extract indentation
    const indentMatch = line.match(/^(\s*[\+\!\:\|]*\s*)/);
    const indent = indentMatch ? indentMatch[1] : "";
    const content = line.substring(indent.length);

    // Try to extract sample count, function name, and address
    const parts = {
      indent,
      content,
      original: line,
    };

    // Extract sample count if at beginning
    const sampleMatch = content.match(/^(\d+)\s+(.+)/);
    if (sampleMatch) {
      parts.sampleCount = sampleMatch[1];
      parts.content = sampleMatch[2];
    }

    return parts;
  }

  // Create collapsible backtrace structure
  function createCollapsibleBacktrace(backtrace) {
    return backtrace.map((line, index) => ({
      id: index,
      line,
      parsed: parseBacktraceLine(line),
      collapsed: false,
    }));
  }

  // Toggle backtrace line collapse
  function toggleBacktraceLine(threadId, lineIndex) {
    threads = threads.map((thread) => {
      if (thread.id === threadId && thread.collapsibleBacktrace) {
        const updatedBacktrace = [...thread.collapsibleBacktrace];
        updatedBacktrace[lineIndex] = {
          ...updatedBacktrace[lineIndex],
          collapsed: !updatedBacktrace[lineIndex].collapsed,
        };
        return { ...thread, collapsibleBacktrace: updatedBacktrace };
      }
      return thread;
    });
  }

  // Reactive updates - run filter when threads OR filterText changes
  $: filteredThreads = threads.filter(
    (thread) =>
      !filterText.trim() ||
      thread.fullName.toLowerCase().startsWith(filterText.toLowerCase())
  );
</script>

<div class="header">
  <h1>πŸ” Backtrace Viewer</h1>
  <div class="controls">
    <label for="fileInput" class="file-label"> πŸ“ Load Backtrace File </label>
    <input
      type="file"
      id="fileInput"
      class="file-input"
      accept=".txt"
      on:change={handleFileUpload}
      bind:this={fileInput}
    />

    <input
      type="text"
      class="filter-input"
      placeholder="Filter threads by name prefix..."
      bind:value={filterText}
    />

    <div class="stats">
      {filteredThreads.length} / {threads.length} threads
    </div>
  </div>
</div>

{#if threads.length === 0}
  <div class="no-threads">
    <p>No backtrace data loaded. Upload a file to get started.</p>
  </div>
{:else}
  {#each filteredThreads as thread (thread.id)}
    <div class="thread-container">
      <div
        class="thread-header"
        on:click={() => toggleThread(thread.id)}
        on:keydown={(e) => e.key === "Enter" && toggleThread(thread.id)}
        role="button"
        tabindex="0"
      >
        <div class="thread-title">
          <strong>{thread.sampleCount}</strong>
          {thread.id}
          {#if thread.name}
            : <span style="color: #3498db;">{thread.name}</span>
          {/if}
        </div>
        <div class="toggle-indicator" class:expanded={thread.expanded}>β–Ά</div>
      </div>

      {#if thread.expanded}
        <div class="thread-content">
          <div class="backtrace">
            {#each thread.collapsibleBacktrace as backtraceLine (backtraceLine.id)}
              <div
                class="backtrace-line"
                class:collapsed={backtraceLine.collapsed}
                on:click={() =>
                  toggleBacktraceLine(thread.id, backtraceLine.id)}
                on:keydown={(e) =>
                  e.key === "Enter" &&
                  toggleBacktraceLine(thread.id, backtraceLine.id)}
                role="button"
                tabindex="0"
              >
                <span class="indent">{backtraceLine.parsed.indent}</span
                >{#if backtraceLine.parsed.sampleCount}<span
                    class="sample-count"
                    >{backtraceLine.parsed.sampleCount}</span
                  >
                {/if}<span class="content">{backtraceLine.parsed.content}</span>
              </div>
            {/each}
          </div>
        </div>
      {/if}
    </div>
  {/each}
{/if}