2015-03-19 23:21:08 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
#if !ADB_HOST
|
2015-03-19 23:21:08 +01:00
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG JDWP
|
2015-03-19 23:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "sysdeps.h"
|
2015-03-19 23:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2015-07-24 02:12:58 +02:00
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2016-05-14 00:28:34 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2010-02-15 12:20:44 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-03-19 23:21:08 +01:00
|
|
|
#include "adb.h"
|
2016-05-14 00:28:34 +02:00
|
|
|
#include "adb_io.h"
|
2016-08-31 00:39:25 +02:00
|
|
|
#include "adb_unique_fd.h"
|
2015-10-07 00:10:05 +02:00
|
|
|
#include "adb_utils.h"
|
2015-03-19 23:21:08 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* here's how these things work.
|
|
|
|
|
|
|
|
when adbd starts, it creates a unix server socket
|
2016-05-14 00:28:34 +02:00
|
|
|
named @jdwp-control (@ is a shortcut for "first byte is zero"
|
2009-03-04 04:32:55 +01:00
|
|
|
to use the private namespace instead of the file system)
|
|
|
|
|
|
|
|
when a new JDWP daemon thread starts in a new VM process, it creates
|
2016-05-14 00:28:34 +02:00
|
|
|
a connection to @jdwp-control to announce its availability.
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
JDWP thread @jdwp-control
|
2009-03-04 04:32:55 +01:00
|
|
|
| |
|
|
|
|
|-------------------------------> |
|
|
|
|
| hello I'm in process <pid> |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
|
the connection is kept alive. it will be closed automatically if
|
|
|
|
the JDWP process terminates (this allows adbd to detect dead
|
|
|
|
processes).
|
|
|
|
|
|
|
|
adbd thus maintains a list of "active" JDWP processes. it can send
|
|
|
|
its content to clients through the "device:debug-ports" service,
|
|
|
|
or even updates through the "device:track-debug-ports" service.
|
|
|
|
|
|
|
|
when a debugger wants to connect, it simply runs the command
|
|
|
|
equivalent to "adb forward tcp:<hostport> jdwp:<pid>"
|
|
|
|
|
|
|
|
"jdwp:<pid>" is a new forward destination format used to target
|
|
|
|
a given JDWP process on the device. when sutch a request arrives,
|
|
|
|
adbd does the following:
|
|
|
|
|
|
|
|
- first, it calls socketpair() to create a pair of equivalent
|
|
|
|
sockets.
|
|
|
|
|
|
|
|
- it attaches the first socket in the pair to a local socket
|
|
|
|
which is itself attached to the transport's remote socket:
|
|
|
|
|
|
|
|
|
|
|
|
- it sends the file descriptor of the second socket directly
|
|
|
|
to the JDWP process with the help of sendmsg()
|
|
|
|
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
JDWP thread @jdwp-control
|
2009-03-04 04:32:55 +01:00
|
|
|
| |
|
|
|
|
| <----------------------|
|
|
|
|
| OK, try this file descriptor |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
|
|
|
|
then, the JDWP thread uses this new socket descriptor as its
|
|
|
|
pass-through connection to the debugger (and receives the
|
|
|
|
JDWP-Handshake message, answers to it, etc...)
|
|
|
|
|
|
|
|
this gives the following graphics:
|
|
|
|
____________________________________
|
|
|
|
| |
|
|
|
|
| ADB Server (host) |
|
|
|
|
| |
|
|
|
|
Debugger <---> LocalSocket <----> RemoteSocket |
|
|
|
|
| ^^ |
|
|
|
|
|___________________________||_______|
|
|
|
|
||
|
|
|
|
Transport ||
|
|
|
|
(TCP for emulator - USB for device) ||
|
|
|
|
||
|
|
|
|
___________________________||_______
|
|
|
|
| || |
|
|
|
|
| ADBD (device) || |
|
|
|
|
| VV |
|
|
|
|
JDWP <======> LocalSocket <----> RemoteSocket |
|
|
|
|
| |
|
|
|
|
|____________________________________|
|
|
|
|
|
|
|
|
due to the way adb works, this doesn't need a special socket
|
|
|
|
type or fancy handling of socket termination if either the debugger
|
|
|
|
or the JDWP process closes the connection.
|
|
|
|
|
|
|
|
THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
|
|
|
|
TO HAVE A BETTER IDEA, LET ME KNOW - Digit
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/** JDWP PID List Support Code
|
|
|
|
** for each JDWP process, we record its pid and its connected socket
|
|
|
|
**/
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
// PIDs are transmitted as 4 hex digits in ascii.
|
|
|
|
static constexpr size_t PID_LEN = 4;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_process_event(int socket, unsigned events, void* _proc);
|
|
|
|
static void jdwp_process_list_updated(void);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
struct JdwpProcess;
|
|
|
|
static std::list<std::unique_ptr<JdwpProcess>> _jdwp_list;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
struct JdwpProcess {
|
2016-05-14 00:28:34 +02:00
|
|
|
explicit JdwpProcess(int socket) {
|
|
|
|
this->socket = socket;
|
|
|
|
this->fde = fdevent_create(socket, jdwp_process_event, this);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (!this->fde) {
|
|
|
|
fatal("could not create fdevent for new JDWP process");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
this->fde->state |= FDE_DONT_CLOSE;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
/* start by waiting for the PID */
|
|
|
|
fdevent_add(this->fde, FDE_READ);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
~JdwpProcess() {
|
|
|
|
if (this->socket >= 0) {
|
|
|
|
adb_shutdown(this->socket);
|
|
|
|
adb_close(this->socket);
|
|
|
|
this->socket = -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (this->fde) {
|
|
|
|
fdevent_destroy(this->fde);
|
|
|
|
this->fde = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
out_fds.clear();
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
void RemoveFromList() {
|
|
|
|
if (this->pid >= 0) {
|
|
|
|
D("removing pid %d from jdwp process list", this->pid);
|
|
|
|
} else {
|
|
|
|
D("removing transient JdwpProcess from list");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
auto pred = [this](const auto& proc) { return proc.get() == this; };
|
|
|
|
_jdwp_list.remove_if(pred);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
int pid = -1;
|
|
|
|
int socket = -1;
|
|
|
|
fdevent* fde = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
std::vector<unique_fd> out_fds;
|
|
|
|
char in_buf[PID_LEN + 1];
|
|
|
|
ssize_t in_len = 0;
|
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static size_t jdwp_process_list(char* buffer, size_t bufferlen) {
|
|
|
|
std::string temp;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
for (auto& proc : _jdwp_list) {
|
|
|
|
/* skip transient connections */
|
|
|
|
if (proc->pid < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
std::string next = std::to_string(proc->pid) + "\n";
|
|
|
|
if (temp.length() + next.length() > bufferlen) {
|
|
|
|
D("truncating JDWP process list (max len = %zu)", bufferlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
temp.append(next);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
memcpy(buffer, temp.data(), temp.length());
|
|
|
|
return temp.length();
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static size_t jdwp_process_list_msg(char* buffer, size_t bufferlen) {
|
|
|
|
// Message is length-prefixed with 4 hex digits in ASCII.
|
|
|
|
static constexpr size_t header_len = 4;
|
|
|
|
if (bufferlen < header_len) {
|
|
|
|
fatal("invalid JDWP process list buffer size: %zu", bufferlen);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
char head[header_len + 1];
|
|
|
|
size_t len = jdwp_process_list(buffer + header_len, bufferlen - header_len);
|
|
|
|
snprintf(head, sizeof head, "%04zx", len);
|
|
|
|
memcpy(buffer, head, header_len);
|
|
|
|
return len + header_len;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_process_event(int socket, unsigned events, void* _proc) {
|
|
|
|
JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (events & FDE_READ) {
|
|
|
|
if (proc->pid < 0) {
|
|
|
|
/* read the PID as a 4-hexchar string */
|
2016-05-14 00:28:34 +02:00
|
|
|
if (proc->in_len < 0) {
|
|
|
|
fatal("attempting to read JDWP pid again?");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
char* p = proc->in_buf + proc->in_len;
|
|
|
|
size_t size = PID_LEN - proc->in_len;
|
|
|
|
|
|
|
|
ssize_t rc = TEMP_FAILURE_RETRY(recv(socket, p, size, 0));
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-05-14 00:28:34 +02:00
|
|
|
|
|
|
|
D("failed to read jdwp pid: %s", strerror(errno));
|
|
|
|
goto CloseProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc->in_len += rc;
|
|
|
|
if (proc->in_len != PID_LEN) {
|
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
proc->in_buf[PID_LEN] = '\0';
|
|
|
|
proc->in_len = -1;
|
|
|
|
|
|
|
|
if (sscanf(proc->in_buf, "%04x", &proc->pid) != 1) {
|
|
|
|
D("could not decode JDWP %p PID number: '%s'", proc, p);
|
2009-03-04 04:32:55 +01:00
|
|
|
goto CloseProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all is well, keep reading to detect connection closure */
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Adding pid %d to jdwp process list", proc->pid);
|
2009-03-04 04:32:55 +01:00
|
|
|
jdwp_process_list_updated();
|
2016-05-14 00:28:34 +02:00
|
|
|
} else {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* the pid was read, if we get there it's probably because the connection
|
|
|
|
* was closed (e.g. the JDWP process exited or crashed) */
|
2016-05-14 00:28:34 +02:00
|
|
|
char buf[32];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
while (true) {
|
|
|
|
int len = TEMP_FAILURE_RETRY(recv(socket, buf, sizeof(buf), 0));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (len == 0) {
|
|
|
|
D("terminating JDWP %d connection: EOF", proc->pid);
|
|
|
|
break;
|
|
|
|
} else if (len < 0) {
|
|
|
|
if (len < 0 && errno == EAGAIN) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-14 00:28:34 +02:00
|
|
|
|
|
|
|
D("terminating JDWP %d connection: EOF", proc->pid);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
D("ignoring unexpected JDWP %d control socket activity (%d bytes)", proc->pid,
|
|
|
|
len);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
goto CloseProcess;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events & FDE_WRITE) {
|
2016-05-14 00:28:34 +02:00
|
|
|
D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size());
|
|
|
|
if (!proc->out_fds.empty()) {
|
|
|
|
int fd = proc->out_fds.back().get();
|
|
|
|
struct cmsghdr* cmsg;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct iovec iov;
|
|
|
|
char dummy = '!';
|
|
|
|
char buffer[sizeof(struct cmsghdr) + sizeof(int)];
|
|
|
|
|
|
|
|
iov.iov_base = &dummy;
|
|
|
|
iov.iov_len = 1;
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
msg.msg_control = buffer;
|
2009-03-04 04:32:55 +01:00
|
|
|
msg.msg_controllen = sizeof(buffer);
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
2016-05-14 00:28:34 +02:00
|
|
|
cmsg->cmsg_len = msg.msg_controllen;
|
2009-03-04 04:32:55 +01:00
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
2016-05-14 00:28:34 +02:00
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
2009-03-04 04:32:55 +01:00
|
|
|
((int*)CMSG_DATA(cmsg))[0] = fd;
|
|
|
|
|
2015-10-07 00:10:05 +02:00
|
|
|
if (!set_file_block_mode(proc->socket, true)) {
|
|
|
|
VLOG(JDWP) << "failed to set blocking mode for fd " << proc->socket;
|
2010-02-15 12:20:44 +01:00
|
|
|
goto CloseProcess;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
int ret = TEMP_FAILURE_RETRY(sendmsg(proc->socket, &msg, 0));
|
|
|
|
if (ret < 0) {
|
|
|
|
D("sending new file descriptor to JDWP %d failed: %s", proc->pid, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
goto CloseProcess;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
adb_close(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
D("sent file descriptor %d to JDWP process %d", fd, proc->pid);
|
|
|
|
|
|
|
|
proc->out_fds.pop_back();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-10-07 00:10:05 +02:00
|
|
|
if (!set_file_block_mode(proc->socket, false)) {
|
|
|
|
VLOG(JDWP) << "failed to set non-blocking mode for fd " << proc->socket;
|
2010-02-15 12:20:44 +01:00
|
|
|
goto CloseProcess;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (proc->out_fds.empty()) {
|
|
|
|
fdevent_del(proc->fde, FDE_WRITE);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
CloseProcess:
|
|
|
|
proc->RemoveFromList();
|
|
|
|
jdwp_process_list_updated();
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
int create_jdwp_connection_fd(int pid) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("looking for pid %d in JDWP process list", pid);
|
2016-05-14 00:28:34 +02:00
|
|
|
|
|
|
|
for (auto& proc : _jdwp_list) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (proc->pid == pid) {
|
2016-05-14 00:28:34 +02:00
|
|
|
int fds[2];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (adb_socketpair(fds) < 0) {
|
|
|
|
D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
D("socketpair: (%d,%d)", fds[0], fds[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
proc->out_fds.emplace_back(fds[1]);
|
|
|
|
if (proc->out_fds.size() == 1) {
|
|
|
|
fdevent_add(proc->fde, FDE_WRITE);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
return fds[0];
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 00:28:34 +02:00
|
|
|
D("search failed !!");
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** VM DEBUG CONTROL SOCKET
|
|
|
|
**
|
|
|
|
** we do implement a custom asocket to receive the data
|
|
|
|
**/
|
|
|
|
|
|
|
|
/* name of the debug control Unix socket */
|
2016-05-14 00:28:34 +02:00
|
|
|
#define JDWP_CONTROL_NAME "\0jdwp-control"
|
|
|
|
#define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME) - 1)
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-17 18:47:42 +02:00
|
|
|
struct JdwpControl {
|
2016-05-14 00:28:34 +02:00
|
|
|
int listen_socket;
|
|
|
|
fdevent* fde;
|
2015-04-17 18:47:42 +02:00
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static JdwpControl _jdwp_control;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_control_event(int s, unsigned events, void* user);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static int jdwp_control_init(JdwpControl* control, const char* sockname, int socknamelen) {
|
|
|
|
sockaddr_un addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int s;
|
|
|
|
int maxpath = sizeof(addr.sun_path);
|
|
|
|
int pathlen = socknamelen;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (pathlen >= maxpath) {
|
2016-05-14 00:28:34 +02:00
|
|
|
D("vm debug control socket name too long (%d extra chars)", pathlen + 1 - maxpath);
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
memcpy(addr.sun_path, sockname, socknamelen);
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (s < 0) {
|
2016-05-14 00:28:34 +02:00
|
|
|
D("could not create vm debug control socket. %d: %s", errno, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
addrlen = pathlen + sizeof(addr.sun_family);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-12-01 09:27:59 +01:00
|
|
|
if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) < 0) {
|
2016-05-14 00:28:34 +02:00
|
|
|
D("could not bind vm debug control socket: %d: %s", errno, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (listen(s, 4) < 0) {
|
|
|
|
D("listen failed in jdwp control socket: %d: %s", errno, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
control->listen_socket = s;
|
|
|
|
|
|
|
|
control->fde = fdevent_create(s, jdwp_control_event, control);
|
|
|
|
if (control->fde == NULL) {
|
2016-05-14 00:28:34 +02:00
|
|
|
D("could not create fdevent for jdwp control socket");
|
2009-03-04 04:32:55 +01:00
|
|
|
adb_close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only wait for incoming connections */
|
|
|
|
fdevent_add(control->fde, FDE_READ);
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("jdwp control socket started (%d)", control->listen_socket);
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_control_event(int s, unsigned events, void* _control) {
|
|
|
|
JdwpControl* control = (JdwpControl*)_control;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (events & FDE_READ) {
|
2016-08-24 00:41:56 +02:00
|
|
|
int s = adb_socket_accept(control->listen_socket, nullptr, nullptr);
|
2016-05-14 00:28:34 +02:00
|
|
|
if (s < 0) {
|
|
|
|
if (errno == ECONNABORTED) {
|
|
|
|
/* oops, the JDWP process died really quick */
|
|
|
|
D("oops, the JDWP process died really quick");
|
|
|
|
return;
|
|
|
|
} else {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* the socket is probably closed ? */
|
2016-05-14 00:28:34 +02:00
|
|
|
D("weird accept() failed on jdwp control socket: %s", strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
auto proc = std::make_unique<JdwpProcess>(s);
|
|
|
|
if (!proc) {
|
|
|
|
fatal("failed to allocate JdwpProcess");
|
|
|
|
}
|
|
|
|
|
|
|
|
_jdwp_list.emplace_back(std::move(proc));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** "jdwp" local service implementation
|
|
|
|
** this simply returns the list of known JDWP process pids
|
|
|
|
**/
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
struct JdwpSocket : public asocket {
|
|
|
|
bool pass;
|
2015-04-17 18:47:42 +02:00
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_socket_close(asocket* s) {
|
|
|
|
D("LS(%d): closing jdwp socket", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (s->peer) {
|
|
|
|
D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
|
|
|
|
s->peer->peer = nullptr;
|
|
|
|
s->peer->close(s->peer);
|
|
|
|
s->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-05-14 00:28:34 +02:00
|
|
|
|
|
|
|
remove_socket(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static int jdwp_socket_enqueue(asocket* s, apacket* p) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* you can't write to this asocket */
|
2016-05-14 00:28:34 +02:00
|
|
|
D("LS(%d): JDWP socket received data?", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
put_apacket(p);
|
|
|
|
s->peer->close(s->peer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_socket_ready(asocket* s) {
|
|
|
|
JdwpSocket* jdwp = (JdwpSocket*)s;
|
|
|
|
asocket* peer = jdwp->peer;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
/* on the first call, send the list of pids,
|
|
|
|
* on the second one, close the connection
|
|
|
|
*/
|
|
|
|
if (!jdwp->pass) {
|
|
|
|
apacket* p = get_apacket();
|
2015-07-13 20:12:28 +02:00
|
|
|
p->len = jdwp_process_list((char*)p->data, s->get_max_payload());
|
2009-03-04 04:32:55 +01:00
|
|
|
peer->enqueue(peer, p);
|
2016-05-14 00:28:34 +02:00
|
|
|
jdwp->pass = true;
|
|
|
|
} else {
|
2009-03-04 04:32:55 +01:00
|
|
|
peer->close(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
asocket* create_jdwp_service_socket(void) {
|
2015-02-26 02:51:28 +01:00
|
|
|
JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (!s) {
|
|
|
|
fatal("failed to allocate JdwpSocket");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
install_local_socket(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
s->ready = jdwp_socket_ready;
|
|
|
|
s->enqueue = jdwp_socket_enqueue;
|
|
|
|
s->close = jdwp_socket_close;
|
|
|
|
s->pass = false;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
return s;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** "track-jdwp" local service implementation
|
|
|
|
** this periodically sends the list of known JDWP process pids
|
|
|
|
** to the client...
|
|
|
|
**/
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
struct JdwpTracker : public asocket {
|
|
|
|
bool need_initial;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static std::vector<std::unique_ptr<JdwpTracker>> _jdwp_trackers;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_process_list_updated(void) {
|
|
|
|
char buffer[1024];
|
|
|
|
int len = jdwp_process_list_msg(buffer, sizeof(buffer));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
for (auto& t : _jdwp_trackers) {
|
|
|
|
apacket* p = get_apacket();
|
2009-03-04 04:32:55 +01:00
|
|
|
memcpy(p->data, buffer, len);
|
|
|
|
p->len = len;
|
2016-05-14 00:28:34 +02:00
|
|
|
|
|
|
|
if (t->peer) {
|
|
|
|
// The tracker might not have been connected yet.
|
|
|
|
t->peer->enqueue(t->peer, p);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_tracker_close(asocket* s) {
|
|
|
|
D("LS(%d): destroying jdwp tracker service", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (s->peer) {
|
|
|
|
D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
|
|
|
|
s->peer->peer = nullptr;
|
|
|
|
s->peer->close(s->peer);
|
|
|
|
s->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
remove_socket(s);
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
auto pred = [s](const auto& tracker) { return tracker.get() == s; };
|
|
|
|
std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static void jdwp_tracker_ready(asocket* s) {
|
|
|
|
JdwpTracker* t = (JdwpTracker*)s;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
if (t->need_initial) {
|
|
|
|
apacket* p = get_apacket();
|
|
|
|
t->need_initial = false;
|
2015-07-13 20:12:28 +02:00
|
|
|
p->len = jdwp_process_list_msg((char*)p->data, s->get_max_payload());
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->enqueue(s->peer, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
static int jdwp_tracker_enqueue(asocket* s, apacket* p) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* you can't write to this socket */
|
2016-05-14 00:28:34 +02:00
|
|
|
D("LS(%d): JDWP tracker received data?", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
put_apacket(p);
|
|
|
|
s->peer->close(s->peer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
asocket* create_jdwp_tracker_service_socket(void) {
|
|
|
|
auto t = std::make_unique<JdwpTracker>();
|
|
|
|
if (!t) {
|
|
|
|
fatal("failed to allocate JdwpTracker");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
memset(t.get(), 0, sizeof(asocket));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
install_local_socket(t.get());
|
|
|
|
D("LS(%d): created new jdwp tracker service", t->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
t->ready = jdwp_tracker_ready;
|
|
|
|
t->enqueue = jdwp_tracker_enqueue;
|
|
|
|
t->close = jdwp_tracker_close;
|
|
|
|
t->need_initial = true;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
asocket* result = t.get();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
_jdwp_trackers.emplace_back(std::move(t));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
return result;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:28:34 +02:00
|
|
|
int init_jdwp(void) {
|
|
|
|
return jdwp_control_init(&_jdwp_control, JDWP_CONTROL_NAME, JDWP_CONTROL_NAME_LEN);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !ADB_HOST */
|