2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static void debug_recv(hammer2_iocom_t *iocom);
39 static void debug_send(hammer2_iocom_t *iocom);
40 static void debug_tty(hammer2_iocom_t *iocom);
41 static void hammer2_debug_parse(hammer2_iocom_t *iocom,
42 hammer2_msg_t *msg, char *cmdbuf);
47 struct sockaddr_in lsin;
48 struct hammer2_iocom iocom;
53 * Acquire socket and set options
55 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
56 fprintf(stderr, "cmd_debug: socket(): %s\n",
62 * Connect to the target
64 bzero(&lsin, sizeof(lsin));
65 lsin.sin_family = AF_INET;
66 lsin.sin_addr.s_addr = 0;
67 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
68 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
70 fprintf(stderr, "debug: connect failed: %s\n",
76 * Run the session. The remote end transmits our prompt.
78 hammer2_iocom_init(&iocom, fd, 0);
79 printf("debug: connected\n");
81 msg = hammer2_iocom_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
82 hammer2_ioq_write(&iocom, msg);
84 hammer2_iocom_core(&iocom, debug_recv, debug_send, debug_tty);
85 fprintf(stderr, "debug: disconnected\n");
91 * Callback from hammer2_iocom_core() when messages might be present
96 debug_recv(hammer2_iocom_t *iocom)
100 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
101 (msg = hammer2_ioq_read(iocom)) != NULL) {
102 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
103 case HAMMER2_LNK_ERROR:
104 fprintf(stderr, "Link Error: %d\n",
105 msg->any.head.error);
107 case HAMMER2_DBG_SHELL:
109 * We send the commands, not accept them.
111 hammer2_iocom_freemsg(iocom, msg);
113 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
115 * A reply from the remote is data we copy to stdout.
118 msg->aux_data[msg->aux_size - 1] = 0;
119 write(1, msg->aux_data, strlen(msg->aux_data));
121 hammer2_iocom_freemsg(iocom, msg);
124 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
125 fprintf(stderr, "Unknown message: %08x\n",
127 hammer2_ioq_reply_term(iocom, msg,
128 HAMMER2_MSG_ERR_UNKNOWN);
132 if (iocom->ioq_rx.error) {
133 fprintf(stderr, "node_master_recv: comm error %d\n",
134 iocom->ioq_rx.error);
139 * Callback from hammer2_iocom_core() when messages might be transmittable
144 debug_send(hammer2_iocom_t *iocom)
146 hammer2_ioq_write(iocom, NULL);
151 debug_tty(hammer2_iocom_t *iocom)
157 if (fgets(buf, sizeof(buf), stdin) != NULL) {
159 if (len && buf[len - 1] == '\n')
162 msg = hammer2_iocom_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
163 bcopy(buf, msg->aux_data, len);
164 hammer2_ioq_write(iocom, msg);
167 * Set EOF flag without setting any error code for normal
170 iocom->flags |= HAMMER2_IOCOMF_EOF;
175 * This is called from the master node to process a received debug
176 * shell command. We process the command, outputting the results,
177 * then finish up by outputting another prompt.
180 hammer2_debug_remote(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
183 msg->aux_data[msg->aux_size - 1] = 0;
184 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
186 * A reply just prints out the string. No newline is added
187 * (it is expected to be embedded if desired).
190 write(2, msg->aux_data, strlen(msg->aux_data));
191 hammer2_iocom_freemsg(iocom, msg);
194 * Otherwise this is a command which we must process.
195 * When we are finished we generate a final reply.
197 hammer2_debug_parse(iocom, msg, msg->aux_data);
198 iocom_printf(iocom, msg, "debug> ");
199 hammer2_iocom_freemsg(iocom, msg);
204 hammer2_debug_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg, char *cmdbuf)
206 char *cmd = strsep(&cmdbuf, " \t");
208 if (cmd == NULL || *cmd == 0) {
210 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
211 iocom_printf(iocom, msg,
212 "help Command help\n"
215 iocom_printf(iocom, msg, "Unrecognized command: %s\n", cmd);
220 iocom_printf(hammer2_iocom_t *iocom, hammer2_msg_t *msg, const char *ctl, ...)
228 vsnprintf(buf, sizeof(buf), ctl, va);
230 len = strlen(buf) + 1;
232 rmsg = hammer2_iocom_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
233 bcopy(buf, rmsg->aux_data, len);
234 rmsg->any.head = msg->any.head;
235 rmsg->any.head.aux_icrc = 0;
237 hammer2_ioq_reply(iocom, rmsg);