Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Windows debugger socket setup #10212

Merged
merged 4 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Working version
default overhead parameter accordingly.
(Damien Doligez, review by Josh Berdine and Xavier Leroy)

- #10212: Simplify and improve the Windows-specific code that connects
to the debugger via a socket.
(Antonin Décimo, review by Xavier Leroy)

### Code generation and optimizations:

- #9876: do not cache the young_limit GC variable in a processor register.
Expand Down
55 changes: 21 additions & 34 deletions runtime/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CAMLexport void caml_debugger_cleanup_fork(void)
#include <netdb.h>
#else
#define ATOM ATOM_WS
#include <winsock.h>
#include <winsock2.h>
#undef ATOM
#include <process.h>
#endif
Expand Down Expand Up @@ -103,42 +103,29 @@ static struct skiplist event_points_table = SKIPLIST_STATIC_INITIALIZER;
static void open_connection(void)
{
#ifdef _WIN32
/* Set socket to synchronous mode so that file descriptor-oriented
functions (read()/write() etc.) can be used */

int oldvalue, oldvaluelen, newvalue, retcode;
oldvaluelen = sizeof(oldvalue);
retcode = getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
(char *) &oldvalue, &oldvaluelen);
if (retcode == 0) {
newvalue = SO_SYNCHRONOUS_NONALERT;
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
(char *) &newvalue, sizeof(newvalue));
}
#endif
/* Set socket to synchronous mode (= non-overlapped) so that file
descriptor-oriented functions (read()/write() etc.) can be
used */
SOCKET sock = WSASocket(sock_domain, SOCK_STREAM, 0,
NULL, 0,
0 /* not WSA_FLAG_OVERLAPPED */);
if (sock == INVALID_SOCKET
|| connect(sock, &sock_addr.s_gen, sock_addr_len) != 0)
caml_fatal_error("cannot connect to debugger at %s\n"
"WSA error code: %d",
(dbg_addr ? dbg_addr : "(none)"),
WSAGetLastError());
dbg_socket = _open_osfhandle(sock, 0);
if (dbg_socket == -1)
#else
dbg_socket = socket(sock_domain, SOCK_STREAM, 0);
#ifdef _WIN32
if (retcode == 0) {
/* Restore initial mode */
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
(char *) &oldvalue, oldvaluelen);
}
#endif
if (dbg_socket == -1 ||
connect(dbg_socket, &sock_addr.s_gen, sock_addr_len) == -1){
caml_fatal_error
(
"cannot connect to debugger at %s\n"
"error: %s",
(dbg_addr ? dbg_addr : "(none)"),
strerror (errno)
);
}
#ifdef _WIN32
dbg_socket = _open_osfhandle(dbg_socket, 0);
if (dbg_socket == -1)
caml_fatal_error("_open_osfhandle failed");
connect(dbg_socket, &sock_addr.s_gen, sock_addr_len) == -1)
#endif
caml_fatal_error("cannot connect to debugger at %s\n"
"error: %s",
(dbg_addr ? dbg_addr : "(none)"),
strerror (errno));
dbg_in = caml_open_descriptor_in(dbg_socket);
dbg_out = caml_open_descriptor_out(dbg_socket);
/* The code in this file does not bracket channel I/O operations with
Expand Down