You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

367 lines
10 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2010 Øyvind Harboe *
  3. * oyvind.harboe@zylin.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "tcl_server.h"
  22. #include <target/target.h>
  23. #include <helper/binarybuffer.h>
  24. #define TCL_SERVER_VERSION "TCL Server 0.1"
  25. #define TCL_LINE_INITIAL (4*1024)
  26. #define TCL_LINE_MAX (4*1024*1024)
  27. struct tcl_connection {
  28. int tc_linedrop;
  29. int tc_lineoffset;
  30. int tc_line_size;
  31. char *tc_line;
  32. int tc_outerror;/* flag an output error */
  33. enum target_state tc_laststate;
  34. bool tc_notify;
  35. bool tc_trace;
  36. };
  37. static char *tcl_port;
  38. /* handlers */
  39. static int tcl_new_connection(struct connection *connection);
  40. static int tcl_input(struct connection *connection);
  41. static int tcl_output(struct connection *connection, const void *buf, ssize_t len);
  42. static int tcl_closed(struct connection *connection);
  43. static int tcl_target_callback_event_handler(struct target *target,
  44. enum target_event event, void *priv)
  45. {
  46. struct connection *connection = priv;
  47. struct tcl_connection *tclc;
  48. char buf[256];
  49. tclc = connection->priv;
  50. if (tclc->tc_notify) {
  51. snprintf(buf, sizeof(buf), "type target_event event %s\r\n\x1a", target_event_name(event));
  52. tcl_output(connection, buf, strlen(buf));
  53. }
  54. if (tclc->tc_laststate != target->state) {
  55. tclc->tc_laststate = target->state;
  56. if (tclc->tc_notify) {
  57. snprintf(buf, sizeof(buf), "type target_state state %s\r\n\x1a", target_state_name(target));
  58. tcl_output(connection, buf, strlen(buf));
  59. }
  60. }
  61. return ERROR_OK;
  62. }
  63. static int tcl_target_callback_reset_handler(struct target *target,
  64. enum target_reset_mode reset_mode, void *priv)
  65. {
  66. struct connection *connection = priv;
  67. struct tcl_connection *tclc;
  68. char buf[256];
  69. tclc = connection->priv;
  70. if (tclc->tc_notify) {
  71. snprintf(buf, sizeof(buf), "type target_reset mode %s\r\n\x1a", target_reset_mode_name(reset_mode));
  72. tcl_output(connection, buf, strlen(buf));
  73. }
  74. return ERROR_OK;
  75. }
  76. static int tcl_target_callback_trace_handler(struct target *target,
  77. size_t len, uint8_t *data, void *priv)
  78. {
  79. struct connection *connection = priv;
  80. struct tcl_connection *tclc;
  81. char *header = "type target_trace data ";
  82. char *trailer = "\r\n\x1a";
  83. size_t hex_len = len * 2 + 1;
  84. size_t max_len = hex_len + strlen(header) + strlen(trailer);
  85. char *buf, *hex;
  86. tclc = connection->priv;
  87. if (tclc->tc_trace) {
  88. hex = malloc(hex_len);
  89. buf = malloc(max_len);
  90. hexify(hex, data, len, hex_len);
  91. snprintf(buf, max_len, "%s%s%s", header, hex, trailer);
  92. tcl_output(connection, buf, strlen(buf));
  93. free(hex);
  94. free(buf);
  95. }
  96. return ERROR_OK;
  97. }
  98. /* write data out to a socket.
  99. *
  100. * this is a blocking write, so the return value must equal the length, if
  101. * that is not the case then flag the connection with an output error.
  102. */
  103. int tcl_output(struct connection *connection, const void *data, ssize_t len)
  104. {
  105. ssize_t wlen;
  106. struct tcl_connection *tclc;
  107. tclc = connection->priv;
  108. if (tclc->tc_outerror)
  109. return ERROR_SERVER_REMOTE_CLOSED;
  110. wlen = connection_write(connection, data, len);
  111. if (wlen == len)
  112. return ERROR_OK;
  113. LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
  114. tclc->tc_outerror = 1;
  115. return ERROR_SERVER_REMOTE_CLOSED;
  116. }
  117. /* connections */
  118. static int tcl_new_connection(struct connection *connection)
  119. {
  120. struct tcl_connection *tclc;
  121. tclc = calloc(1, sizeof(struct tcl_connection));
  122. if (!tclc)
  123. return ERROR_CONNECTION_REJECTED;
  124. tclc->tc_line_size = TCL_LINE_INITIAL;
  125. tclc->tc_line = malloc(tclc->tc_line_size);
  126. if (!tclc->tc_line) {
  127. free(tclc);
  128. return ERROR_CONNECTION_REJECTED;
  129. }
  130. connection->priv = tclc;
  131. struct target *target = get_current_target_or_null(connection->cmd_ctx);
  132. if (target)
  133. tclc->tc_laststate = target->state;
  134. /* store the connection object on cmd_ctx so we can access it from command handlers */
  135. connection->cmd_ctx->output_handler_priv = connection;
  136. target_register_event_callback(tcl_target_callback_event_handler, connection);
  137. target_register_reset_callback(tcl_target_callback_reset_handler, connection);
  138. target_register_trace_callback(tcl_target_callback_trace_handler, connection);
  139. return ERROR_OK;
  140. }
  141. static int tcl_input(struct connection *connection)
  142. {
  143. Jim_Interp *interp = (Jim_Interp *)connection->cmd_ctx->interp;
  144. int retval;
  145. int i;
  146. ssize_t rlen;
  147. const char *result;
  148. int reslen;
  149. struct tcl_connection *tclc;
  150. unsigned char in[256];
  151. char *tc_line_new;
  152. int tc_line_size_new;
  153. rlen = connection_read(connection, &in, sizeof(in));
  154. if (rlen <= 0) {
  155. if (rlen < 0)
  156. LOG_ERROR("error during read: %s", strerror(errno));
  157. return ERROR_SERVER_REMOTE_CLOSED;
  158. }
  159. tclc = connection->priv;
  160. if (!tclc)
  161. return ERROR_CONNECTION_REJECTED;
  162. /* push as much data into the line as possible */
  163. for (i = 0; i < rlen; i++) {
  164. /* buffer the data */
  165. tclc->tc_line[tclc->tc_lineoffset] = in[i];
  166. if (tclc->tc_lineoffset + 1 < tclc->tc_line_size) {
  167. tclc->tc_lineoffset++;
  168. } else if (tclc->tc_line_size >= TCL_LINE_MAX) {
  169. /* maximum line size reached, drop line */
  170. tclc->tc_linedrop = 1;
  171. } else {
  172. /* grow line buffer: exponential below 1 MB, linear above */
  173. if (tclc->tc_line_size <= 1*1024*1024)
  174. tc_line_size_new = tclc->tc_line_size * 2;
  175. else
  176. tc_line_size_new = tclc->tc_line_size + 1*1024*1024;
  177. if (tc_line_size_new > TCL_LINE_MAX)
  178. tc_line_size_new = TCL_LINE_MAX;
  179. tc_line_new = realloc(tclc->tc_line, tc_line_size_new);
  180. if (!tc_line_new) {
  181. tclc->tc_linedrop = 1;
  182. } else {
  183. tclc->tc_line = tc_line_new;
  184. tclc->tc_line_size = tc_line_size_new;
  185. tclc->tc_lineoffset++;
  186. }
  187. }
  188. /* ctrl-z is end of command. When testing from telnet, just
  189. * press ctrl-z a couple of times first to put telnet into the
  190. * mode where it will send 0x1a in response to pressing ctrl-z
  191. */
  192. if (in[i] != '\x1a')
  193. continue;
  194. /* process the line */
  195. if (tclc->tc_linedrop) {
  196. #define ESTR "line too long\n"
  197. retval = tcl_output(connection, ESTR, sizeof(ESTR));
  198. if (retval != ERROR_OK)
  199. return retval;
  200. #undef ESTR
  201. } else {
  202. tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
  203. command_run_line(connection->cmd_ctx, tclc->tc_line);
  204. result = Jim_GetString(Jim_GetResult(interp), &reslen);
  205. retval = tcl_output(connection, result, reslen);
  206. if (retval != ERROR_OK)
  207. return retval;
  208. /* Always output ctrl-z as end of line to allow multiline results */
  209. tcl_output(connection, "\x1a", 1);
  210. }
  211. tclc->tc_lineoffset = 0;
  212. tclc->tc_linedrop = 0;
  213. }
  214. return ERROR_OK;
  215. }
  216. static int tcl_closed(struct connection *connection)
  217. {
  218. struct tcl_connection *tclc;
  219. tclc = connection->priv;
  220. /* cleanup connection context */
  221. if (tclc) {
  222. free(tclc->tc_line);
  223. free(tclc);
  224. connection->priv = NULL;
  225. }
  226. target_unregister_event_callback(tcl_target_callback_event_handler, connection);
  227. target_unregister_reset_callback(tcl_target_callback_reset_handler, connection);
  228. target_unregister_trace_callback(tcl_target_callback_trace_handler, connection);
  229. return ERROR_OK;
  230. }
  231. int tcl_init(void)
  232. {
  233. if (strcmp(tcl_port, "disabled") == 0) {
  234. LOG_INFO("tcl server disabled");
  235. return ERROR_OK;
  236. }
  237. return add_service("tcl", tcl_port, CONNECTION_LIMIT_UNLIMITED,
  238. &tcl_new_connection, &tcl_input,
  239. &tcl_closed, NULL);
  240. }
  241. COMMAND_HANDLER(handle_tcl_port_command)
  242. {
  243. return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port);
  244. }
  245. COMMAND_HANDLER(handle_tcl_notifications_command)
  246. {
  247. struct connection *connection = NULL;
  248. struct tcl_connection *tclc = NULL;
  249. if (CMD_CTX->output_handler_priv)
  250. connection = CMD_CTX->output_handler_priv;
  251. if (connection && !strcmp(connection->service->name, "tcl")) {
  252. tclc = connection->priv;
  253. return CALL_COMMAND_HANDLER(handle_command_parse_bool, &tclc->tc_notify, "Target Notification output ");
  254. } else {
  255. LOG_ERROR("%s: can only be called from the tcl server", CMD_NAME);
  256. return ERROR_COMMAND_SYNTAX_ERROR;
  257. }
  258. }
  259. COMMAND_HANDLER(handle_tcl_trace_command)
  260. {
  261. struct connection *connection = NULL;
  262. struct tcl_connection *tclc = NULL;
  263. if (CMD_CTX->output_handler_priv)
  264. connection = CMD_CTX->output_handler_priv;
  265. if (connection && !strcmp(connection->service->name, "tcl")) {
  266. tclc = connection->priv;
  267. return CALL_COMMAND_HANDLER(handle_command_parse_bool, &tclc->tc_trace, "Target trace output ");
  268. } else {
  269. LOG_ERROR("%s: can only be called from the tcl server", CMD_NAME);
  270. return ERROR_COMMAND_SYNTAX_ERROR;
  271. }
  272. }
  273. static const struct command_registration tcl_command_handlers[] = {
  274. {
  275. .name = "tcl_port",
  276. .handler = handle_tcl_port_command,
  277. .mode = COMMAND_CONFIG,
  278. .help = "Specify port on which to listen "
  279. "for incoming Tcl syntax. "
  280. "Read help on 'gdb_port'.",
  281. .usage = "[port_num]",
  282. },
  283. {
  284. .name = "tcl_notifications",
  285. .handler = handle_tcl_notifications_command,
  286. .mode = COMMAND_EXEC,
  287. .help = "Target Notification output",
  288. .usage = "[on|off]",
  289. },
  290. {
  291. .name = "tcl_trace",
  292. .handler = handle_tcl_trace_command,
  293. .mode = COMMAND_EXEC,
  294. .help = "Target trace output",
  295. .usage = "[on|off]",
  296. },
  297. COMMAND_REGISTRATION_DONE
  298. };
  299. int tcl_register_commands(struct command_context *cmd_ctx)
  300. {
  301. tcl_port = strdup("6666");
  302. return register_commands(cmd_ctx, NULL, tcl_command_handlers);
  303. }
  304. void tcl_service_free(void)
  305. {
  306. free(tcl_port);
  307. }