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/mc_client.h"
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"
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
30 mc_client_t mc_client;
32 void MC_client_init(void)
34 if (mc_mode != MC_MODE_NONE)
36 if (!getenv(MC_ENV_SOCKET_FD))
38 mc_mode = MC_MODE_CLIENT;
41 XBT_WARN("MC_client_init called more than once.");
45 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
47 xbt_die("MC socket not found");
49 int fd = xbt_str_parse_int(fd_env,bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
50 XBT_DEBUG("Model-checked application found socket FD %i", fd);
53 socklen_t socklen = sizeof(type);
54 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
55 xbt_die("Could not check socket type");
56 if (type != SOCK_DGRAM)
57 xbt_die("Unexpected socket type %i", type);
58 XBT_DEBUG("Model-checked application found expected socket type");
60 mc_client = xbt_new0(s_mc_client_t, 1);
62 mc_client->active = 1;
64 // Waiting for the model-checker:
65 if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0)
66 xbt_die("Could not wait for the model-checker");
67 MC_client_handle_messages();
70 void MC_client_send_message(void* message, size_t size)
72 if (MC_protocol_send(mc_client->fd, message, size))
73 xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type);
76 void MC_client_send_simple_message(e_mc_message_type type)
78 if (MC_protocol_send_simple_message(mc_client->fd, type))
79 xbt_die("Could not send message %i", type);
82 void MC_client_handle_messages(void)
85 XBT_DEBUG("Waiting messages from model-checker");
87 char message_buffer[MC_MESSAGE_LENGTH];
89 if ((s = MC_receive_message(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) < 0)
90 xbt_die("Could not receive commands from the model-checker");
92 s_mc_message_t message;
93 if ((size_t) s < sizeof(message))
94 xbt_die("Received message is too small");
95 memcpy(&message, message_buffer, sizeof(message));
96 switch (message.type) {
98 case MC_MESSAGE_DEADLOCK_CHECK:
100 int result = MC_deadlock_check();
101 s_mc_int_message_t answer;
102 answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
103 answer.value = result;
104 if (MC_protocol_send(mc_client->fd, &answer, sizeof(answer)))
105 xbt_die("Could not send response");
109 case MC_MESSAGE_CONTINUE:
112 case MC_MESSAGE_SIMCALL_HANDLE:
114 s_mc_simcall_handle_message_t message;
115 if (s != sizeof(message))
116 xbt_die("Unexpected size for SIMCALL_HANDLE");
117 memcpy(&message, message_buffer, sizeof(message));
118 smx_process_t process = SIMIX_process_from_PID(message.pid);
120 xbt_die("Invalid pid %lu", (unsigned long) message.pid);
121 SIMIX_simcall_handle(&process->simcall, message.value);
122 MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
126 case MC_MESSAGE_RESTORE:
128 s_mc_restore_message_t message;
129 if (s != sizeof(message))
130 xbt_die("Unexpected size for SIMCALL_HANDLE");
131 memcpy(&message, message_buffer, sizeof(message));
132 smpi_really_switch_data_segment(message.index);
137 xbt_die("%s received unexpected message %s (%i)",
138 MC_mode_name(mc_mode),
139 MC_message_type_name(message.type),
146 void MC_client_main_loop(void)
149 MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
150 MC_client_handle_messages();
151 MC_wait_for_requests();