1 /* |
1 /* |
2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
25 |
25 |
26 package jdk.jfr.internal.instrument; |
26 package jdk.jfr.internal.instrument; |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.net.InetAddress; |
29 import java.net.InetAddress; |
|
30 import java.net.Socket; |
30 |
31 |
31 import jdk.jfr.events.SocketReadEvent; |
32 import jdk.jfr.events.SocketReadEvent; |
32 |
33 |
33 /** |
34 /** |
34 * See {@link JITracer} for an explanation of this code. |
35 * See {@link JITracer} for an explanation of this code. |
35 */ |
36 */ |
36 @JIInstrumentationTarget("java.net.SocketInputStream") |
37 @JIInstrumentationTarget("java.net.Socket$SocketInputStream") |
37 @JITypeMapping(from = "jdk.jfr.internal.instrument.SocketInputStreamInstrumentor$AbstractPlainSocketImpl", |
|
38 to = "java.net.AbstractPlainSocketImpl") |
|
39 final class SocketInputStreamInstrumentor { |
38 final class SocketInputStreamInstrumentor { |
40 |
39 |
41 private SocketInputStreamInstrumentor() { |
40 private SocketInputStreamInstrumentor() { |
42 } |
41 } |
43 |
42 |
44 @SuppressWarnings("deprecation") |
43 @SuppressWarnings("deprecation") |
45 @JIInstrumentationMethod |
44 @JIInstrumentationMethod |
46 int read(byte b[], int off, int length, int timeout) throws IOException { |
45 public int read(byte b[], int off, int length) throws IOException { |
47 SocketReadEvent event = SocketReadEvent.EVENT.get(); |
46 SocketReadEvent event = SocketReadEvent.EVENT.get(); |
48 if (!event.isEnabled()) { |
47 if (!event.isEnabled()) { |
49 return read(b, off, length, timeout); |
48 return read(b, off, length); |
50 } |
49 } |
51 int bytesRead = 0; |
50 int bytesRead = 0; |
52 try { |
51 try { |
53 event.begin(); |
52 event.begin(); |
54 bytesRead = read(b, off, length, timeout); |
53 bytesRead = read(b, off, length); |
55 } finally { |
54 } finally { |
56 event.end(); |
55 event.end(); |
57 if (event.shouldCommit()) { |
56 if (event.shouldCommit()) { |
58 String hostString = impl.address.toString(); |
57 InetAddress remote = parent.getInetAddress(); |
59 int delimiterIndex = hostString.lastIndexOf('/'); |
58 event.host = remote.getHostName(); |
60 |
59 event.address = remote.getHostAddress(); |
61 event.host = hostString.substring(0, delimiterIndex); |
60 event.port = parent.getPort(); |
62 event.address = hostString.substring(delimiterIndex + 1); |
|
63 event.port = impl.port; |
|
64 if (bytesRead < 0) { |
61 if (bytesRead < 0) { |
65 event.endOfStream = true; |
62 event.endOfStream = true; |
66 } else { |
63 } else { |
67 event.bytesRead = bytesRead; |
64 event.bytesRead = bytesRead; |
68 } |
65 } |
69 event.timeout = timeout; |
66 event.timeout = parent.getSoTimeout(); |
70 |
67 |
71 event.commit(); |
68 event.commit(); |
72 event.reset(); |
69 event.reset(); |
73 } |
70 } |
74 } |
71 } |
75 return bytesRead; |
72 return bytesRead; |
76 } |
73 } |
77 |
74 |
78 private AbstractPlainSocketImpl impl = null; |
75 // private field in java.net.Socket$SocketInputStream |
79 |
76 private Socket parent; |
80 void silenceFindBugsUnwrittenField(InetAddress dummy) { |
|
81 impl.address = dummy; |
|
82 } |
|
83 |
|
84 static class AbstractPlainSocketImpl { |
|
85 InetAddress address; |
|
86 int port; |
|
87 } |
|
88 } |
77 } |