armv7m: add a TCP channel to stream captured trace
[openocd.git] / src / target / armv7m_trace.c
index eb07a6e64056f9db57907b0b28c22496159c5069..916d1a1647866d8d8ad4f938966e32dffb725cb3 100644 (file)
@@ -10,6 +10,9 @@
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -35,10 +38,44 @@ static int armv7m_poll_trace(void *target)
        if (retval != ERROR_OK || !size)
                return retval;
 
-       if (fwrite(buf, 1, size, armv7m->trace_config.trace_file) == size)
-               fflush(armv7m->trace_config.trace_file);
-       else {
-               LOG_ERROR("Error writing to the trace destination file");
+       target_call_trace_callbacks(target, size, buf);
+
+       switch (armv7m->trace_config.internal_channel) {
+       case TRACE_INTERNAL_CHANNEL_FILE:
+               if (armv7m->trace_config.trace_file != NULL) {
+                       if (fwrite(buf, 1, size, armv7m->trace_config.trace_file) == size)
+                               fflush(armv7m->trace_config.trace_file);
+                       else {
+                               LOG_ERROR("Error writing to the trace destination file");
+                               return ERROR_FAIL;
+                       }
+               }
+               break;
+       case TRACE_INTERNAL_CHANNEL_TCP:
+               if (armv7m->trace_config.trace_service != NULL) {
+                       /* broadcast to all service connections */
+                       struct connection *connection = armv7m->trace_config.trace_service->connections;
+                       retval = ERROR_OK;
+                       while (connection) {
+                               if (connection_write(connection, buf, size) != (int) size)
+                                       retval = ERROR_FAIL;
+
+                               connection = connection->next;
+                       }
+
+                       if (retval != ERROR_OK) {
+                               LOG_ERROR("Error streaming the trace to TCP/IP port");
+                               return ERROR_FAIL;
+                       }
+               }
+               break;
+       case TRACE_INTERNAL_CHANNEL_TCL_ONLY:
+               /* nothing to do :
+                * the trace data is sent to TCL by calling the target_call_trace_callbacks
+                **/
+               break;
+       default:
+               LOG_ERROR("unsupported trace internal channel");
                return ERROR_FAIL;
        }
 
@@ -49,41 +86,39 @@ int armv7m_trace_tpiu_config(struct target *target)
 {
        struct armv7m_common *armv7m = target_to_armv7m(target);
        struct armv7m_trace_config *trace_config = &armv7m->trace_config;
-       int prescaler;
+       uint16_t prescaler;
        int retval;
 
        target_unregister_timer_callback(armv7m_poll_trace, target);
 
+       retval = adapter_config_trace(trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL,
+               trace_config->pin_protocol, trace_config->port_size,
+               &trace_config->trace_freq, trace_config->traceclkin_freq, &prescaler);
 
-       retval = adapter_config_trace(trace_config->config_type == INTERNAL,
-                                     trace_config->pin_protocol,
-                                     trace_config->port_size,
-                                     &trace_config->trace_freq);
        if (retval != ERROR_OK)
                return retval;
 
+       if (trace_config->config_type == TRACE_CONFIG_TYPE_EXTERNAL) {
+               prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
+
+               if (trace_config->traceclkin_freq % trace_config->trace_freq) {
+                       prescaler++;
+
+                       int trace_freq = trace_config->traceclkin_freq / prescaler;
+                       LOG_INFO("Can not obtain %u trace port frequency from %u "
+                               "TRACECLKIN frequency, using %u instead",
+                               trace_config->trace_freq, trace_config->traceclkin_freq,
+                               trace_freq);
+
+                       trace_config->trace_freq = trace_freq;
+               }
+       }
+
        if (!trace_config->trace_freq) {
                LOG_ERROR("Trace port frequency is 0, can't enable TPIU");
                return ERROR_FAIL;
        }
 
-       prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
-
-       if (trace_config->traceclkin_freq % trace_config->trace_freq) {
-               prescaler++;
-               int trace_freq = trace_config->traceclkin_freq / prescaler;
-               LOG_INFO("Can not obtain %u trace port frequency from %u TRACECLKIN frequency, using %u instead",
-                         trace_config->trace_freq, trace_config->traceclkin_freq,
-                         trace_freq);
-               trace_config->trace_freq = trace_freq;
-               retval = adapter_config_trace(trace_config->config_type == INTERNAL,
-                                             trace_config->pin_protocol,
-                                             trace_config->port_size,
-                                             &trace_config->trace_freq);
-               if (retval != ERROR_OK)
-                       return retval;
-       }
-
        retval = target_write_u32(target, TPIU_CSPSR, 1 << trace_config->port_size);
        if (retval != ERROR_OK)
                return retval;
@@ -108,8 +143,9 @@ int armv7m_trace_tpiu_config(struct target *target)
        if (retval != ERROR_OK)
                return retval;
 
-       if (trace_config->config_type == INTERNAL)
-               target_register_timer_callback(armv7m_poll_trace, 1, 1, target);
+       if (trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL)
+               target_register_timer_callback(armv7m_poll_trace, 1,
+               TARGET_TIMER_TYPE_PERIODIC, target);
 
        target_call_event_callbacks(target, TARGET_EVENT_TRACE_CONFIG);
 
@@ -146,11 +182,56 @@ int armv7m_trace_itm_config(struct target *target)
        return ERROR_OK;
 }
 
-static void close_trace_file(struct armv7m_common *armv7m)
+static void close_trace_channel(struct armv7m_common *armv7m)
+{
+       switch (armv7m->trace_config.internal_channel) {
+       case TRACE_INTERNAL_CHANNEL_FILE:
+               if (armv7m->trace_config.trace_file)
+                       fclose(armv7m->trace_config.trace_file);
+               armv7m->trace_config.trace_file = NULL;
+               break;
+       case TRACE_INTERNAL_CHANNEL_TCP:
+               if (armv7m->trace_config.trace_service)
+                       remove_service(armv7m->trace_config.trace_service->name, armv7m->trace_config.trace_service->port);
+               armv7m->trace_config.trace_service = NULL;
+               break;
+       case TRACE_INTERNAL_CHANNEL_TCL_ONLY:
+               /* nothing to do:
+                * the trace polling is disabled in the beginning of armv7m_trace_tpiu_config
+                **/
+               break;
+       default:
+               LOG_ERROR("unsupported trace internal channel");
+       }
+}
+
+static int trace_new_connection(struct connection *connection)
+{
+       /* nothing to do */
+       return ERROR_OK;
+}
+
+static int trace_input(struct connection *connection)
+{
+       /* create a dummy buffer to check if the connection is still active */
+       const int buf_len = 100;
+       unsigned char buf[buf_len];
+       int bytes_read = connection_read(connection, buf, buf_len);
+
+       if (bytes_read == 0)
+               return ERROR_SERVER_REMOTE_CLOSED;
+       else if (bytes_read == -1) {
+               LOG_ERROR("error during read: %s", strerror(errno));
+               return ERROR_SERVER_REMOTE_CLOSED;
+       }
+
+       return ERROR_OK;
+}
+
+static int trace_connection_closed(struct connection *connection)
 {
-       if (armv7m->trace_config.trace_file)
-               fclose(armv7m->trace_config.trace_file);
-       armv7m->trace_config.trace_file = NULL;
+       /* nothing to do, no connection->priv to free */
+       return ERROR_OK;
 }
 
 COMMAND_HANDLER(handle_tpiu_config_command)
@@ -164,9 +245,9 @@ COMMAND_HANDLER(handle_tpiu_config_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
        if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
                if (CMD_ARGC == cmd_idx + 1) {
-                       close_trace_file(armv7m);
+                       close_trace_channel(armv7m);
 
-                       armv7m->trace_config.config_type = DISABLED;
+                       armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_DISABLED;
                        if (CMD_CTX->mode == COMMAND_EXEC)
                                return armv7m_trace_tpiu_config(target);
                        else
@@ -174,19 +255,36 @@ COMMAND_HANDLER(handle_tpiu_config_command)
                }
        } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
                   !strcmp(CMD_ARGV[cmd_idx], "internal")) {
-               close_trace_file(armv7m);
+               close_trace_channel(armv7m);
 
-               armv7m->trace_config.config_type = EXTERNAL;
+               armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_EXTERNAL;
                if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
                        cmd_idx++;
                        if (CMD_ARGC == cmd_idx)
                                return ERROR_COMMAND_SYNTAX_ERROR;
 
-                       armv7m->trace_config.config_type = INTERNAL;
-                       armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
-                       if (!armv7m->trace_config.trace_file) {
-                               LOG_ERROR("Can't open trace destination file");
-                               return ERROR_FAIL;
+                       armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_INTERNAL;
+                       armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_TCL_ONLY;
+
+                       if (strcmp(CMD_ARGV[cmd_idx], "-") != 0) {
+                               if (CMD_ARGV[cmd_idx][0] == ':') {
+                                       armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_TCP;
+
+                                       int ret = add_service("armv7m_trace", &(CMD_ARGV[cmd_idx][1]),
+                                                       CONNECTION_LIMIT_UNLIMITED, trace_new_connection, trace_input,
+                                                       trace_connection_closed, NULL, &armv7m->trace_config.trace_service);
+                                       if (ret != ERROR_OK) {
+                                               LOG_ERROR("Can't configure trace TCP port");
+                                               return ERROR_FAIL;
+                                       }
+                               } else {
+                                       armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_FILE;
+                                       armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
+                                       if (!armv7m->trace_config.trace_file) {
+                                               LOG_ERROR("Can't open trace destination file");
+                                               return ERROR_FAIL;
+                                       }
+                               }
                        }
                }
                cmd_idx++;
@@ -194,7 +292,7 @@ COMMAND_HANDLER(handle_tpiu_config_command)
                        return ERROR_COMMAND_SYNTAX_ERROR;
 
                if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
-                       armv7m->trace_config.pin_protocol = SYNC;
+                       armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_SYNC;
 
                        cmd_idx++;
                        if (CMD_ARGC == cmd_idx)
@@ -203,9 +301,9 @@ COMMAND_HANDLER(handle_tpiu_config_command)
                        COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
                } else {
                        if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
-                               armv7m->trace_config.pin_protocol = ASYNC_MANCHESTER;
+                               armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_MANCHESTER;
                        else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
-                               armv7m->trace_config.pin_protocol = ASYNC_UART;
+                               armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_UART;
                        else
                                return ERROR_COMMAND_SYNTAX_ERROR;
 
@@ -227,7 +325,7 @@ COMMAND_HANDLER(handle_tpiu_config_command)
                        COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
                        cmd_idx++;
                } else {
-                       if (armv7m->trace_config.config_type != INTERNAL) {
+                       if (armv7m->trace_config.config_type != TRACE_CONFIG_TYPE_INTERNAL) {
                                LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
                                return ERROR_COMMAND_SYNTAX_ERROR;
                        }
@@ -297,7 +395,7 @@ static const struct command_registration tpiu_command_handlers[] = {
                .mode = COMMAND_ANY,
                .help = "Configure TPIU features",
                .usage = "(disable | "
-               "((external | internal <filename>) "
+               "((external | internal (<filename> | <:port> | -)) "
                "(sync <port width> | ((manchester | uart) <formatter enable>)) "
                "<TRACECLKIN freq> [<trace freq>]))",
        },

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)