1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <sys/types.h>
11 #include <sys/ptrace.h>
12 #include <sys/socket.h>
15 #include <xbt/sysdep.h>
16 #include <xbt/mmalloc.h>
18 #include "src/mc/mc_protocol.h"
19 #include "src/mc/Client.hpp"
21 // We won't need those once the separation MCer/MCed is complete:
22 #include "src/mc/mc_ignore.h"
23 #include "src/mc/mc_private.h" // MC_deadlock_check()
24 #include "src/mc/mc_smx.h"
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
31 std::unique_ptr<Client> Client::client_;
33 Client* Client::initialize()
35 // We are not in MC mode:
36 // TODO, handle this more gracefully.
37 if (!getenv(MC_ENV_SOCKET_FD))
40 // Do not break if we are called multiple times:
44 // Check and set the mode:
45 if (mc_mode != MC_MODE_NONE)
47 mc_mode = MC_MODE_CLIENT;
49 // Fetch socket from MC_ENV_SOCKET_FD:
50 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
52 xbt_die("No MC socket passed in the environment");
53 int fd = xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
54 XBT_DEBUG("Model-checked application found socket FD %i", fd);
56 // Check the socket type/validity:
58 socklen_t socklen = sizeof(type);
59 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
60 xbt_die("Could not check socket type");
61 if (type != SOCK_DGRAM)
62 xbt_die("Unexpected socket type %i", type);
63 XBT_DEBUG("Model-checked application found expected socket type");
65 client_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
67 // Wait for the model-checker:
68 if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0)
69 xbt_die("Could not wait for the model-checker");
71 client_->handleMessages();
75 void Client::handleMessages()
78 XBT_DEBUG("Waiting messages from model-checker");
80 char message_buffer[MC_MESSAGE_LENGTH];
83 if ((s = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
84 xbt_die("Could not receive commands from the model-checker");
86 s_mc_message_t message;
87 if ((size_t) s < sizeof(message))
88 xbt_die("Received message is too small");
89 memcpy(&message, message_buffer, sizeof(message));
90 switch (message.type) {
92 case MC_MESSAGE_DEADLOCK_CHECK:
94 int result = MC_deadlock_check();
95 s_mc_int_message_t answer;
96 answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
97 answer.value = result;
98 if (channel_.send(answer))
99 xbt_die("Could not send response");
103 case MC_MESSAGE_CONTINUE:
106 case MC_MESSAGE_SIMCALL_HANDLE:
108 s_mc_simcall_handle_message_t message;
109 if (s != sizeof(message))
110 xbt_die("Unexpected size for SIMCALL_HANDLE");
111 memcpy(&message, message_buffer, sizeof(message));
112 smx_process_t process = SIMIX_process_from_PID(message.pid);
114 xbt_die("Invalid pid %lu", (unsigned long) message.pid);
115 SIMIX_simcall_handle(&process->simcall, message.value);
116 if (channel_.send(MC_MESSAGE_WAITING))
117 xbt_die("Could not send MESSAGE_WAITING to model-checker");
121 case MC_MESSAGE_RESTORE:
123 s_mc_restore_message_t message;
124 if (s != sizeof(message))
125 xbt_die("Unexpected size for SIMCALL_HANDLE");
126 memcpy(&message, message_buffer, sizeof(message));
127 smpi_really_switch_data_segment(message.index);
132 xbt_die("%s received unexpected message %s (%i)",
133 MC_mode_name(mc_mode),
134 MC_message_type_name(message.type),
141 void Client::mainLoop(void)
144 if (channel_.send(MC_MESSAGE_WAITING))
145 xbt_die("Could not send WAITING mesage to model-checker");
146 this->handleMessages();
147 simgrid::mc::wait_for_requests();