This is an automated email from Gerrit.

Maxim Karaoglanov (m...@bbt.ru) just uploaded a new patch set to Gerrit, which 
you can find at http://openocd.zylin.com/1697

-- gerrit

commit 5f66cfbfeafcd1d2fbb6e4e87ac801c60cb84c69
Author: mmk <m...@bbt.ru>
Date:   Tue Oct 15 01:06:08 2013 +0400

    topic: Added trace server for stlink2
    
    Added output trace information via TCP server (before, output only to file).
    Added the following parameters:
      trace_port (by default 7777)
      init_ITM [enable | disable] if "disable" - turns off initialization ITM 
registers (default - "enable".
    Required when you want special options of serialization or access rights.
    External application can independently configure the ITM via telnet.
      trace_file filename - if not defined - no output to file
      trace_hz 168000000 CPU speed for correct serializer devider
    Deleted the following parameter:
      trace - now file name defined in trace_file
    
    Change-Id: I829a3bafb085a855b291eab2968961d5a3d1a166
    Signed-off-by: Maxim Karaoglanov <m...@bbt.ru>

diff --git a/src/jtag/drivers/stlink_usb.c b/src/jtag/drivers/stlink_usb.c
index 31f08cb..22afe56 100644
--- a/src/jtag/drivers/stlink_usb.c
+++ b/src/jtag/drivers/stlink_usb.c
@@ -38,6 +38,7 @@
 #include <target/cortex_m.h>
 
 #include "libusb_common.h"
+#include "server/trace_server.h"
 
 #define ENDPOINT_IN  0x80
 #define ENDPOINT_OUT 0x00
@@ -56,7 +57,7 @@
 
 /* the current implementation of the stlink limits
  * 8bit read/writes to max 64 bytes. */
-#define STLINK_MAX_RW8         (64)
+#define STLINK_MAX_RW8          (64)
 
 enum stlink_jtag_api_version {
        STLINK_JTAG_API_V1 = 1,
@@ -107,6 +108,8 @@ struct stlink_usb_handle_s {
                bool enabled;
                /** trace data destination file */
                FILE *output_f;
+               /** */
+               bool init_ITM;
                /** trace module source clock (for prescaler) */
                uint32_t source_hz;
                /** trace module clock prescaler */
@@ -251,9 +254,8 @@ static int stlink_usb_xfer_rw(void *handle, int cmdsize, 
const uint8_t *buf, int
        h = (struct stlink_usb_handle_s *)handle;
 
        if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, 
cmdsize,
-                       STLINK_WRITE_TIMEOUT) != cmdsize) {
+                       STLINK_WRITE_TIMEOUT) != cmdsize)
                return ERROR_FAIL;
-       }
 
        if (h->direction == STLINK_TX_EP && size) {
                if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
@@ -369,7 +371,7 @@ static void stlink_usb_xfer_v1_create_cmd(void *handle, 
uint8_t direction, uint3
        buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
        h->cmdidx += 4;
        h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : 
ENDPOINT_OUT);
-       h->cmdbuf[h->cmdidx++] = 0; /* lun */
+       h->cmdbuf[h->cmdidx++] = 0;     /* lun */
        h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
 }
 
@@ -391,7 +393,7 @@ static void stlink_usb_init_buffer(void *handle, uint8_t 
direction, uint32_t siz
                stlink_usb_xfer_v1_create_cmd(handle, direction, size);
 }
 
-static const char * const stlink_usb_error_msg[] = {
+static const char *const stlink_usb_error_msg[] = {
        "unknown"
 };
 
@@ -855,8 +857,10 @@ static void stlink_usb_trace_read(void *handle)
 
                                res = stlink_usb_read_trace(handle, buf, size);
                                if (res == ERROR_OK) {
-                                       /* Log retrieved trace output */
-                                       if (fwrite(buf, 1, size, 
h->trace.output_f) > 0)
+                                       trace_server_output_all(buf, size);
+                                       /* Log retrieved trace output to file */
+                                       if ((h->trace.output_f != NULL) &&
+                                               (fwrite(buf, 1, size, 
h->trace.output_f) > 0))
                                                fflush(h->trace.output_f);
                                }
                        }
@@ -1068,9 +1072,13 @@ static int stlink_usb_trace_enable(void *handle)
        if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
                uint32_t trace_hz;
 
-               res = stlink_configure_target_trace_port(handle);
-               if (res != ERROR_OK)
-                       LOG_ERROR("Unable to configure tracing on target\n");
+               if (h->trace.init_ITM) {
+                       res = stlink_configure_target_trace_port(handle);
+                       if (res != ERROR_OK)
+                               LOG_ERROR("Unable to configure tracing on 
target\n");
+                       else
+                               LOG_INFO("ITM initialized");
+               }
 
                trace_hz = h->trace.prescale > 0 ?
                        h->trace.source_hz / (h->trace.prescale + 1) :
@@ -1087,7 +1095,7 @@ static int stlink_usb_trace_enable(void *handle)
 
                res = stlink_usb_xfer(handle, h->databuf, 2);
 
-               if (res == ERROR_OK)  {
+               if (res == ERROR_OK) {
                        h->trace.enabled = true;
                        LOG_DEBUG("Tracing: recording at %uHz\n", trace_hz);
                }
@@ -1111,9 +1119,7 @@ static int stlink_usb_run(void *handle)
 
        if (h->jtag_api == STLINK_JTAG_API_V2) {
                res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, 
DBGKEY|C_DEBUGEN);
-
-               /* Try to start tracing, if requested */
-               if (res == ERROR_OK && h->trace.output_f) {
+               if (res == ERROR_OK) {
                        if (stlink_usb_trace_enable(handle) == ERROR_OK)
                                LOG_DEBUG("Tracing: enabled\n");
                        else
@@ -1315,7 +1321,7 @@ static int stlink_usb_get_rw_status(void *handle)
 
 /** */
 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
-                         uint8_t *buffer)
+       uint8_t *buffer)
 {
        int res;
        uint16_t read_len = len;
@@ -1356,7 +1362,7 @@ static int stlink_usb_read_mem8(void *handle, uint32_t 
addr, uint16_t len,
 
 /** */
 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
-                          const uint8_t *buffer)
+       const uint8_t *buffer)
 {
        int res;
        struct stlink_usb_handle_s *h;
@@ -1390,7 +1396,7 @@ static int stlink_usb_write_mem8(void *handle, uint32_t 
addr, uint16_t len,
 
 /** */
 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
-                         uint8_t *buffer)
+       uint8_t *buffer)
 {
        int res;
        struct stlink_usb_handle_s *h;
@@ -1426,7 +1432,7 @@ static int stlink_usb_read_mem32(void *handle, uint32_t 
addr, uint16_t len,
 
 /** */
 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
-                          const uint8_t *buffer)
+       const uint8_t *buffer)
 {
        int res;
        struct stlink_usb_handle_s *h;
@@ -1467,7 +1473,7 @@ static uint32_t stlink_max_block_size(uint32_t 
tar_autoincr_block, uint32_t addr
 }
 
 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
-               uint32_t count, uint8_t *buffer)
+       uint32_t count, uint8_t *buffer)
 {
        int retval = ERROR_OK;
        uint32_t bytes_remaining;
@@ -1478,8 +1484,8 @@ static int stlink_usb_read_mem(void *handle, uint32_t 
addr, uint32_t size,
 
        while (count) {
 
-               bytes_remaining = (size == 4) ? \
-                               stlink_max_block_size(h->max_mem_packet, addr) 
: STLINK_MAX_RW8;
+               bytes_remaining = (size == 4) ? \
+                       stlink_max_block_size(h->max_mem_packet, addr) : 
STLINK_MAX_RW8;
 
                if (count < bytes_remaining)
                        bytes_remaining = count;
@@ -1510,9 +1516,16 @@ static int stlink_usb_read_mem(void *handle, uint32_t 
addr, uint32_t size,
                        }
 
                        if (bytes_remaining % 4)
-                               retval = stlink_usb_read_mem(handle, addr, 1, 
bytes_remaining, buffer);
+                               retval = stlink_usb_read_mem(handle,
+                                       addr,
+                                       1,
+                                       bytes_remaining,
+                                       buffer);
                        else
-                               retval = stlink_usb_read_mem32(handle, addr, 
bytes_remaining, buffer);
+                               retval = stlink_usb_read_mem32(handle,
+                                       addr,
+                                       bytes_remaining,
+                                       buffer);
                } else
                        retval = stlink_usb_read_mem8(handle, addr, 
bytes_remaining, buffer);
 
@@ -1528,7 +1541,7 @@ static int stlink_usb_read_mem(void *handle, uint32_t 
addr, uint32_t size,
 }
 
 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
-               uint32_t count, const uint8_t *buffer)
+       uint32_t count, const uint8_t *buffer)
 {
        int retval = ERROR_OK;
        uint32_t bytes_remaining;
@@ -1539,8 +1552,8 @@ static int stlink_usb_write_mem(void *handle, uint32_t 
addr, uint32_t size,
 
        while (count) {
 
-               bytes_remaining = (size == 4) ? \
-                               stlink_max_block_size(h->max_mem_packet, addr) 
: STLINK_MAX_RW8;
+               bytes_remaining = (size == 4) ? \
+                       stlink_max_block_size(h->max_mem_packet, addr) : 
STLINK_MAX_RW8;
 
                if (count < bytes_remaining)
                        bytes_remaining = count;
@@ -1571,9 +1584,16 @@ static int stlink_usb_write_mem(void *handle, uint32_t 
addr, uint32_t size,
                        }
 
                        if (bytes_remaining % 4)
-                               retval = stlink_usb_write_mem(handle, addr, 1, 
bytes_remaining, buffer);
+                               retval = stlink_usb_write_mem(handle,
+                                       addr,
+                                       1,
+                                       bytes_remaining,
+                                       buffer);
                        else
-                               retval = stlink_usb_write_mem32(handle, addr, 
bytes_remaining, buffer);
+                               retval = stlink_usb_write_mem32(handle,
+                                       addr,
+                                       bytes_remaining,
+                                       buffer);
 
                } else
                        retval = stlink_usb_write_mem8(handle, addr, 
bytes_remaining, buffer);
@@ -1651,21 +1671,21 @@ static int stlink_usb_open(struct hl_interface_param_s 
*param, void **fd)
 
                /* wrap version for first read */
                switch (param->pid) {
-               case 0x3744:
-                       h->version.stlink = 1;
-                       break;
-               default:
-                       h->version.stlink = 2;
-                       break;
+                       case 0x3744:
+                               h->version.stlink = 1;
+                               break;
+                       default:
+                               h->version.stlink = 2;
+                               break;
                }
 
                /* get the device version */
                err = stlink_usb_version(h);
 
-               if (err == ERROR_OK) {
+               if (err == ERROR_OK)
                        break;
-               } else if (h->version.stlink == 1 ||
-                          retry_count == 0) {
+               else if (h->version.stlink == 1 ||
+                       retry_count == 0) {
                        LOG_ERROR("read version failed");
                        goto error_open;
                } else {
@@ -1732,15 +1752,15 @@ static int stlink_usb_open(struct hl_interface_param_s 
*param, void **fd)
        /* set the used jtag api, this will default to the newest supported 
version */
        h->jtag_api = api;
 
-       if (h->jtag_api >= 2 && param->trace_f && param->trace_source_hz > 0) {
+       if (h->jtag_api >= 2 && param->trace_source_hz > 0) {
                uint32_t prescale;
 
                prescale = param->trace_source_hz > STLINK_TRACE_MAX_HZ ?
                        (param->trace_source_hz / STLINK_TRACE_MAX_HZ) - 1 : 0;
-
-               h->trace.output_f = param->trace_f;
                h->trace.source_hz = param->trace_source_hz;
                h->trace.prescale = prescale;
+               h->trace.init_ITM = param->trace_init_ITM;
+               h->trace.output_f = param->trace_f;
        }
 
        /* initialize the debug hardware */
diff --git a/src/jtag/hla/hla_interface.c b/src/jtag/hla/hla_interface.c
index 00e4549..6527ec7 100644
--- a/src/jtag/hla/hla_interface.c
+++ b/src/jtag/hla/hla_interface.c
@@ -37,7 +37,7 @@
 
 #include <target/target.h>
 
-static struct hl_interface_s hl_if = { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN, 
false, NULL, 0}, 0, 0 };
+static struct hl_interface_s hl_if = { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN, 
false, NULL, true, 0}, 0, 0 };
 
 int hl_interface_open(enum hl_transports tr)
 {
@@ -85,7 +85,7 @@ int hl_interface_init_target(struct target *t)
 
                /* treat "-expected-id 0" as a "don't-warn" wildcard */
                if (!expected || !t->tap->idcode ||
-                   (t->tap->idcode == expected)) {
+                       (t->tap->idcode == expected)) {
                        found = 1;
                        break;
                }
@@ -93,7 +93,7 @@ int hl_interface_init_target(struct target *t)
 
        if (found == 0) {
                LOG_ERROR("hl_interface_init_target: target not found: idcode: 
0x%08x",
-                               t->tap->idcode);
+                       t->tap->idcode);
                return ERROR_FAIL;
        }
 
@@ -114,12 +114,10 @@ static int hl_interface_init(void)
 static int hl_interface_quit(void)
 {
        LOG_DEBUG("hl_interface_quit");
-
        if (hl_if.param.trace_f) {
                fclose(hl_if.param.trace_f);
                hl_if.param.trace_f = NULL;
        }
-
        return ERROR_OK;
 }
 
@@ -146,11 +144,10 @@ COMMAND_HANDLER(hl_interface_handle_device_desc_command)
 {
        LOG_DEBUG("hl_interface_handle_device_desc_command");
 
-       if (CMD_ARGC == 1) {
+       if (CMD_ARGC == 1)
                hl_if.param.device_desc = strdup(CMD_ARGV[0]);
-       } else {
+       else
                LOG_ERROR("expected exactly one argument to hl_device_desc 
<description>");
-       }
 
        return ERROR_OK;
 }
@@ -159,11 +156,10 @@ COMMAND_HANDLER(hl_interface_handle_serial_command)
 {
        LOG_DEBUG("hl_interface_handle_serial_command");
 
-       if (CMD_ARGC == 1) {
+       if (CMD_ARGC == 1)
                hl_if.param.serial = strdup(CMD_ARGV[0]);
-       } else {
+       else
                LOG_ERROR("expected exactly one argument to hl_serial 
<serial-number>");
-       }
 
        return ERROR_OK;
 }
@@ -179,13 +175,13 @@ COMMAND_HANDLER(hl_interface_handle_layout_command)
 
        if (hl_if.layout) {
                LOG_ERROR("already specified hl_layout %s",
-                               hl_if.layout->name);
+                       hl_if.layout->name);
                return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
-                   ? ERROR_FAIL : ERROR_OK;
+                      ? ERROR_FAIL : ERROR_OK;
        }
 
        for (const struct hl_layout *l = hl_layout_get_list(); l->name;
-            l++) {
+               l++) {
                if (strcmp(l->name, CMD_ARGV[0]) == 0) {
                        hl_if.layout = l;
                        return ERROR_OK;
@@ -226,73 +222,105 @@ COMMAND_HANDLER(stlink_interface_handle_api_command)
        return ERROR_OK;
 }
 
-COMMAND_HANDLER(interface_handle_trace_command)
+COMMAND_HANDLER(interface_handle_trace_hz_command)
 {
-       FILE *f;
        unsigned source_hz;
 
-       if (CMD_ARGC != 2)
+       if (CMD_ARGC != 1)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       f = fopen(CMD_ARGV[0], "a");
-       if (!f)
+       COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], source_hz);
+       if (source_hz == 0)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], source_hz);
-       if (source_hz == 0) {
-               fclose(f);
+       hl_if.param.trace_source_hz = source_hz;
+
+       return ERROR_OK;
+}
+
+COMMAND_HANDLER(interface_handle_init_ITM_command)
+{
+       bool enable;
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
+       hl_if.param.trace_init_ITM = enable;
+       return ERROR_OK;
+}
+
+COMMAND_HANDLER(interface_handle_trace_file_command)
+{
+       FILE *f;
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       f = fopen(CMD_ARGV[0], "a");
+       if (!f)
                return ERROR_COMMAND_SYNTAX_ERROR;
-       }
 
        hl_if.param.trace_f = f;
-       hl_if.param.trace_source_hz = source_hz;
 
        return ERROR_OK;
 }
 
+
 static const struct command_registration hl_interface_command_handlers[] = {
        {
-        .name = "hla_device_desc",
-        .handler = &hl_interface_handle_device_desc_command,
-        .mode = COMMAND_CONFIG,
-        .help = "set the a device description of the adapter",
-        .usage = "description_string",
-        },
+               .name = "hla_device_desc",
+               .handler = &hl_interface_handle_device_desc_command,
+               .mode = COMMAND_CONFIG,
+               .help = "set the a device description of the adapter",
+               .usage = "description_string",
+       },
+       {
+               .name = "hla_serial",
+               .handler = &hl_interface_handle_serial_command,
+               .mode = COMMAND_CONFIG,
+               .help = "set the serial number of the adapter",
+               .usage = "serial_string",
+       },
+       {
+               .name = "hla_layout",
+               .handler = &hl_interface_handle_layout_command,
+               .mode = COMMAND_CONFIG,
+               .help = "set the layout of the adapter",
+               .usage = "layout_name",
+       },
+       {
+               .name = "hla_vid_pid",
+               .handler = &hl_interface_handle_vid_pid_command,
+               .mode = COMMAND_CONFIG,
+               .help = "the vendor and product ID of the adapter",
+               .usage = "(vid pid)* ",
+       },
+       {
+               .name = "stlink_api",
+               .handler = &stlink_interface_handle_api_command,
+               .mode = COMMAND_CONFIG,
+               .help = "set the desired stlink api level",
+               .usage = "api version 1 or 2",
+       },
        {
-        .name = "hla_serial",
-        .handler = &hl_interface_handle_serial_command,
-        .mode = COMMAND_CONFIG,
-        .help = "set the serial number of the adapter",
-        .usage = "serial_string",
-        },
+               .name = "trace_hz",
+               .handler = &interface_handle_trace_hz_command,
+               .mode = COMMAND_CONFIG,
+               .help = "configure trace cpu speed",
+               .usage = "source_lock_hz",
+       },
        {
-        .name = "hla_layout",
-        .handler = &hl_interface_handle_layout_command,
-        .mode = COMMAND_CONFIG,
-        .help = "set the layout of the adapter",
-        .usage = "layout_name",
-        },
+               .name = "trace_file",
+               .handler = &interface_handle_trace_file_command,
+               .mode = COMMAND_CONFIG,
+               .help = "configure trace reception file name",
+               .usage = "destination_path",
+       },
        {
-        .name = "hla_vid_pid",
-        .handler = &hl_interface_handle_vid_pid_command,
-        .mode = COMMAND_CONFIG,
-        .help = "the vendor and product ID of the adapter",
-        .usage = "(vid pid)* ",
-        },
-        {
-        .name = "stlink_api",
-        .handler = &stlink_interface_handle_api_command,
-        .mode = COMMAND_CONFIG,
-        .help = "set the desired stlink api level",
-        .usage = "api version 1 or 2",
-        },
-        {
-        .name = "trace",
-        .handler = &interface_handle_trace_command,
-        .mode = COMMAND_CONFIG,
-        .help = "configure trace reception",
-        .usage = "destination_path source_lock_hz",
-        },
+               .name = "init_ITM",
+               .handler = &interface_handle_init_ITM_command,
+               .mode = COMMAND_CONFIG,
+               .help = "configure initialisation ITM",
+               .usage = "enable|disable",
+       },
        COMMAND_REGISTRATION_DONE
 };
 
diff --git a/src/jtag/hla/hla_interface.h b/src/jtag/hla/hla_interface.h
index fcf1d9e..3749bab 100644
--- a/src/jtag/hla/hla_interface.h
+++ b/src/jtag/hla/hla_interface.h
@@ -24,6 +24,7 @@
 #ifndef _HL_INTERFACE
 #define _HL_INTERFACE
 
+
 /** */
 struct target;
 /** */
@@ -48,6 +49,8 @@ struct hl_interface_param_s {
        bool connect_under_reset;
        /** Output file for trace data (if any) */
        FILE *trace_f;
+       /** */
+       bool trace_init_ITM;
        /** Trace module source clock rate */
        uint32_t trace_source_hz;
 };
@@ -68,4 +71,4 @@ int hl_interface_open(enum hl_transports tr);
 int hl_interface_init_target(struct target *t);
 int hl_interface_init_reset(void);
 
-#endif /* _HL_INTERFACE */
+#endif /* _HL_INTERFACE */
diff --git a/src/server/Makefile.am b/src/server/Makefile.am
index 04d3035..0c3bfc6 100644
--- a/src/server/Makefile.am
+++ b/src/server/Makefile.am
@@ -17,6 +17,11 @@ endif
 noinst_HEADERS += tcl_server.h
 libserver_la_SOURCES += tcl_server.c
 
+# trace server addons
+noinst_HEADERS += trace_server.h
+libserver_la_SOURCES += trace_server.c
+
+
 EXTRA_DIST = \
        startup.tcl
 
diff --git a/src/server/server.c b/src/server/server.c
index 01e8441..a898c70 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -2,7 +2,7 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   dominic.r...@gmx.de                                                   *
  *                                                                         *
- *   Copyright (C) 2007-2010 Øyvind Harboe                                 *
+ *   Copyright (C) 2007-2010 yvind Harboe                                 *
  *   oyvind.har...@zylin.com                                               *
  *                                                                         *
  *   Copyright (C) 2008 by Spencer Oliver                                  *
@@ -34,6 +34,7 @@
 #include "openocd.h"
 #include "tcl_server.h"
 #include "telnet_server.h"
+#include "trace_server.h"
 
 #include <signal.h>
 
@@ -437,7 +438,7 @@ int server_loop(struct command_context *command_context)
                for (service = services; service; service = service->next) {
                        /* handle new connections on listeners */
                        if ((service->fd != -1)
-                           && (FD_ISSET(service->fd, &read_fds))) {
+                               && (FD_ISSET(service->fd, &read_fds))) {
                                if (service->max_connections > 0)
                                        add_connection(service, 
command_context);
                                else {
@@ -446,8 +447,8 @@ int server_loop(struct command_context *command_context)
                                                socklen_t address_size = 
sizeof(sin);
                                                int tmp_fd;
                                                tmp_fd = accept(service->fd,
-                                                               (struct 
sockaddr *)&service->sin,
-                                                               &address_size);
+                                                       (struct sockaddr 
*)&service->sin,
+                                                       &address_size);
                                                close_socket(tmp_fd);
                                        }
                                        LOG_INFO(
@@ -466,7 +467,8 @@ int server_loop(struct command_context *command_context)
                                                if (retval != ERROR_OK) {
                                                        struct connection *next 
= c->next;
                                                        if (service->type == 
CONNECTION_PIPE ||
-                                                                       
service->type == CONNECTION_STDINOUT) {
+                                                               service->type ==
+                                                               
CONNECTION_STDINOUT) {
                                                                /* if 
connection uses a pipe then
                                                                 * shutdown 
openocd on error */
                                                                
shutdown_openocd = 1;
@@ -543,6 +545,10 @@ int server_init(struct command_context *cmd_ctx)
        if (ERROR_OK != ret)
                return ret;
 
+       ret = trace_server_init();
+       if (ERROR_OK != ret)
+               return ret;
+
        return telnet_init("Open On-Chip Debugger");
 }
 
@@ -609,6 +615,10 @@ int server_register_commands(struct command_context 
*cmd_ctx)
        if (ERROR_OK != retval)
                return retval;
 
+       retval = trace_server_register_commands(cmd_ctx);
+       if (ERROR_OK != retval)
+               return retval;
+
        return register_commands(cmd_ctx, NULL, server_command_handlers);
 }
 
@@ -653,3 +663,18 @@ SERVER_PIPE_COMMAND()
        }
        return ERROR_OK;
 }
+
+
+struct service *find_by_name(char *name)
+{
+       struct service *found = NULL;
+       struct service *s = services;
+       while (s) {
+               if (strcmp(s->name, name) == 0) {
+                       found = s;
+                       break;
+               }
+               s = s->next;
+       }
+       return found;
+}
diff --git a/src/server/server.h b/src/server/server.h
index e6a9195..3276d71 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -2,7 +2,7 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   dominic.r...@gmx.de                                                   *
  *                                                                         *
- *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
+ *   Copyright (C) 2007,2008 yvind Harboe                                 *
  *   oyvind.har...@zylin.com                                               *
  *                                                                         *
  *   Copyright (C) 2008 by Spencer Oliver                                  *
@@ -71,9 +71,11 @@ struct service {
 };
 
 int add_service(char *name, const char *port,
-               int max_connections, new_connection_handler_t 
new_connection_handler,
-               input_handler_t in_handler, connection_closed_handler_t 
close_handler,
-               void *priv);
+       int max_connections, new_connection_handler_t new_connection_handler,
+       input_handler_t in_handler, connection_closed_handler_t close_handler,
+       void *priv);
+
+struct service *find_by_name(char *name);
 
 int server_preinit(void);
 int server_init(struct command_context *cmd_ctx);
@@ -110,7 +112,7 @@ SERVER_PIPE_COMMAND();
 
 SERVER_PORT_COMMAND();
 
-#define ERROR_SERVER_REMOTE_CLOSED             (-400)
-#define ERROR_CONNECTION_REJECTED              (-401)
+#define ERROR_SERVER_REMOTE_CLOSED              (-400)
+#define ERROR_CONNECTION_REJECTED               (-401)
 
 #endif /* SERVER_H */
diff --git a/src/server/trace_server.c b/src/server/trace_server.c
new file mode 100644
index 0000000..5423f93
--- /dev/null
+++ b/src/server/trace_server.c
@@ -0,0 +1,135 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "trace_server.h"
+
+#define TRACE_SERVER_SERVICE_NAME "trace server"
+
+struct trce_server_connection {
+       int tc_outerror;/* flag an output error */
+};
+
+static const char *trace_server_port;
+
+static struct service *trace_server;
+
+/* handlers */
+static int trace_server_new_connection(struct connection *connection);
+static int trace_server_input(struct connection *connection);
+static int trace_server_output(struct connection *connection, const void *buf, 
ssize_t len);
+static int trace_server_closed(struct connection *connection);
+
+/* write data out to a socket.
+ *
+ * this is a blocking write, so the return value must equal the length, if
+ * that is not the case then flag the connection with an output error.
+ */
+int trace_server_output(struct connection *connection, const void *data, 
ssize_t len)
+{
+       ssize_t wlen;
+       struct trce_server_connection *tclc;
+
+       tclc = connection->priv;
+       if (tclc->tc_outerror)
+               return ERROR_SERVER_REMOTE_CLOSED;
+
+       wlen = connection_write(connection, data, len);
+
+       if (wlen == len)
+               return ERROR_OK;
+
+       LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
+       tclc->tc_outerror = 1;
+       return ERROR_SERVER_REMOTE_CLOSED;
+}
+
+/* connections */
+static int trace_server_new_connection(struct connection *connection)
+{
+       struct trce_server_connection *tclc;
+
+       tclc = malloc(sizeof(struct trce_server_connection));
+       if (tclc == NULL)
+               return ERROR_CONNECTION_REJECTED;
+
+       memset(tclc, 0, sizeof(struct trce_server_connection));
+       connection->priv = tclc;
+       return ERROR_OK;
+}
+
+static int trace_server_input(struct connection *connection)
+{
+       ssize_t rlen;
+       unsigned char in[256];
+
+       rlen = connection_read(connection, &in, sizeof(in));
+       if (rlen <= 0) {
+               if (rlen < 0)
+                       LOG_ERROR("error during read: %s", strerror(errno));
+               return ERROR_SERVER_REMOTE_CLOSED;
+       }
+
+       LOG_WARNING("trace server input not supported");
+
+       return ERROR_OK;
+}
+
+static int trace_server_closed(struct connection *connection)
+{
+       /* cleanup connection context */
+       if (connection->priv) {
+               free(connection->priv);
+               connection->priv = NULL;
+       }
+       return ERROR_OK;
+}
+
+int trace_server_init(void)
+{
+       trace_server = NULL;
+       if (strcmp(trace_server_port, "disabled") == 0) {
+               LOG_INFO("trace server disabled");
+               return ERROR_OK;
+       }
+       int rez = add_service(TRACE_SERVER_SERVICE_NAME, trace_server_port, 4,
+               &trace_server_new_connection, &trace_server_input,
+               &trace_server_closed, NULL);
+       trace_server = find_by_name(TRACE_SERVER_SERVICE_NAME);
+       return rez;
+}
+
+COMMAND_HANDLER(handle_trace_server_port_command)
+{
+       return CALL_COMMAND_HANDLER(server_pipe_command, &trace_server_port);
+}
+
+static const struct command_registration trace_server_command_handlers[] = {
+       {
+               .name = "trace_port",
+               .handler = handle_trace_server_port_command,
+               .mode = COMMAND_CONFIG,
+               .help = "Specify port on which to listen "
+                       "for Trace server.  "
+                       "Read help on 'trace_port'.",
+               .usage = "[port_num]",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+
+int trace_server_register_commands(struct command_context *cmd_ctx)
+{
+       trace_server_port = strdup("7777");
+       return register_commands(cmd_ctx, NULL, trace_server_command_handlers);
+}
+
+
+int trace_server_output_all(const void *data, ssize_t len)
+{
+       struct connection *t_conn = trace_server->connections;
+       while (t_conn != NULL) {
+               trace_server_output(t_conn, data, len);
+               t_conn = t_conn->next;
+       }
+       return ERROR_OK;
+}
diff --git a/src/server/trace_server.h b/src/server/trace_server.h
new file mode 100644
index 0000000..9a0c726
--- /dev/null
+++ b/src/server/trace_server.h
@@ -0,0 +1,12 @@
+#ifndef TRACE_SERVER_H
+#define TRACE_SERVER_H
+
+#include <server/server.h>
+
+int trace_server_init(void);
+int trace_server_register_commands(struct command_context *cmd_ctx);
+
+int trace_server_output_all(const void *data, ssize_t len);
+
+
+#endif /* TRACE_SERVER_H */

-- 

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135031&iu=/4140/ostg.clktrk
_______________________________________________
OpenOCD-devel mailing list
OpenOCD-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openocd-devel

Reply via email to