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_mmalloc.h"
23 #include "src/mc/mc_ignore.h"
24 #include "src/mc/mc_private.h" // MC_deadlock_check()
25 #include "src/mc/mc_smx.h"
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
31 mc_client_t mc_client;
33 void MC_client_init(void)
35 if (mc_mode != MC_MODE_NONE)
37 if (!getenv(MC_ENV_SOCKET_FD))
39 mc_mode = MC_MODE_CLIENT;
42 XBT_WARN("MC_client_init called more than once.");
46 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
48 xbt_die("MC socket not found");
50 int fd = xbt_str_parse_int(fd_env,bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
51 XBT_DEBUG("Model-checked application found socket FD %i", fd);
54 socklen_t socklen = sizeof(type);
55 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
56 xbt_die("Could not check socket type");
57 if (type != SOCK_DGRAM)
58 xbt_die("Unexpected socket type %i", type);
59 XBT_DEBUG("Model-checked application found expected socket type");
61 mc_client = xbt_new0(s_mc_client_t, 1);
63 mc_client->active = 1;
65 // Waiting for the model-checker:
66 if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1 || raise(SIGSTOP) != 0)
67 xbt_die("Could not wait for the model-checker");
68 MC_client_handle_messages();
71 void MC_client_send_message(void* message, size_t size)
73 if (MC_protocol_send(mc_client->fd, message, size))
74 xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type);
77 void MC_client_send_simple_message(e_mc_message_type type)
79 if (MC_protocol_send_simple_message(mc_client->fd, type))
80 xbt_die("Could not send message %i", type);
83 void MC_client_handle_messages(void)
86 XBT_DEBUG("Waiting messages from model-checker");
88 char message_buffer[MC_MESSAGE_LENGTH];
90 if ((s = MC_receive_message(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) < 0)
91 xbt_die("Could not receive commands from the model-checker");
93 s_mc_message_t message;
94 if ((size_t) s < sizeof(message))
95 xbt_die("Received message is too small");
96 memcpy(&message, message_buffer, sizeof(message));
97 switch (message.type) {
99 case MC_MESSAGE_DEADLOCK_CHECK:
101 int result = MC_deadlock_check();
102 s_mc_int_message_t answer;
103 answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
104 answer.value = result;
105 if (MC_protocol_send(mc_client->fd, &answer, sizeof(answer)))
106 xbt_die("Could not send response");
110 case MC_MESSAGE_CONTINUE:
113 case MC_MESSAGE_SIMCALL_HANDLE:
115 s_mc_simcall_handle_message_t message;
116 if (s != sizeof(message))
117 xbt_die("Unexpected size for SIMCALL_HANDLE");
118 memcpy(&message, message_buffer, sizeof(message));
119 smx_process_t process = SIMIX_process_from_PID(message.pid);
121 xbt_die("Invalid pid %lu", (unsigned long) message.pid);
122 SIMIX_simcall_handle(&process->simcall, message.value);
123 MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
127 case MC_MESSAGE_RESTORE:
129 s_mc_restore_message_t message;
130 if (s != sizeof(message))
131 xbt_die("Unexpected size for SIMCALL_HANDLE");
132 memcpy(&message, message_buffer, sizeof(message));
133 smpi_really_switch_data_segment(message.index);
138 xbt_die("%s received unexpected message %s (%i)",
139 MC_mode_name(mc_mode),
140 MC_message_type_name(message.type),
147 void MC_client_main_loop(void)
150 MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING);
151 MC_client_handle_messages();
152 MC_wait_for_requests();