void err_check( uint8_t* file_name, size_t file_name_size, uint8_t* func_name, size_t function_name_size, uint32_t line_number) { trace_write((const char*)file_name, file_name_size); trace_write("\n\r", 2); trace_write((const char*)func_name, function_name_size); trace_write("\n\r", 2); trace_printf("%d", line_number); while(1) { __WFE(); } }
/* Writes a trace message. */ void trace(const char* format, ...) { va_list varg; va_start(varg, format); char message[MESSAGE_MAX_LENGTH]; vsnprintf(message, sizeof(message), format, varg); trace_write(message); va_end(varg); }
int do_try_info(trace_t * trace) { int ret; ret = trace_write (trace); if (ret == 2) return 1; if (ret < 0) return -1; return 0; }
ssize_t _write (int fd __attribute__((unused)), const char* buf /*__attribute__((unused))*/, size_t nbyte /*__attribute__((unused))*/) { #if defined(TRACE) // STDOUT and STDERR are routed to the trace device if (fd == 1 || fd == 2) { return trace_write (buf, nbyte); } #endif // TRACE SendChars(buf, nbyte); return nbyte; // // errno = ENOSYS; // return -1; }
int trace_printf(const char* format, ...) { int ret; va_list ap; va_start (ap, format); static char buf[TRACE_BUFFER_SIZE]; // Print to the local buffer ret = vsnprintf (buf, sizeof(buf), format, ap); if (ret > 0) { // Transfer the buffer to the device ret = trace_write (buf, (size_t)ret); } va_end (ap); return ret; }
int trace_printf(const char* format, ...) { int ret; va_list ap; va_start (ap, format); // TODO: rewrite it to no longer use newlib, it is way too heavy static char buf[OS_INTEGER_TRACE_PRINTF_TMP_ARRAY_SIZE]; // Print to the local buffer ret = vsnprintf (buf, sizeof(buf), format, ap); if (ret > 0) { // Transfer the buffer to the device ret = trace_write (buf, (size_t)ret); } va_end (ap); return ret; }
int pr_trace_vmsg(const char *channel, int level, const char *fmt, va_list msg) { char buf[PR_TUNABLE_BUFFER_SIZE] = {'\0'}; size_t buflen; struct trace_levels *levels; int discard = FALSE; /* Writing a trace message at level zero is NOT helpful; this makes it * impossible to quell messages to that trace channel by setting the level * filter to zero. That being the case, treat level of zero as an invalid * level. */ if (channel == NULL || fmt == NULL || level <= 0) { errno = EINVAL; return -1; } if (trace_tab == NULL) { errno = EPERM; return -1; } levels = trace_get_levels(channel); if (levels == NULL) { discard = TRUE; if (pr_log_event_listening(PR_LOG_TYPE_TRACELOG) <= 0) { return -1; } } if (discard == FALSE && level < levels->min_level) { discard = TRUE; if (pr_log_event_listening(PR_LOG_TYPE_TRACELOG) <= 0) { return 0; } } if (discard == FALSE && level > levels->max_level) { discard = TRUE; if (pr_log_event_listening(PR_LOG_TYPE_TRACELOG) <= 0) { return 0; } } buflen = vsnprintf(buf, sizeof(buf), fmt, msg); /* Always make sure the buffer is NUL-terminated. */ buf[sizeof(buf)-1] = '\0'; /* Trim trailing newlines. */ while (buflen >= 1 && buf[buflen-1] == '\n') { pr_signals_handle(); buf[buflen-1] = '\0'; buflen--; } return trace_write(channel, level, buf, discard); }
void do_try (trace_t * trace, const int hop, const int max_hops, const int max_tries) { fd_set readset; int ret, tries, readonly = 0; struct timeval now, time; double triptime = 0.0; uint32_t prev_addr = 0; printf (" %d ", hop); for (tries = 0; tries < max_tries; tries++) { FD_ZERO (&readset); FD_SET (trace_icmp_sock (trace), &readset); time.tv_sec = TIME_INTERVAL; time.tv_usec = 0; if (!readonly) trace_write (trace); ret = select (FD_SETSIZE, &readset, NULL, NULL, &time); gettimeofday (&now, NULL); now.tv_usec -= trace->tsent.tv_usec; now.tv_sec -= trace->tsent.tv_sec; if (ret < 0) { switch (errno) { case EINTR: /* was interrupted */ break; default: error (EXIT_FAILURE, EPERM, "select failed"); break; } } else if (ret == 0) { /* time expired */ printf (" * "); fflush (stdout); } else { if (FD_ISSET (trace_icmp_sock (trace), &readset)) { triptime = ((double) now.tv_sec) * 1000.0 + ((double) now.tv_usec) / 1000.0; if (trace_read (trace)) { /* FIXME: printf ("Some error ocurred\n"); */ tries--; readonly = 1; continue; } else { if (tries == 0 || prev_addr != trace->from.sin_addr.s_addr) printf (" %s (%s) ", inet_ntoa (trace->from.sin_addr), get_hostname (&trace->from.sin_addr)); printf ("%.3fms ", triptime); } prev_addr = trace->from.sin_addr.s_addr; } } readonly = 0; fflush (stdout); } printf ("\n"); }
int trace_putchar(int c) { trace_write((const char*)&c, 1); return c; }
int trace_puts(const char *s) { trace_write(s, strlen(s)); return trace_write("\n", 1); }
int do_try (trace_t * trace, const int hop, const int max_hops, const int max_tries, const int wait_time, tracert_cfg_item_t *item) { fd_set readset; int ret=0, tries, readonly = 0; struct timeval now, time; double triptime = 0.0; uint32_t prev_addr = 0; item->num = max_tries; if(NULL == (item->time = malloc(sizeof(double)*max_tries))) { return -1; } for (tries = 0; tries < max_tries; tries++) { FD_ZERO (&readset); FD_SET (trace_icmp_sock (trace), &readset); time.tv_sec = wait_time; time.tv_usec = 0; if (!readonly) ret = trace_write (trace); if (ret == 2) return 1; if (ret < 0) return -1; ret = select (FD_SETSIZE, &readset, NULL, NULL, &time); gettimeofday (&now, NULL); now.tv_usec -= trace->tsent.tv_usec; now.tv_sec -= trace->tsent.tv_sec; if (ret < 0) { return -1; } else if (ret == 0) { /* time expired */ strcpy(item->dstIp,"*"); strcpy(item->Ip,"*"); item->time[tries] = 0.000; //fflush (stdout); } else { if (FD_ISSET (trace_icmp_sock (trace), &readset)) { triptime = ((double) now.tv_sec) * 1000.0 + ((double) now.tv_usec) / 1000.0; if (trace_read (trace)) { /* FIXME: printf ("Some error ocurred\n"); */ tries--; readonly = 1; continue; } else { if (tries == 0 || prev_addr != trace->from.sin_addr.s_addr) { strcpy(item->dstIp,inet_ntoa (trace->from.sin_addr)); strcpy(item->Ip,get_hostname (&trace->from.sin_addr)); } item->time[tries] = triptime; } prev_addr = trace->from.sin_addr.s_addr; } } readonly = 0; //fflush (stdout); } return 0; }
int main(int argc, char *argv[]) { int ctx, i, n, done; char command[256]; /* trace flags */ int DBG_GRAPH, DBG_VAR, DBG_RESOLVE, DBG_ACTION, DBG_VM; TRACE_DECLARE_MODULE(test, "test", TRACE_FLAG("graph" , "dependency graph" , &DBG_GRAPH), TRACE_FLAG("var" , "variable handling" , &DBG_VAR), TRACE_FLAG("resolve", "dependency resolving", &DBG_RESOLVE), TRACE_FLAG("action" , "action processing" , &DBG_ACTION), TRACE_FLAG("vm" , "VM execution" , &DBG_VM)); trace_init(); if ((ctx = trace_context_open("test")) < 0) fatal(1, "failed to create test trace context"); if (trace_module_add(ctx, &test) != 0) fatal(1, "failed to add trace module test (%d: %s)", errno, strerror(errno)); info("registered trace module test..."); info("got debug flags:"); info("graph: 0x%x, var: 0x%x, res: 0x%x, act: 0x%x, vm: 0x%x", DBG_GRAPH, DBG_VAR, DBG_RESOLVE, DBG_ACTION, DBG_VM); #define SHOW(n) do { \ printf("%s,%s,%s,%s,%s\n", \ i & 0x01 ? "GRAPH" : "-", \ i & 0x02 ? "VAR" : "-", \ i & 0x04 ? "RESOLVE" : "-", \ i & 0x08 ? "ACTION" : "-", \ i & 0x10 ? "VM" : "-"); \ } while (0) #define FLIP(id, b) \ ((i) & (0x1<<(b)) ? trace_flag_set : trace_flag_clr)(DBG_##id) trace_configure("*.*=+all"); trace_context_enable(ctx); done = FALSE; while (!done) { trace_write(DBG_GRAPH , "DBG_GRAPH"); trace_write(DBG_VAR , "DBG_VAR"); trace_write(DBG_RESOLVE, "DBG_RESOLVE"); trace_write(DBG_ACTION , "DBG_ACTION"); trace_write(DBG_VM , "DBG_VM"); while (!done) { char *nl; printf("trace> "); if (fgets(command, sizeof(command), stdin) == NULL) continue; if ((nl = strchr(command, '\n')) != NULL) *nl = '\0'; if (!strcmp(command, "quit") || !strcmp(command, "exit")) done = TRUE; else if (!strcmp(command, "show")) printf("I would if I could...\n"); else if (!strcmp(command, "test") || !strcmp(command, "run")) break; else if (!strcmp(command, "help")) { printf("Possible commands are:\n"); printf("quit: quit\n"); printf("show: show current trace settings\n"); printf("test: write one test trace message per flag\n"); printf("context.module=[+|-]f1,...: change trace flags\n"); printf("context > file: redirect trace context to file\n"); } else trace_configure(command); } } if (trace_module_del(ctx, "test") != 0) fatal(1, "failed to remove trace module %s (%d: %s)", "test", errno, strerror(errno)); info("test trace module %s removed...", "test"); return 0; }
/* Write/send (the next part of) a msg (fragment). */ static void tcp_write_message_fragment (IP_CX *cxp) { ssize_t n, max; unsigned char *sp; TCP_DATA *dp; ctrc_printd (TCPS_ID, TCPS_TX_FRAG, &cxp->fd, sizeof (cxp->fd)); if ((dp = cxp->sproto) == NULL) { warn_printf ("tcp_write_message_fragment: no TCP context!"); return; } if (!dp->send_msg) { warn_printf ("tcp_write_message_fragment: no send_msg context!"); #ifdef TRACE_POLL_EVENTS log_printf (RTPS_ID, 0, "TLS: POLLOUT = 0 [%d]\r\n", cxp->fd); #endif sock_fd_event_socket (cxp->fd, POLLOUT, 0); return; } sp = dp->send_msg->buffer + dp->send_msg->used; while (dp->send_msg->used < dp->send_msg->size) { max = dp->send_msg->size - dp->send_msg->used; #ifdef __APPLE__ /* Apple does not support MSG_NOSIGNAL, therefore we set the equivalent Apple-specific socket option SO_NOSIGPIPE. */ n = send (cxp->fd, sp, max, 0); #else /* We're using send() here iso write() so that we can indicate to the kernel *not* to send a SIGPIPE in case the other already closed this connection. A return code of -1 and ERRNO to EPIPE is given instead */ n = send (cxp->fd, sp, max, MSG_NOSIGNAL); #endif ctrc_begind (TCPS_ID, TCPS_TX_CHUNK, &cxp->fd, sizeof (cxp->fd)); ctrc_contd (&max, sizeof (max)); ctrc_contd (&n, sizeof (n)); ctrc_endd (); trace_write (n, cxp->fd, max); if (n < 0) { if (ERRNO == EINTR) continue; /* immediately try again */ if ((ERRNO == EAGAIN) || (ERRNO == EWOULDBLOCK)) return; /* Wait for poll() to indicate that we can write another chunk. */ /*perror ("tcp_write_message_fragment");*/ log_printf (RTPS_ID, 0, "%s: error sending data on [%d] (%s).\r\n", __FUNCTION__, cxp->fd, strerror (ERRNO)); xfree (dp->send_msg->buffer); xfree (dp->send_msg); dp->send_msg = NULL; tcp_cleanup_ctx (cxp); return; } dp->send_msg->used += n; sp += n; } xfree (dp->send_msg->buffer); xfree (dp->send_msg); dp->send_msg = NULL; #ifdef TRACE_POLL_EVENTS log_printf (RTPS_ID, 0, "TLS: POLLOUT = 0 [%d]\r\n", cxp->fd); #endif sock_fd_event_socket (cxp->fd, POLLOUT, 0); ctrc_printd (TCPS_ID, TCPS_TX_COMPL, &cxp->fd, sizeof (cxp->fd)); if (cxp->stream_cb->on_write_completed) cxp->stream_cb->on_write_completed (cxp); else if (cxp->paired && cxp->paired->fd == cxp->fd && cxp->paired->stream_cb->on_write_completed) cxp->paired->stream_cb->on_write_completed (cxp); }