This is an automated email from Gerrit.

"Antonio Borneo <[email protected]>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9746

-- gerrit

commit b6dafee2e076d45df1e572e04bfa428caa3b9956
Author: Antonio Borneo <[email protected]>
Date:   Mon Jun 15 22:43:01 2026 +0200

    openocd: replace gettimeofday() with timeval_ms()
    
    The man gettimeofday(2) reports that:
    
            POSIX.1-2008 marks gettimeofday() as obsolete,
            recommending the use of clock_gettime(2) instead.
    
    Uniform the code by replacing gettimeofday() with the OpenOCD
    function timeval_ms().
    
    Change-Id: I27f4f0273291be5105d2c567ed4cc897778ec70d
    Signed-off-by: Antonio Borneo <[email protected]>

diff --git a/src/jtag/drivers/presto.c b/src/jtag/drivers/presto.c
index 552c74c9c9..1519a6d986 100644
--- a/src/jtag/drivers/presto.c
+++ b/src/jtag/drivers/presto.c
@@ -94,9 +94,8 @@ static int presto_read(uint8_t *buf, uint32_t size)
 {
        uint32_t ftbytes = 0;
 
-       struct timeval timeout, now;
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, 1, 0);       /* one second timeout */
+       // 1 second timeout
+       int64_t then = timeval_ms() + 1000;
 
        while (ftbytes < size) {
                presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, 
size - ftbytes);
@@ -106,8 +105,7 @@ static int presto_read(uint8_t *buf, uint32_t size)
                }
                ftbytes += presto->retval;
 
-               gettimeofday(&now, NULL);
-               if (timeval_compare(&now, &timeout) > 0)
+               if (timeval_ms() > then)
                        break;
        }
 
diff --git a/src/jtag/drivers/sysfsgpio.c b/src/jtag/drivers/sysfsgpio.c
index 279b35377c..902459c7d1 100644
--- a/src/jtag/drivers/sysfsgpio.c
+++ b/src/jtag/drivers/sysfsgpio.c
@@ -101,7 +101,6 @@ static void unexport_sysfs_gpio(int gpio)
  */
 static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
 {
-       struct timeval timeout, now;
        char buf[40];
        char gpiostr[5];
        int ret;
@@ -121,16 +120,15 @@ static int setup_sysfs_gpio(int gpio, int is_output, int 
init_high)
                }
        }
 
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, 0, 500000);
+       // 500 ms timeout
+       int64_t then = timeval_ms() + 500;
 
        snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
        for (;;) {
                ret = open_write_close(buf, is_output ? (init_high ? "high" : 
"low") : "in");
                if (ret >= 0 || errno != EACCES)
                        break;
-               gettimeofday(&now, NULL);
-               if (timeval_compare(&now, &timeout) >= 0)
+               if (timeval_ms() >= then)
                        break;
                jtag_sleep(10000);
        }
@@ -146,8 +144,7 @@ static int setup_sysfs_gpio(int gpio, int is_output, int 
init_high)
                ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
                if (ret >= 0 || errno != EACCES)
                        break;
-               gettimeofday(&now, NULL);
-               if (timeval_compare(&now, &timeout) >= 0)
+               if (timeval_ms() >= then)
                        break;
                jtag_sleep(10000);
        }
diff --git a/src/target/cortex_m.c b/src/target/cortex_m.c
index f8b6164af9..61cddafc11 100644
--- a/src/target/cortex_m.c
+++ b/src/target/cortex_m.c
@@ -2370,7 +2370,6 @@ void cortex_m_deinit_target(struct target *target)
 int cortex_m_profiling(struct target *target, uint32_t *samples,
                              uint32_t max_num_samples, uint32_t *num_samples, 
uint32_t seconds)
 {
-       struct timeval timeout, now;
        struct armv7m_common *armv7m = target_to_armv7m(target);
        uint32_t reg_value;
        int retval;
@@ -2385,8 +2384,7 @@ int cortex_m_profiling(struct target *target, uint32_t 
*samples,
                return target_profiling_default(target, samples, 
max_num_samples, num_samples, seconds);
        }
 
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, seconds, 0);
+       int64_t then = timeval_ms() + seconds * 1000LL;
 
        LOG_TARGET_INFO(target, "Starting Cortex-M profiling. Sampling DWT_PCSR 
as fast as we can...");
 
@@ -2422,8 +2420,7 @@ int cortex_m_profiling(struct target *target, uint32_t 
*samples,
                }
 
 
-               gettimeofday(&now, NULL);
-               if (sample_count >= max_num_samples || timeval_compare(&now, 
&timeout) > 0) {
+               if (sample_count >= max_num_samples || timeval_ms() > then) {
                        LOG_TARGET_INFO(target, "Profiling completed. %" PRIu32 
" samples.", sample_count);
                        break;
                }
diff --git a/src/target/embeddedice.c b/src/target/embeddedice.c
index f6e330b377..890b5f499c 100644
--- a/src/target/embeddedice.c
+++ b/src/target/embeddedice.c
@@ -574,7 +574,7 @@ int embeddedice_send(struct arm_jtag *jtag_info, uint32_t 
*data, uint32_t size)
 /**
  * Poll DCC control register until read or write handshake completes.
  */
-int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t 
timeout)
+int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t 
timeout_ms)
 {
        struct scan_field fields[3];
        uint8_t field0_in[4];
@@ -582,8 +582,6 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int 
hsbit, uint32_t timeou
        uint8_t field2_out[1];
        int retval;
        uint32_t hsact;
-       struct timeval now;
-       struct timeval timeout_end;
 
        if (hsbit == EICE_COMM_CTRL_WBIT)
                hsact = 1;
@@ -616,8 +614,8 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int 
hsbit, uint32_t timeou
        fields[2].in_value = NULL;
 
        jtag_add_dr_scan(jtag_info->tap, 3, fields, TAP_IDLE);
-       gettimeofday(&timeout_end, NULL);
-       timeval_add_time(&timeout_end, 0, timeout * 1000);
+
+       int64_t then = timeval_ms() + timeout_ms;
        do {
                jtag_add_dr_scan(jtag_info->tap, 3, fields, TAP_IDLE);
                retval = jtag_execute_queue();
@@ -627,8 +625,7 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int 
hsbit, uint32_t timeou
                if (buf_get_u32(field0_in, hsbit, 1) == hsact)
                        return ERROR_OK;
 
-               gettimeofday(&now, NULL);
-       } while (timeval_compare(&now, &timeout_end) <= 0);
+       } while (timeval_ms() <= then);
 
        LOG_ERROR("embeddedice handshake timeout");
        return ERROR_TARGET_TIMEOUT;
diff --git a/src/target/espressif/esp_xtensa.c 
b/src/target/espressif/esp_xtensa.c
index 4cadcb3301..3575272608 100644
--- a/src/target/espressif/esp_xtensa.c
+++ b/src/target/espressif/esp_xtensa.c
@@ -183,7 +183,6 @@ int esp_xtensa_breakpoint_remove(struct target *target, 
struct breakpoint *break
 int esp_xtensa_profiling(struct target *target, uint32_t *samples,
        uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
 {
-       struct timeval timeout, now;
        struct xtensa *xtensa = target_to_xtensa(target);
        int retval = ERROR_OK;
        int res;
@@ -192,8 +191,7 @@ int esp_xtensa_profiling(struct target *target, uint32_t 
*samples,
        #define MIN_PASS 200
        #define MAX_PASS 1000
 
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, seconds, 0);
+       int64_t then = timeval_ms() + seconds * 1000LL;
 
        uint8_t buf[sizeof(uint32_t) * MAX_PASS];
 
@@ -238,8 +236,7 @@ int esp_xtensa_profiling(struct target *target, uint32_t 
*samples,
                        uint32_t sample32 = buf_get_u32(buf + i * 
sizeof(uint32_t), 0, 32);
                        samples[sample_count++] = sample32;
                }
-               gettimeofday(&now, NULL);
-               if (sample_count >= max_num_samples || timeval_compare(&now, 
&timeout) > 0) {
+               if (sample_count >= max_num_samples || timeval_ms() > then) {
                        LOG_TARGET_INFO(target, "Profiling completed. %" PRIu32 
" samples.", sample_count);
                        break;
                }
diff --git a/src/target/openrisc/or1k.c b/src/target/openrisc/or1k.c
index eab064140e..200a97dfeb 100644
--- a/src/target/openrisc/or1k.c
+++ b/src/target/openrisc/or1k.c
@@ -1200,13 +1200,11 @@ static int or1k_checksum_memory(struct target *target, 
target_addr_t address,
 static int or1k_profiling(struct target *target, uint32_t *samples,
                uint32_t max_num_samples, uint32_t *num_samples, uint32_t 
seconds)
 {
-       struct timeval timeout, now;
        struct or1k_common *or1k = target_to_or1k(target);
        struct or1k_du *du_core = or1k_to_du(or1k);
        int retval = ERROR_OK;
 
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, seconds, 0);
+       int64_t then = timeval_ms() + seconds * 1000LL;
 
        LOG_INFO("Starting or1k profiling. Sampling npc as fast as we can...");
 
@@ -1232,8 +1230,7 @@ static int or1k_profiling(struct target *target, uint32_t 
*samples,
 
                samples[sample_count++] = reg_value;
 
-               gettimeofday(&now, NULL);
-               if ((sample_count >= max_num_samples) || timeval_compare(&now, 
&timeout) > 0) {
+               if (sample_count >= max_num_samples || timeval_ms() > then) {
                        LOG_INFO("Profiling completed. %" PRIu32 " samples.", 
sample_count);
                        break;
                }
diff --git a/src/target/target.c b/src/target/target.c
index e6c0ae9aa7..fbf25cd02d 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -2339,10 +2339,7 @@ static int target_gdb_fileio_end_default(struct target 
*target,
 int target_profiling_default(struct target *target, uint32_t *samples,
                uint32_t max_num_samples, uint32_t *num_samples, uint32_t 
seconds)
 {
-       struct timeval timeout, now;
-
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, seconds, 0);
+       int64_t then = timeval_ms() + seconds * 1000LL;
 
        LOG_INFO("Starting profiling. Halting and resuming the"
                        " target as often as we can...");
@@ -2373,8 +2370,7 @@ int target_profiling_default(struct target *target, 
uint32_t *samples,
                if (retval != ERROR_OK)
                        break;
 
-               gettimeofday(&now, NULL);
-               if ((sample_count >= max_num_samples) || timeval_compare(&now, 
&timeout) >= 0) {
+               if (sample_count >= max_num_samples || timeval_ms() >= then) {
                        LOG_INFO("Profiling completed. %" PRIu32 " samples.", 
sample_count);
                        break;
                }
diff --git a/src/target/xscale.c b/src/target/xscale.c
index 64980ee242..6dc4e43082 100644
--- a/src/target/xscale.c
+++ b/src/target/xscale.c
@@ -334,7 +334,6 @@ static int xscale_read_tx(struct target *target, int 
consume)
        enum tap_state path[3];
        enum tap_state noconsume_path[6];
        int retval;
-       struct timeval timeout, now;
        struct scan_field fields[3];
        uint8_t field0_in = 0x0;
        uint8_t field0_check_value = 0x2;
@@ -369,8 +368,8 @@ static int xscale_read_tx(struct target *target, int 
consume)
        uint8_t tmp;
        fields[2].in_value = &tmp;
 
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, 1, 0);
+       // 1 second timeout
+       int64_t then = timeval_ms() + 1000;
 
        for (;; ) {
                /* if we want to consume the register content (i.e. clear 
TX_READY),
@@ -393,8 +392,7 @@ static int xscale_read_tx(struct target *target, int 
consume)
                        return ERROR_TARGET_TIMEOUT;
                }
 
-               gettimeofday(&now, NULL);
-               if (timeval_compare(&now, &timeout) > 0) {
+               if (timeval_ms() > then) {
                        LOG_ERROR("time out reading TX register");
                        return ERROR_TARGET_TIMEOUT;
                }
@@ -418,7 +416,6 @@ static int xscale_write_rx(struct target *target)
 {
        struct xscale_common *xscale = target_to_xscale(target);
        int retval;
-       struct timeval timeout, now;
        struct scan_field fields[3];
        uint8_t field0_out = 0x0;
        uint8_t field0_in = 0x0;
@@ -446,8 +443,8 @@ static int xscale_write_rx(struct target *target)
        uint8_t tmp;
        fields[2].in_value = &tmp;
 
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, 1, 0);
+       // 1 second timeout
+       int64_t then = timeval_ms() + 1000;
 
        /* poll until rx_read is low */
        LOG_DEBUG("polling RX");
@@ -463,9 +460,7 @@ static int xscale_write_rx(struct target *target)
                        return retval;
                }
 
-               gettimeofday(&now, NULL);
-               if ((now.tv_sec > timeout.tv_sec) ||
-                       ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > 
timeout.tv_usec))) {
+               if (timeval_ms() > then) {
                        LOG_ERROR("time out writing RX register");
                        return ERROR_TARGET_TIMEOUT;
                }

-- 

Reply via email to