FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
dmstack/dmstack/unwind/backtraces.cpp at main · DamengDB/dmstack · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DamengDB
/
dmstack
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
dmstack
/
dmstack
/
unwind
/
backtraces.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
266 lines (229 loc) · 6.81 KB
Breadcrumbs
dmstack
/
dmstack
/
unwind
/
backtraces.cpp
Copy path
File metadata and controls
266 lines (229 loc) · 6.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
259
260
261
262
263
264
265
266
#
include
"
unwind/backtraces.h
"
#
include
"
utils/signal_handler.h
"
#
include
"
utils/defer.h
"
#
include
"
log/log.h
"
#
include
<
dirent.h
>
#
include
<
sys/ptrace.h
>
#
include
<
sys/wait.h
>
#
include
<
sys/stat.h
>
#
include
<
libunwind.h
>
#
include
<
libunwind-ptrace.h
>
#
include
<
fstream
>
void
wait_info
(
int
tid,
int
task_idx,
int
total_task,
long
long
wait_count)
{
char
line[
128
];
//
避免打印过于频繁
if
(wait_count %
1000
==
0
)
{
char
bar[
21
];
int
fill;
int
i;
int
percent = (total_task >
0
) ? (
int
)((
long
long
)task_idx *
100
/ total_task) :
0
;
if
(percent <
0
)
percent =
0
;
if
(percent >
100
)
percent =
100
;
fill = percent *
20
/
100
;
for
(i =
0
; i <
20
; i++)
bar[i] = (i < fill) ?
'
#
'
:
'
-
'
;
bar[
20
] =
'
\0
'
;
snprintf
(line,
sizeof
(line),
"
T%-5d [%-20s] %3d%% %4d/%4d
"
,
tid, bar, percent, task_idx, total_task);
//
固定整行输出宽度,
fprintf
(stderr,
"
\r
%-80s
\r
"
, line);
fflush
(stderr);
}
}
BackTraces::BackTraces
(std::unordered_map<
int
, std::string>& tasks): error_code_(
0
)
{
int
rc =
0
;
int
task_idx =
0
;
int
total_task = tasks.
size
();
int
w_pid =
0
;
int
status =
0
;
int
count =
0
;
bts_.
reserve
(total_task);
do
{
unw_addr_space_t
addr_space =
unw_create_addr_space
(&_UPT_accessors,
0
);
if
(!addr_space)
{
rc = -
1
;
LOG
(
ERROR
,
"
unw_create_addr_space failed
"
);
break
;
}
DEFER
(
if
(addr_space)
unw_destroy_addr_space
(addr_space));
unw_set_caching_policy
(addr_space,
UNW_CACHE_GLOBAL
);
for
(
auto
&t: tasks)
{
//
init local bt
BackTrace
bt
(t.
first
);
//
reset wait info
rc =
0
;
w_pid =
0
;
status =
0
;
count =
0
;
task_idx++;
//
break the loop while interrupt
if
(utils::interrupt)
{
break
;
}
//
attach
rc =
ptrace
(
PTRACE_ATTACH
, t.
first
);
if
(-
1
== rc)
{
if
(errno !=
ESRCH
)
{
LOG
(
ERROR
,
"
ptrace attach failed, tid: %d, err: %d, errmsg: %s
"
,
t.
first
, errno,
strerror
(errno));
}
continue
;
}
DEFER
(
ptrace
(
PTRACE_DETACH
, t.
first
,
0
,
0
));
//
wait stop
while
(
1
)
{
w_pid =
wait4
(t.
first
, &status, __WALL |
WNOHANG
,
NULL
);
//
no status change
if
(
0
== w_pid)
{
usleep
(
20
);
wait_info
(t.
first
, task_idx, total_task, count++);
continue
;
}
if
(-
1
== w_pid)
{
//
Interrupted system call, should retry
if
(errno ==
EINTR
)
{
continue
;
}
rc = errno;
break
;
}
else
if
(
WIFEXITED
(status) ||
WIFSIGNALED
(status))
{
//
no such process
rc =
ESRCH
;
break
;
}
else
if
(
WIFSTOPPED
(status))
{
//
successfully stopped
rc =
0
;
break
;
}
}
if
(rc !=
0
)
{
LOG
(
ERROR
,
"
wait %d failed, err: %d, errmsg: %s
"
, t.
first
, rc,
strerror
(rc));
continue
;
}
//
unwind backtrace
struct
UPT_info
* upt_info = (
struct
UPT_info
*)
_UPT_create
(t.
first
);
if
(!upt_info)
{
LOG
(
WARN
,
"
unw_create_addr_space failed
"
);
rc = -
1
;
continue
;
}
DEFER
(
_UPT_destroy
(upt_info));
unw_cursor_t
c;
unw_init_remote
(&c, addr_space, upt_info);
do
{
unw_word_t
uip;
if
((rc =
unw_get_reg
(&c,
UNW_REG_IP
, &uip)) <
0
)
{
LOG
(
WARN
,
"
get reg failed, err: %d
\n
"
, rc);
break
;
}
bt.
addAddr
(
static_cast
<
int64_t
>(uip));
}
while
((rc =
unw_step
(&c)) >
0
);
if
(bt.
addrs
().
size
() >
0
)
{
bts_.
push_back
(bt);
}
}
if
(utils::interrupt) {
error_code_ = -
1
;
LOG
(
WARN
,
"
interruption occurs, will exit...
"
);
}
}
while
(
0
);
}
std::unordered_map<
int
, std::string>
ThreadInfo::getThreads
(
int
pid,
bool
thread_only)
{
std::unordered_map<
int
, std::string> thread_map;
std::string path =
"
/proc/
"
+
std::to_string
(pid) +
"
/task
"
;
struct
dirent
* entry;
struct
stat
entry_stat;
auto
getThreadName = [](
int
tid)
{
std::string path =
"
/proc/
"
+
std::to_string
(tid) +
"
/comm
"
;
std::ifstream
file
(path);
if
(!file.
is_open
())
return
std::string
(
"
Unknown
"
);
std::string name;
std::getline
(file, name);
file.
close
();
return
name;
};
if
(thread_only)
{
std::string name =
getThreadName
(pid);
thread_map.
emplace
(pid, name);
return
thread_map;
}
DIR
* dir =
opendir
(path.
c_str
());
if
(!dir)
{
LOG
(
ERROR
,
"
Failed to open directory: %s
"
, path.
c_str
());
return
thread_map;
}
//
遍历 /proc/{pid}/task/ 目录,获取所有线程 TID
while
((entry =
readdir
(dir)) !=
nullptr
)
{
std::string full_path = path +
"
/
"
+ entry->
d_name
;
if
(
stat
(full_path.
c_str
(), &entry_stat) ==
0
&&
S_ISDIR
(entry_stat.
st_mode
))
{
std::string dirName = entry->
d_name
;
if
(dirName !=
"
.
"
&& dirName !=
"
..
"
)
{
int
tid =
std::stoi
(dirName);
std::string name =
getThreadName
(tid);
thread_map.
emplace
(tid, name);
}
}
}
closedir
(dir);
return
thread_map;
}
bool
ThreadInfo::isPidStopped
()
{
FILE
* status_file;
char
buf[
100
];
bool
stopped =
false
;
snprintf
(buf,
sizeof
(buf),
"
/proc/%d/status
"
, (
int
)tid_);
status_file =
fopen
(buf,
"
r
"
);
if
(status_file !=
NULL
)
{
int
have_state =
0
;
while
(
fgets
(buf,
sizeof
(buf), status_file))
{
buf[
strlen
(buf) -
1
] =
'
\0
'
;
if
(
strncmp
(buf,
"
State:
"
,
6
) ==
0
)
{
have_state =
1
;
break
;
}
}
if
(have_state &&
strstr
(buf,
"
T
"
) !=
NULL
)
{
LOG
(
WARN
,
"
process %d %s
"
, tid_, buf);
stopped =
true
;
}
fclose
(status_file);
}
return
stopped;
}
Back
|
FazBrowse Home
|
New Git URL