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.
 
 
 
 
 
 

364 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 == NULL)
  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 == NULL) {
  127. free(tclc);
  128. return ERROR_CONNECTION_REJECTED;
  129. }
  130. connection->priv = tclc;
  131. struct target *target = get_target_by_num(connection->cmd_ctx->current_target);
  132. if (target != NULL)
  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;
  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. interp = connection->cmd_ctx->interp->interp;
  154. rlen = connection_read(connection, &in, sizeof(in));
  155. if (rlen <= 0) {
  156. if (rlen < 0)
  157. LOG_ERROR("error during read: %s", strerror(errno));
  158. return ERROR_SERVER_REMOTE_CLOSED;
  159. }
  160. tclc = connection->priv;
  161. if (tclc == NULL)
  162. return ERROR_CONNECTION_REJECTED;
  163. /* push as much data into the line as possible */
  164. for (i = 0; i < rlen; i++) {
  165. /* buffer the data */
  166. tclc->tc_line[tclc->tc_lineoffset] = in[i];
  167. if (tclc->tc_lineoffset < tclc->tc_line_size) {
  168. tclc->tc_lineoffset++;
  169. } else if (tclc->tc_line_size >= TCL_LINE_MAX) {
  170. /* maximum line size reached, drop line */
  171. tclc->tc_linedrop = 1;
  172. } else {
  173. /* grow line buffer: exponential below 1 MB, linear above */
  174. if (tclc->tc_line_size <= 1*1024*1024)
  175. tc_line_size_new = tclc->tc_line_size * 2;
  176. else
  177. tc_line_size_new = tclc->tc_line_size + 1*1024*1024;
  178. if (tc_line_size_new > TCL_LINE_MAX)
  179. tc_line_size_new = TCL_LINE_MAX;
  180. tc_line_new = realloc(tclc->tc_line, tc_line_size_new);
  181. if (tc_line_new == NULL) {
  182. tclc->tc_linedrop = 1;
  183. } else {
  184. tclc->tc_line = tc_line_new;
  185. tclc->tc_line_size = tc_line_size_new;
  186. tclc->tc_lineoffset++;
  187. }
  188. }
  189. /* ctrl-z is end of command. When testing from telnet, just
  190. * press ctrl-z a couple of times first to put telnet into the
  191. * mode where it will send 0x1a in response to pressing ctrl-z
  192. */
  193. if (in[i] != '\x1a')
  194. continue;
  195. /* process the line */
  196. if (tclc->tc_linedrop) {
  197. #define ESTR "line too long\n"
  198. retval = tcl_output(connection, ESTR, sizeof(ESTR));
  199. if (retval != ERROR_OK)
  200. return retval;
  201. #undef ESTR
  202. } else {
  203. tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
  204. command_run_line(connection->cmd_ctx, tclc->tc_line);
  205. result = Jim_GetString(Jim_GetResult(interp), &reslen);
  206. retval = tcl_output(connection, result, reslen);
  207. if (retval != ERROR_OK)
  208. return retval;
  209. /* Always output ctrl-d as end of line to allow multiline results */
  210. tcl_output(connection, "\x1a", 1);
  211. }
  212. tclc->tc_lineoffset = 0;
  213. tclc->tc_linedrop = 0;
  214. }
  215. return ERROR_OK;
  216. }
  217. static int tcl_closed(struct connection *connection)
  218. {
  219. struct tcl_connection *tclc;
  220. tclc = connection->priv;
  221. /* cleanup connection context */
  222. if (tclc) {
  223. free(tclc->tc_line);
  224. free(tclc);
  225. connection->priv = NULL;
  226. }
  227. target_unregister_event_callback(tcl_target_callback_event_handler, connection);
  228. target_unregister_reset_callback(tcl_target_callback_reset_handler, connection);
  229. target_unregister_trace_callback(tcl_target_callback_trace_handler, connection);
  230. return ERROR_OK;
  231. }
  232. int tcl_init(void)
  233. {
  234. if (strcmp(tcl_port, "disabled") == 0) {
  235. LOG_INFO("tcl server disabled");
  236. return ERROR_OK;
  237. }
  238. return add_service("tcl", tcl_port, CONNECTION_LIMIT_UNLIMITED,
  239. &tcl_new_connection, &tcl_input,
  240. &tcl_closed, NULL);
  241. }
  242. COMMAND_HANDLER(handle_tcl_port_command)
  243. {
  244. return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port);
  245. }
  246. COMMAND_HANDLER(handle_tcl_notifications_command)
  247. {
  248. struct connection *connection = NULL;
  249. struct tcl_connection *tclc = NULL;
  250. if (CMD_CTX->output_handler_priv != NULL)
  251. connection = CMD_CTX->output_handler_priv;
  252. if (connection != NULL && !strcmp(connection->service->name, "tcl")) {
  253. tclc = connection->priv;
  254. return CALL_COMMAND_HANDLER(handle_command_parse_bool, &tclc->tc_notify, "Target Notification output ");
  255. } else {
  256. LOG_ERROR("%s: can only be called from the tcl server", CMD_NAME);
  257. return ERROR_COMMAND_SYNTAX_ERROR;
  258. }
  259. }
  260. COMMAND_HANDLER(handle_tcl_trace_command)
  261. {
  262. struct connection *connection = NULL;
  263. struct tcl_connection *tclc = NULL;
  264. if (CMD_CTX->output_handler_priv != NULL)
  265. connection = CMD_CTX->output_handler_priv;
  266. if (connection != NULL && !strcmp(connection->service->name, "tcl")) {
  267. tclc = connection->priv;
  268. return CALL_COMMAND_HANDLER(handle_command_parse_bool, &tclc->tc_trace, "Target trace output ");
  269. } else {
  270. LOG_ERROR("%s: can only be called from the tcl server", CMD_NAME);
  271. return ERROR_COMMAND_SYNTAX_ERROR;
  272. }
  273. }
  274. static const struct command_registration tcl_command_handlers[] = {
  275. {
  276. .name = "tcl_port",
  277. .handler = handle_tcl_port_command,
  278. .mode = COMMAND_ANY,
  279. .help = "Specify port on which to listen "
  280. "for incoming Tcl syntax. "
  281. "Read help on 'gdb_port'.",
  282. .usage = "[port_num]",
  283. },
  284. {
  285. .name = "tcl_notifications",
  286. .handler = handle_tcl_notifications_command,
  287. .mode = COMMAND_EXEC,
  288. .help = "Target Notification output",
  289. .usage = "[on|off]",
  290. },
  291. {
  292. .name = "tcl_trace",
  293. .handler = handle_tcl_trace_command,
  294. .mode = COMMAND_EXEC,
  295. .help = "Target trace output",
  296. .usage = "[on|off]",
  297. },
  298. COMMAND_REGISTRATION_DONE
  299. };
  300. int tcl_register_commands(struct command_context *cmd_ctx)
  301. {
  302. tcl_port = strdup("6666");
  303. return register_commands(cmd_ctx, NULL, tcl_command_handlers);
  304. }