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_msg_t *msg, char *cmdbuf);
46 struct sockaddr_in lsin;
47 struct hammer2_iocom iocom;
52 * Acquire socket and set options
54 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
55 fprintf(stderr, "cmd_debug: socket(): %s\n",
61 * Connect to the target
63 bzero(&lsin, sizeof(lsin));
64 lsin.sin_family = AF_INET;
65 lsin.sin_addr.s_addr = 0;
66 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
67 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
69 fprintf(stderr, "debug: connect failed: %s\n",
75 * Run the session. The remote end transmits our prompt.
77 hammer2_iocom_init(&iocom, fd, 0);
78 printf("debug: connected\n");
80 msg = hammer2_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
81 hammer2_ioq_write(msg);
83 hammer2_iocom_core(&iocom, debug_recv, debug_send, debug_tty);
84 fprintf(stderr, "debug: disconnected\n");
90 * Callback from hammer2_iocom_core() when messages might be present
95 debug_recv(hammer2_iocom_t *iocom)
99 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
100 (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_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
112 hammer2_freemsg(msg);
114 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
116 * A reply from the remote is data we copy to stdout.
119 msg->aux_data[msg->aux_size - 1] = 0;
120 write(1, msg->aux_data, strlen(msg->aux_data));
122 write(1, "debug> ", 7);
124 hammer2_freemsg(msg);
127 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
128 fprintf(stderr, "Unknown message: %08x\n",
130 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
134 if (iocom->ioq_rx.error) {
135 fprintf(stderr, "node_master_recv: comm error %d\n",
136 iocom->ioq_rx.error);
141 * Callback from hammer2_iocom_core() when messages might be transmittable
146 debug_send(hammer2_iocom_t *iocom)
148 hammer2_iocom_flush(iocom);
153 debug_tty(hammer2_iocom_t *iocom)
159 if (fgets(buf, sizeof(buf), stdin) != NULL) {
161 if (len && buf[len - 1] == '\n')
164 msg = hammer2_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
165 bcopy(buf, msg->aux_data, len);
166 hammer2_ioq_write(msg);
169 * Set EOF flag without setting any error code for normal
172 iocom->flags |= HAMMER2_IOCOMF_EOF;
177 * This is called from the master node to process a received debug
178 * shell command. We process the command, outputting the results,
179 * then finish up by outputting another prompt.
182 hammer2_debug_remote(hammer2_msg_t *msg)
184 /* hammer2_iocom_t *iocom = msg->iocom; */
187 msg->aux_data[msg->aux_size - 1] = 0;
188 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
190 * A reply just prints out the string. No newline is added
191 * (it is expected to be embedded if desired).
194 write(2, msg->aux_data, strlen(msg->aux_data));
195 hammer2_freemsg(msg);
198 * Otherwise this is a command which we must process.
199 * When we are finished we generate a final reply.
201 hammer2_debug_parse(msg, msg->aux_data);
202 hammer2_replymsg(msg, 0);
207 hammer2_debug_parse(hammer2_msg_t *msg, char *cmdbuf)
209 /* hammer2_iocom_t *iocom = msg->iocom; */
210 char *cmd = strsep(&cmdbuf, " \t");
212 if (cmd == NULL || *cmd == 0) {
214 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
215 msg_printf(msg, "help Command help\n");
217 msg_printf(msg, "Unrecognized command: %s\n", cmd);
222 * Returns text debug output to the original defined by (msg). (msg) is
223 * not modified and stays intact.
226 msg_printf(hammer2_msg_t *msg, const char *ctl, ...)
228 /* hammer2_iocom_t *iocom = msg->iocom; */
235 vsnprintf(buf, sizeof(buf), ctl, va);
237 len = strlen(buf) + 1;
239 rmsg = hammer2_allocreply(msg, HAMMER2_DBG_SHELL, len);
240 bcopy(buf, rmsg->aux_data, len);
242 hammer2_ioq_write(rmsg);