Repository navigation
Expand file tree
/
Copy pathcmd_thread.cpp
More file actions
225 lines (204 loc) · 8.03 KB
/
Copy pathcmd_thread.cpp
File metadata and controls
225 lines (204 loc) · 8.03 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
214
215
216
217
218
219
220
221
222
223
224
225
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/debugger/cmd/cmd_thread.h"
#include "hphp/runtime/base/execution-context.h"
#include "hphp/runtime/base/string-util.h"
#include "hphp/util/process.h"
namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debugger);
void CmdThread::sendImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::sendImpl(thrift);
thrift.write(m_out);
thrift.write(m_threads);
}
void CmdThread::recvImpl(DebuggerThriftBuffer &thrift) {
DebuggerCommand::recvImpl(thrift);
thrift.read(m_out);
thrift.read(m_threads);
}
void CmdThread::list(DebuggerClient &client) {
if (client.argCount() == 0) {
static const char *keywords[] =
{ "list", "normal", "sticky", "exclusive", nullptr };
client.addCompletion(keywords);
}
}
void CmdThread::help(DebuggerClient &client) {
client.helpTitle("Thread Command");
client.helpCmds(
"[t]hread", "displays current thread's information",
"[t]hread [l]ist", "lists all threads at break",
"[t]hread {index}", "switches to the specified thread",
"[t]hread [n]ormal", "breaks all threads",
"[t]hread [s]ticky", "only send command to current thread",
"[t]hread [e]xclusive", "only break current thread",
nullptr
);
client.helpBody(
"Use '[t]hread' alone to display information of current thread.\n"
"\n"
"When a thread is at break, you may specify how other threads should "
"behave if they also happen to hit some breakpoints. Normally, other "
"threads will also break, and they will interrupt debugger session "
"with their breakpoints. So breaks from different threads may interleave. "
"If '[t]hread [s]ticky' is specified, all other threads will wait until "
"current thread is finished. This will help debugging to focus on just "
"one thread without losing breaks from other threads. If there is no need "
"to hold up any other threads, use '[t]hread [e]xclusive'. Then other "
"threads will not break at all. This mode is useful for live debugging "
"a production server, without interrupting many threads at a time. Use "
"'[t]hread [n]ormal' to change thread mode back to normal.\n"
"\n"
"Some debugging commands will automatically turn thread mode to sticky. "
"These include continue, step, next or out commands with a counter of "
"more than 1. These commands imply non-interruption from another thread. "
"The mode will remain even after these commands until '[t]hread [n]ormal' "
"is issued."
"\n"
"When multple threads hit breakpoints at the same time, use '[t]hread "
"[l]ist' command to display their indices, which can be used to switch "
"between them with '[t]hread {index}'."
);
}
void CmdThread::processList(DebuggerClient &client, bool output /* = true */) {
m_body = "list";
CmdThreadPtr res = client.xend<CmdThread>(this);
client.updateThreads(res->m_threads);
if (!output) return;
for (int i = 0; i < (int)res->m_threads.size(); i++) {
DThreadInfoPtr thread = res->m_threads[i];
const char *flag = " ";
if (thread->m_id == client.getCurrentThreadId()) {
flag = "*";
}
client.print("%4d %s %s (%" PRId64 ") %s\n %s", thread->m_index,
flag, thread->m_type.c_str(), thread->m_id,
thread->m_url.c_str(), thread->m_desc.c_str());
}
}
void CmdThread::onClient(DebuggerClient &client) {
if (DebuggerCommand::displayedHelp(client)) return;
if (client.argCount() > 1) {
help(client);
return;
}
if (client.argCount() == 0) {
m_body = "info";
CmdThreadPtr res = client.xend<CmdThread>(this);
client.print(res->m_out);
} else if (client.arg(1, "list")) {
processList(client);
} else if (client.arg(1, "normal")) {
m_body = "normal";
client.sendToServer(this);
client.info("Thread is running in normal mode now. Other threads will "
"interleave when they hit breakpoints as well.");
} else if (client.arg(1, "sticky")) {
m_body = "sticky";
client.sendToServer(this);
client.info("Thread is running in sticky mode now. All other threads "
"will wait until this thread finishes, when they hit "
"breakpoints.");
} else if (client.arg(1, "exclusive")) {
m_body = "exclusive";
client.sendToServer(this);
client.info("Thread is running in exclusive mode now. All other threads "
"will not break, even when they hit breakpoints.");
} else {
string snum = client.argValue(1);
if (!DebuggerClient::IsValidNumber(snum)) {
client.error("'[t]hread {index}' needs a numeric argument.");
client.tutorial(
"You will have to run '[t]hread [l]ist' first to see a list of valid "
"numbers or indices to specify. Thread 1 is always your current "
"thread. If that's the only thread on the list, you do not have "
"another thread at break to switch to."
);
return;
}
int num = atoi(snum.c_str());
DThreadInfoPtr thread = client.getThread(num);
if (!thread) {
processList(client, false);
thread = client.getThread(num);
if (!thread) {
client.error("\"%s\" is not a valid thread index. Choose one from "
"this list:", snum.c_str());
processList(client);
return;
}
}
if (thread->m_id == client.getCurrentThreadId()) {
client.info("This is your current thread already.");
return;
}
m_body = "switch";
m_threads.push_back(thread);
client.sendToServer(this);
throw DebuggerConsoleExitException();
}
}
void CmdThread::debuggerInfo(InfoVec &info) {
Add(info, "Host", Process::GetHostName());
Add(info, "Binary", Process::GetAppName());
Add(info, "Version", Process::GetAppVersion());
Add(info, "Process ID", FormatNumber("%lld", Process::GetProcessId()));
Add(info, "Thread ID", FormatNumber("0x%llx", (int64_t)Process::GetThreadId()));
}
bool CmdThread::onServer(DebuggerProxy &proxy) {
if (m_body == "info") {
// collect info
InfoVec info;
debuggerInfo(info);
Transport *transport = g_context->getTransport();
if (transport) {
transport->debuggerInfo(info);
} else {
Add(info, "Thread Type", proxy.getThreadType());
}
g_context->debuggerInfo(info);
m_out = DebuggerClient::FormatInfoVec(info);
return proxy.sendToClient(this);
}
if (m_body == "list") {
proxy.getThreads(m_threads);
return proxy.sendToClient(this);
}
if (m_body == "switch") {
if (!m_threads.empty()) {
proxy.switchThread(m_threads[0]);
m_exitInterrupt = true;
return true;
}
}
if (m_body == "normal") {
proxy.switchThreadMode(DebuggerProxy::Normal);
return true;
}
if (m_body == "sticky") {
proxy.switchThreadMode(DebuggerProxy::Sticky);
return true;
}
if (m_body == "exclusive") {
proxy.switchThreadMode(DebuggerProxy::Exclusive);
return true;
}
assert(false);
return false;
}
///////////////////////////////////////////////////////////////////////////////
}}