-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsstack_linux.c
More file actions
213 lines (181 loc) · 6.18 KB
/
sstack_linux.c
File metadata and controls
213 lines (181 loc) · 6.18 KB
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
// This file is for Linux/BSD. Supports both, in theory, never tested for BSD tbf. As usual IntelliSense errors on Windows are expected since, once again, its not the fucking os it was supposed to be on.
#include "sstack_common.h"
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
typedef struct {
uint64_t addr;
char symbol[256];
} SymbolInfo;
typedef struct {
uint64_t start;
uint64_t end;
char path[512];
} MappedRegion;
static int get_symbol_from_maps(uint64_t addr, const char *maps_file, SymbolInfo *out) {
FILE *f = fopen(maps_file, "r");
if (!f) return 0;
char line[1024];
MappedRegion best = {0, 0, ""};
while (fgets(line, sizeof(line), f)) {
uint64_t start, end;
char perms[5], dev[10], offset[20], inode[20], path[512];
if (sscanf(line, "%lx-%lx %4s %s %s %s %511s",
&start, &end, perms, offset, dev, inode, path) >= 6) {
if (addr >= start && addr < end && end > best.end) {
best.start = start;
best.end = end;
strncpy(best.path, path, sizeof(best.path) - 1);
}
}
}
fclose(f);
if (best.end > 0) {
snprintf(out->symbol, sizeof(out->symbol), "%s+0x%llx",
best.path[0] ? best.path : "(unknown)",
(unsigned long long)(addr - best.start));
out->addr = addr;
return 1;
}
return 0;
}
static void read_proc_stack(uint64_t pid, int thread_id, FILE *out, int json_output) {
char path[256];
#ifdef __linux__
snprintf(path, sizeof(path), "/proc/%" PRIu64 "/task/%d/stack", pid, thread_id);
#else
return;
#endif
FILE *f = fopen(path, "r");
if (!f) {
if (json_output) {
fprintf(out, "[]");
}
return;
}
char line[512];
char maps_file[256];
snprintf(maps_file, sizeof(maps_file), "/proc/%" PRIu64 "/maps", pid);
int frame_num = 0;
if (json_output) fprintf(out, "\n ");
while (fgets(line, sizeof(line), f) && frame_num < 128) {
uint64_t addr = 0;
char *bracket_start = strchr(line, '[');
char *bracket_end = bracket_start ? strchr(bracket_start, ']') : NULL;
if (bracket_start && bracket_end) {
if (sscanf(bracket_start, "[<%llx>]", &addr) == 1 && addr > 0) {
SymbolInfo sym = {0, ""};
get_symbol_from_maps(addr, maps_file, &sym);
if (json_output) {
if (frame_num > 0) fprintf(out, ",");
fprintf(out, "\n {\"address\": \"0x%016llx\", \"symbol\": \"%s\"}",
(unsigned long long)addr, sym.symbol);
} else {
fprintf(out, " #%02d 0x%016llx %s\n", frame_num, (unsigned long long)addr, sym.symbol);
}
++frame_num;
}
}
}
fclose(f);
if (json_output && frame_num == 0) {
fprintf(out, "[]");
}
}
static void enumerate_threads_linux(uint64_t pid, FILE *out, int json_output) {
char task_dir[256];
snprintf(task_dir, sizeof(task_dir), "/proc/%" PRIu64 "/task", pid);
DIR *d = opendir(task_dir);
if (!d) {
if (json_output) {
fprintf(out, " ]\n}\n");
}
return;
}
struct dirent *entry;
int thread_count = 0;
int first = 1;
while ((entry = readdir(d)) != NULL) {
if (entry->d_type != DT_DIR) continue;
char *endptr;
long tid = strtol(entry->d_name, &endptr, 10);
if (*entry->d_name == '\0' || *endptr != '\0' || tid <= 0) continue;
if (json_output) {
if (!first) fprintf(out, ",");
fprintf(out, "\n {\n \"thread_id\": %ld,\n \"frames\": [", tid);
first = 0;
} else {
if (thread_count > 0) fprintf(out, "\n");
fprintf(out, "Thread %ld:\n", tid);
}
read_proc_stack(pid, (int)tid, out, json_output);
if (json_output) {
fprintf(out, "\n ]");
fprintf(out, "\n }");
}
++thread_count;
}
closedir(d);
if (json_output) {
fprintf(out, "\n ]\n}\n");
}
}
void platform_stack(uint64_t pid, int json_output) {
char name[128];
FILE *out;
if (json_output) {
out = stdout;
fprintf(out, "{\n");
fprintf(out, " \"pid\": %" PRIu64 ",\n", pid);
fprintf(out, " \"threads\": [");
} else {
out = open_dump_file(pid, name, sizeof(name));
if (!out) {
fprintf(stderr, "[ERROR] Cannot write dump file\n");
return;
}
fprintf(out, "Stack dump for PID %" PRIu64 "\n\n", pid);
char buffer[1024];
snprintf(buffer, sizeof(buffer), "/proc/%" PRIu64 "/status", pid);
FILE *status = fopen(buffer, "r");
if (status) {
fprintf(out, "=== /proc/%" PRIu64 "/status ===\n", pid);
while (fgets(buffer, sizeof(buffer), status)) {
if (strstr(buffer, "Pid:") || strstr(buffer, "PPid:") || strstr(buffer, "State:") ||
strstr(buffer, "Threads:") || strstr(buffer, "VmRSS:") || strstr(buffer, "VmSize:")) {
fputs(buffer, out);
}
}
fclose(status);
fprintf(out, "\n");
}
snprintf(buffer, sizeof(buffer), "/proc/%" PRIu64 "/maps", pid);
FILE *maps = fopen(buffer, "r");
if (maps) {
fprintf(out, "=== /proc/%" PRIu64 "/maps ===\n", pid);
while (fgets(buffer, sizeof(buffer), maps)) {
fputs(buffer, out);
}
fclose(maps);
fprintf(out, "\n");
}
fprintf(out, "=== Stack traces (from /proc) ===\n\n");
}
enumerate_threads_linux(pid, out, json_output);
if (json_output) {
fprintf(out, "\n ]\n}\n");
} else {
fclose(out);
printf("[*] Dump written to %s\n", name);
}
}