Skip to content

Commit f6d5ef6

Browse files
committed
net: improve tracing output for -d trace_tcp; move error_code() calls right after the corresponding C APIs (errno is a global that should be read immediately to be valid)
1 parent 7831fb0 commit f6d5ef6

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎vlib/net/tcp.c.v‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,21 @@ pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) !int {
158158
-1
159159
}
160160
}
161-
$if trace_tcp ? {
162-
eprintln('<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle} | buf_ptr: ${ptr_str(buf_ptr)} len: ${len} | res: ${res}')
163-
}
161+
ecode := error_code()
164162
if res > 0 {
163+
$if trace_tcp ? {
164+
eprintln(
165+
'<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle} | buf_ptr: ${ptr_str(buf_ptr)} | len: ${len} | res: ${res} |\n' +
166+
unsafe { buf_ptr.vstring_with_len(len) })
167+
}
165168
$if trace_tcp_data_read ? {
166169
eprintln(
167170
'<<< TcpConn.read_ptr | 1 data.len: ${res:6} | hex: ${unsafe { buf_ptr.vbytes(res) }.hex()} | data: ' +
168171
unsafe { buf_ptr.vstring_with_len(res) })
169172
}
170173
return res
171174
}
172-
code := if should_ewouldblock { int(error_ewouldblock) } else { error_code() }
175+
code := if should_ewouldblock { int(error_ewouldblock) } else { ecode }
173176
if code in [int(error_ewouldblock), int(error_eagain), C.EINTR] {
174177
c.wait_for_read()!
175178
res = $if is_coroutine ? {
@@ -178,7 +181,9 @@ pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) !int {
178181
C.recv(c.sock.handle, voidptr(buf_ptr), len, msg_dontwait)
179182
}
180183
$if trace_tcp ? {
181-
eprintln('<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle} | buf_ptr: ${ptr_str(buf_ptr)} len: ${len} | res: ${res}')
184+
eprintln(
185+
'<<< TcpConn.read_ptr | c.sock.handle: ${c.sock.handle} | buf_ptr: ${ptr_str(buf_ptr)} | len: ${len} | res: ${res} | code: ${code} |\n' +
186+
unsafe { buf_ptr.vstring_with_len(len) })
182187
}
183188
$if trace_tcp_data_read ? {
184189
if res > 0 {
@@ -234,11 +239,11 @@ pub fn (mut c TcpConn) write_ptr(b &u8, len int) !int {
234239
} $else {
235240
C.send(c.sock.handle, ptr, remaining, msg_nosignal)
236241
}
242+
code := error_code()
237243
$if trace_tcp_data_write ? {
238244
eprintln('>>> TcpConn.write_ptr | data chunk, total_sent: ${total_sent:6}, remaining: ${remaining:6}, ptr: ${voidptr(ptr):x} => sent: ${sent:6}')
239245
}
240246
if sent < 0 {
241-
code := error_code()
242247
$if trace_tcp_send_failures ? {
243248
eprintln('>>> TcpConn.write_ptr | send_failure, data.len: ${len:6}, total_sent: ${total_sent:6}, remaining: ${remaining:6}, ptr: ${voidptr(ptr):x}, c.write_timeout: ${c.write_timeout:3} => sent: ${sent:6}, error code: ${code:3}')
244249
}
@@ -456,9 +461,8 @@ pub fn (mut l TcpListener) accept_only() !&TcpConn {
456461
} $else {
457462
C.accept(l.sock.handle, 0, 0)
458463
}
459-
464+
code := error_code()
460465
if !l.is_blocking && new_handle <= 0 {
461-
code := error_code()
462466
if code in [int(error_einprogress), int(error_ewouldblock), int(error_eagain), C.EINTR] {
463467
l.wait_for_accept()!
464468
new_handle = $if is_coroutine ? {
@@ -642,10 +646,10 @@ fn (mut s TcpSocket) connect(a Addr) ! {
642646
} $else {
643647
C.connect(s.handle, voidptr(&a), a.len())
644648
}
649+
ecode := error_code()
645650
if res == 0 {
646651
return
647652
}
648-
ecode := error_code()
649653
// On nix non-blocking sockets we expect einprogress
650654
// On windows we expect res == -1 && error_code() == ewouldblock
651655
if (is_windows && ecode == int(error_ewouldblock)) || (!is_windows && res == -1

0 commit comments

Comments
 (0)