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.
 
 
 
 
 
 

441 lines
11 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "log.h"
  30. #include "configuration.h"
  31. #include "time_support.h"
  32. #include "command.h"
  33. #include "server.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <stdarg.h>
  38. #define PRINT_MEM() 0
  39. #if PRINT_MEM()
  40. #include <malloc.h>
  41. #endif
  42. int debug_level = -1;
  43. static FILE* log_output;
  44. static log_callback_t *log_callbacks = NULL;
  45. static long long last_time;
  46. static long long current_time;
  47. static long long start;
  48. static char *log_strings[5] =
  49. {
  50. "User : ",
  51. "Error: ",
  52. "Warn : ", /* want a space after each colon, all same width, colons aligned */
  53. "Info : ",
  54. "Debug: "
  55. };
  56. static int count = 0;
  57. /* The log_puts() serves to somewhat different goals:
  58. *
  59. * - logging
  60. * - feeding low-level info to the user in GDB or Telnet
  61. *
  62. * The latter dictates that strings without newline are not logged, lest there
  63. * will be *MANY log lines when sending one char at the time(e.g.
  64. * target_request.c).
  65. *
  66. */
  67. static void log_puts(enum log_levels level, const char *file, int line, const char *function, const char *string)
  68. {
  69. char *f;
  70. if (level == LOG_LVL_OUTPUT)
  71. {
  72. /* do not prepend any headers, just print out what we were given and return */
  73. fputs(string, log_output);
  74. fflush(log_output);
  75. return;
  76. }
  77. f = strrchr(file, '/');
  78. if (f != NULL)
  79. file = f + 1;
  80. if (strchr(string, '\n')!=NULL)
  81. {
  82. if (debug_level >= LOG_LVL_DEBUG)
  83. {
  84. /* print with count and time information */
  85. int t=(int)(timeval_ms()-start);
  86. #if PRINT_MEM()
  87. struct mallinfo info;
  88. info = mallinfo();
  89. #endif
  90. fprintf(log_output, "%s%d %d %s:%d %s()"
  91. #if PRINT_MEM()
  92. " %d"
  93. #endif
  94. ": %s", log_strings[level+1], count, t, file, line, function,
  95. #if PRINT_MEM()
  96. info.fordblks,
  97. #endif
  98. string);
  99. }
  100. else if(server_use_pipes == 0)
  101. {
  102. /* if we are using gdb through pipes then we do not want any output
  103. * to the pipe otherwise we get repeated strings */
  104. if (strcmp(string, "\n") != 0)
  105. {
  106. /* print human readable output - but skip empty lines */
  107. fprintf(log_output, "%s%s",
  108. (level > LOG_LVL_USER)?log_strings[level+1]:"", string);
  109. }
  110. }
  111. } else
  112. {
  113. /* only entire lines are logged. Otherwise it's
  114. * single chars intended for the log callbacks. */
  115. }
  116. fflush(log_output);
  117. /* Never forward LOG_LVL_DEBUG, too verbose and they can be found in the log if need be */
  118. if (level <= LOG_LVL_INFO)
  119. {
  120. log_callback_t *cb, *next;
  121. cb = log_callbacks;
  122. /* DANGER!!!! the log callback can remove itself!!!! */
  123. while (cb)
  124. {
  125. next=cb->next;
  126. cb->fn(cb->priv, file, line, function, string);
  127. cb=next;
  128. }
  129. }
  130. }
  131. void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
  132. {
  133. char *string;
  134. va_list ap;
  135. count++;
  136. if (level > debug_level)
  137. return;
  138. va_start(ap, format);
  139. string = alloc_vprintf(format, ap);
  140. if (string != NULL)
  141. {
  142. log_puts(level, file, line, function, string);
  143. free(string);
  144. }
  145. va_end(ap);
  146. }
  147. void log_printf_lf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
  148. {
  149. char *string;
  150. va_list ap;
  151. count++;
  152. if (level > debug_level)
  153. return;
  154. va_start(ap, format);
  155. string = alloc_vprintf(format, ap);
  156. if (string != NULL)
  157. {
  158. strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
  159. log_puts(level, file, line, function, string);
  160. free(string);
  161. }
  162. va_end(ap);
  163. }
  164. /* change the current debug level on the fly
  165. * 0: only ERRORS
  166. * 1: + WARNINGS
  167. * 2: + INFORMATIONAL MSGS
  168. * 3: + DEBUG MSGS
  169. */
  170. int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  171. {
  172. if (argc == 0)
  173. command_print(cmd_ctx, "debug_level: %i", debug_level);
  174. if (argc > 0)
  175. debug_level = strtoul(args[0], NULL, 0);
  176. if (debug_level < 0)
  177. debug_level = 0;
  178. if (debug_level > 3)
  179. debug_level = 3;
  180. if (debug_level >= LOG_LVL_DEBUG && server_use_pipes == 1)
  181. {
  182. /* if we are enabling debug info then we need to write to a log file
  183. * otherwise the pipe will get full and cause issues with gdb */
  184. FILE* file = fopen("openocd.log", "w");
  185. if (file)
  186. {
  187. log_output = file;
  188. LOG_WARNING("enabling log output as we are using pipes");
  189. }
  190. }
  191. return ERROR_OK;
  192. }
  193. int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  194. {
  195. if (argc == 1)
  196. {
  197. FILE* file = fopen(args[0], "w");
  198. if (file)
  199. {
  200. log_output = file;
  201. }
  202. }
  203. return ERROR_OK;
  204. }
  205. int log_register_commands(struct command_context_s *cmd_ctx)
  206. {
  207. start = timeval_ms();
  208. register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
  209. COMMAND_ANY, "redirect logging to <file> (default: stderr)");
  210. register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
  211. COMMAND_ANY, "adjust debug level <0-3>");
  212. return ERROR_OK;
  213. }
  214. int log_init(struct command_context_s *cmd_ctx)
  215. {
  216. /* set defaults for daemon configuration, if not set by cmdline or cfgfile */
  217. if (debug_level == -1)
  218. debug_level = LOG_LVL_INFO;
  219. if (log_output == NULL)
  220. {
  221. log_output = stderr;
  222. }
  223. start=last_time=timeval_ms();
  224. return ERROR_OK;
  225. }
  226. int set_log_output(struct command_context_s *cmd_ctx, FILE *output)
  227. {
  228. log_output = output;
  229. return ERROR_OK;
  230. }
  231. /* add/remove log callback handler */
  232. int log_add_callback(log_callback_fn fn, void *priv)
  233. {
  234. log_callback_t *cb;
  235. /* prevent the same callback to be registered more than once, just for sure */
  236. for (cb = log_callbacks; cb; cb = cb->next)
  237. {
  238. if (cb->fn == fn && cb->priv == priv)
  239. return ERROR_INVALID_ARGUMENTS;
  240. }
  241. /* alloc memory, it is safe just to return in case of an error, no need for the caller to check this */
  242. if ((cb = malloc(sizeof(log_callback_t))) == NULL)
  243. return ERROR_BUF_TOO_SMALL;
  244. /* add item to the beginning of the linked list */
  245. cb->fn = fn;
  246. cb->priv = priv;
  247. cb->next = log_callbacks;
  248. log_callbacks = cb;
  249. return ERROR_OK;
  250. }
  251. int log_remove_callback(log_callback_fn fn, void *priv)
  252. {
  253. log_callback_t *cb, **p;
  254. for (p = &log_callbacks; (cb = *p); p = &(*p)->next)
  255. {
  256. if (cb->fn == fn && cb->priv == priv)
  257. {
  258. *p = cb->next;
  259. free(cb);
  260. return ERROR_OK;
  261. }
  262. }
  263. /* no such item */
  264. return ERROR_INVALID_ARGUMENTS;
  265. }
  266. /* return allocated string w/printf() result */
  267. char *alloc_vprintf(const char *fmt, va_list ap)
  268. {
  269. /* no buffer at the beginning, force realloc to do the job */
  270. char *string = NULL;
  271. /* start with buffer size suitable for typical messages */
  272. int size = 128;
  273. for (;;)
  274. {
  275. char *t = string;
  276. va_list ap_copy;
  277. int ret;
  278. string = realloc(string, size);
  279. if (string == NULL)
  280. {
  281. if (t != NULL)
  282. free(t);
  283. return NULL;
  284. }
  285. va_copy(ap_copy, ap);
  286. ret = vsnprintf(string, size, fmt, ap_copy);
  287. /* NB! The result of the vsnprintf() might be an *EMPTY* string! */
  288. if ((ret >= 0) && ((ret + 1) < size))
  289. break;
  290. /* there was just enough or not enough space, allocate more in the next round */
  291. size *= 2; /* double the buffer size */
  292. }
  293. /* the returned buffer is by principle guaranteed to be at least one character longer */
  294. return string;
  295. }
  296. char *alloc_printf(const char *format, ...)
  297. {
  298. char *string;
  299. va_list ap;
  300. va_start(ap, format);
  301. string = alloc_vprintf(format, ap);
  302. va_end(ap);
  303. return string;
  304. }
  305. /* Code must return to the server loop before 1000ms has returned or invoke
  306. * this function.
  307. *
  308. * The GDB connection will time out if it spends >2000ms and you'll get nasty
  309. * error messages from GDB:
  310. *
  311. * Ignoring packet error, continuing...
  312. * Reply contains invalid hex digit 116
  313. *
  314. * While it is possible use "set remotetimeout" to more than the default 2000ms
  315. * in GDB, OpenOCD guarantees that it sends keep-alive packages on the
  316. * GDB protocol and it is a bug in OpenOCD not to either return to the server
  317. * loop or invoke keep_alive() every 1000ms.
  318. *
  319. * This function will send a keep alive packet if >500ms has passed since last time
  320. * it was invoked.
  321. *
  322. * Note that this function can be invoked often, so it needs to be relatively
  323. * fast when invoked more often than every 500ms.
  324. *
  325. */
  326. void keep_alive()
  327. {
  328. current_time=timeval_ms();
  329. if (current_time-last_time>1000)
  330. {
  331. LOG_WARNING("BUG: keep_alive() was not invoked in the 1000ms timelimit. GDB alive packet not sent! (%lld)", current_time-last_time);
  332. }
  333. if (current_time-last_time>500)
  334. {
  335. /* this will keep the GDB connection alive */
  336. LOG_USER_N("%s", "");
  337. /* DANGER!!!! do not add code to invoke e.g. target event processing,
  338. * jim timer processing, etc. it can cause infinite recursion +
  339. * jim event callbacks need to happen at a well defined time,
  340. * not anywhere keep_alive() is invoked.
  341. *
  342. * These functions should be invoked at a well defined spot in server.c
  343. */
  344. last_time=current_time;
  345. }
  346. }
  347. /* reset keep alive timer without sending message */
  348. void kept_alive()
  349. {
  350. current_time=timeval_ms();
  351. last_time=current_time;
  352. }
  353. /* if we sleep for extended periods of time, we must invoke keep_alive() intermittantly */
  354. void alive_sleep(int ms)
  355. {
  356. int i;
  357. int napTime=10;
  358. for (i=0; i<ms; i+=napTime)
  359. {
  360. int sleep_a_bit=ms-i;
  361. if (sleep_a_bit>napTime)
  362. {
  363. sleep_a_bit=napTime;
  364. }
  365. usleep(sleep_a_bit*1000);
  366. keep_alive();
  367. }
  368. }
  369. void busy_sleep(int ms)
  370. {
  371. long long then;
  372. then=timeval_ms();
  373. while ((timeval_ms()-then)<ms)
  374. {
  375. /* busy wait */
  376. }
  377. }