Poison

LineNumberTable

今天在调试时又遇到行号无法对齐的问题,因为之前开发 Java Agent 时遇到过,常见于源码 jar 与运行时 jar 不一致导致,本文简要记录。

调试的类为 javax.servlet.annotation.MultipartConfig,即最常见的 Servlet API 中的类,比如对以下四个字段打上了断点:


你会发现断点停在了类上的注释处,查询应用中对 Servlet API 的依赖为:

1
2
3
4
5
6
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

说明 IDE 是使用的 javax.servlet-api 对应的 jar 包内容进行显示,而我们知道运行时是使用的 Tomcat lib 目录中的 servlet-api.jar,于是我将该 jar 解压,并执行命令 javap -v MultipartConfigElement.class,输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
Classfile /private/tmp/javax/servlet/MultipartConfigElement.class
Last modified Jun 3, 2020; size 1574 bytes
MD5 checksum ddf46ee87acaf5c92faa4d9b022352a3
Compiled from "MultipartConfigElement.java"
public class javax.servlet.MultipartConfigElement
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #14.#45 // java/lang/Object."<init>":()V
#2 = Fieldref #13.#46 // javax/servlet/MultipartConfigElement.location:Ljava/lang/String;
#3 = String #47 //
#4 = Long -1l
#6 = Fieldref #13.#48 // javax/servlet/MultipartConfigElement.maxFileSize:J
#7 = Fieldref #13.#49 // javax/servlet/MultipartConfigElement.maxRequestSize:J
#8 = Fieldref #13.#50 // javax/servlet/MultipartConfigElement.fileSizeThreshold:I
#9 = InterfaceMethodref #51.#52 // javax/servlet/annotation/MultipartConfig.location:()Ljava/lang/String;
#10 = InterfaceMethodref #51.#53 // javax/servlet/annotation/MultipartConfig.maxFileSize:()J
#11 = InterfaceMethodref #51.#54 // javax/servlet/annotation/MultipartConfig.maxRequestSize:()J
#12 = InterfaceMethodref #51.#55 // javax/servlet/annotation/MultipartConfig.fileSizeThreshold:()I
#13 = Class #56 // javax/servlet/MultipartConfigElement
#14 = Class #57 // java/lang/Object
#15 = Utf8 location
#16 = Utf8 Ljava/lang/String;
#17 = Utf8 maxFileSize
#18 = Utf8 J
#19 = Utf8 maxRequestSize
#20 = Utf8 fileSizeThreshold
#21 = Utf8 I
#22 = Utf8 <init>
#23 = Utf8 (Ljava/lang/String;)V
#24 = Utf8 Code
#25 = Utf8 LineNumberTable
#26 = Utf8 LocalVariableTable
#27 = Utf8 this
#28 = Utf8 Ljavax/servlet/MultipartConfigElement;
#29 = Utf8 StackMapTable
#30 = Class #56 // javax/servlet/MultipartConfigElement
#31 = Class #58 // java/lang/String
#32 = Utf8 (Ljava/lang/String;JJI)V
#33 = Utf8 (Ljavax/servlet/annotation/MultipartConfig;)V
#34 = Utf8 annotation
#35 = Utf8 Ljavax/servlet/annotation/MultipartConfig;
#36 = Utf8 getLocation
#37 = Utf8 ()Ljava/lang/String;
#38 = Utf8 getMaxFileSize
#39 = Utf8 ()J
#40 = Utf8 getMaxRequestSize
#41 = Utf8 getFileSizeThreshold
#42 = Utf8 ()I
#43 = Utf8 SourceFile
#44 = Utf8 MultipartConfigElement.java
#45 = NameAndType #22:#59 // "<init>":()V
#46 = NameAndType #15:#16 // location:Ljava/lang/String;
#47 = Utf8
#48 = NameAndType #17:#18 // maxFileSize:J
#49 = NameAndType #19:#18 // maxRequestSize:J
#50 = NameAndType #20:#21 // fileSizeThreshold:I
#51 = Class #60 // javax/servlet/annotation/MultipartConfig
#52 = NameAndType #15:#37 // location:()Ljava/lang/String;
#53 = NameAndType #17:#39 // maxFileSize:()J
#54 = NameAndType #19:#39 // maxRequestSize:()J
#55 = NameAndType #20:#42 // fileSizeThreshold:()I
#56 = Utf8 javax/servlet/MultipartConfigElement
#57 = Utf8 java/lang/Object
#58 = Utf8 java/lang/String
#59 = Utf8 ()V
#60 = Utf8 javax/servlet/annotation/MultipartConfig
{
public javax.servlet.MultipartConfigElement(java.lang.String);
descriptor: (Ljava/lang/String;)V
flags: ACC_PUBLIC
Code:
stack=3, locals=2, args_size=2
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_1
5: ifnull 16
8: aload_0
9: aload_1
10: putfield #2 // Field location:Ljava/lang/String;
13: goto 22
16: aload_0
17: ldc #3 // String
19: putfield #2 // Field location:Ljava/lang/String;
22: aload_0
23: ldc2_w #4 // long -1l
26: putfield #6 // Field maxFileSize:J
29: aload_0
30: ldc2_w #4 // long -1l
33: putfield #7 // Field maxRequestSize:J
36: aload_0
37: iconst_0
38: putfield #8 // Field fileSizeThreshold:I
41: return
LineNumberTable:
line 32: 0
line 34: 4
line 35: 8
line 37: 16
line 39: 22
line 40: 29
line 41: 36
line 42: 41
LocalVariableTable:
Start Length Slot Name Signature
0 42 0 this Ljavax/servlet/MultipartConfigElement;
0 42 1 location Ljava/lang/String;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 16
locals = [ class javax/servlet/MultipartConfigElement, class java/lang/String ]
stack = []
frame_type = 5 /* same */

public javax.servlet.MultipartConfigElement(java.lang.String, long, long, int);
descriptor: (Ljava/lang/String;JJI)V
flags: ACC_PUBLIC
Code:
stack=3, locals=7, args_size=5
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_1
5: ifnull 16
8: aload_0
9: aload_1
10: putfield #2 // Field location:Ljava/lang/String;
13: goto 22
16: aload_0
17: ldc #3 // String
19: putfield #2 // Field location:Ljava/lang/String;
22: aload_0
23: lload_2
24: putfield #6 // Field maxFileSize:J
27: aload_0
28: lload 4
30: putfield #7 // Field maxRequestSize:J
33: iload 6
35: ifle 47
38: aload_0
39: iload 6
41: putfield #8 // Field fileSizeThreshold:I
44: goto 52
47: aload_0
48: iconst_0
49: putfield #8 // Field fileSizeThreshold:I
52: return
LineNumberTable:
line 45: 0
line 47: 4
line 48: 8
line 50: 16
line 52: 22
line 53: 27
line 56: 33
line 57: 38
line 59: 47
line 61: 52
LocalVariableTable:
Start Length Slot Name Signature
0 53 0 this Ljavax/servlet/MultipartConfigElement;
0 53 1 location Ljava/lang/String;
0 53 2 maxFileSize J
0 53 4 maxRequestSize J
0 53 6 fileSizeThreshold I
StackMapTable: number_of_entries = 4
frame_type = 255 /* full_frame */
offset_delta = 16
locals = [ class javax/servlet/MultipartConfigElement, class java/lang/String, long, long, int ]
stack = []
frame_type = 5 /* same */
frame_type = 24 /* same */
frame_type = 4 /* same */

public javax.servlet.MultipartConfigElement(javax.servlet.annotation.MultipartConfig);
descriptor: (Ljavax/servlet/annotation/MultipartConfig;)V
flags: ACC_PUBLIC
Code:
stack=3, locals=2, args_size=2
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: aload_1
6: invokeinterface #9, 1 // InterfaceMethod javax/servlet/annotation/MultipartConfig.location:()Ljava/lang/String;
11: putfield #2 // Field location:Ljava/lang/String;
14: aload_0
15: aload_1
16: invokeinterface #10, 1 // InterfaceMethod javax/servlet/annotation/MultipartConfig.maxFileSize:()J
21: putfield #6 // Field maxFileSize:J
24: aload_0
25: aload_1
26: invokeinterface #11, 1 // InterfaceMethod javax/servlet/annotation/MultipartConfig.maxRequestSize:()J
31: putfield #7 // Field maxRequestSize:J
34: aload_0
35: aload_1
36: invokeinterface #12, 1 // InterfaceMethod javax/servlet/annotation/MultipartConfig.fileSizeThreshold:()I
41: putfield #8 // Field fileSizeThreshold:I
44: return
LineNumberTable:
line 63: 0
line 64: 4
line 65: 14
line 66: 24
line 67: 34
line 68: 44
LocalVariableTable:
Start Length Slot Name Signature
0 45 0 this Ljavax/servlet/MultipartConfigElement;
0 45 1 annotation Ljavax/servlet/annotation/MultipartConfig;

public java.lang.String getLocation();
descriptor: ()Ljava/lang/String;
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #2 // Field location:Ljava/lang/String;
4: areturn
LineNumberTable:
line 71: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ljavax/servlet/MultipartConfigElement;

public long getMaxFileSize();
descriptor: ()J
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: getfield #6 // Field maxFileSize:J
4: lreturn
LineNumberTable:
line 75: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ljavax/servlet/MultipartConfigElement;

public long getMaxRequestSize();
descriptor: ()J
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: getfield #7 // Field maxRequestSize:J
4: lreturn
LineNumberTable:
line 79: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ljavax/servlet/MultipartConfigElement;

public int getFileSizeThreshold();
descriptor: ()I
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #8 // Field fileSizeThreshold:I
4: ireturn
LineNumberTable:
line 83: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ljavax/servlet/MultipartConfigElement;
}
SourceFile: "MultipartConfigElement.java"

从以上的字节码信息中可以看出 line 48 正在执行 public javax.servlet.MultipartConfigElement(java.lang.String, long, long, int) 方法中对 location 字段的相关操作,很明显,与 javax.servlet-api 源码不匹配。查询 Tomcat 对应版本的源码,很容易发现 lib 目录中的 servlet-api.jar 由 Tomcat 中的源码构建而来,源码位于 MultipartConfigElement.java at 8.5.56:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.servlet;

import javax.servlet.annotation.MultipartConfig;

/**
* @since Servlet 3.0
* TODO SERVLET3 - Add comments
*/
public class MultipartConfigElement {

private final String location;// = "";
private final long maxFileSize;// = -1;
private final long maxRequestSize;// = -1;
private final int fileSizeThreshold;// = 0;

public MultipartConfigElement(String location) {
// Keep empty string default if location is null
if (location != null) {
this.location = location;
} else {
this.location = "";
}
this.maxFileSize = -1;
this.maxRequestSize = -1;
this.fileSizeThreshold = 0;
}

public MultipartConfigElement(String location, long maxFileSize,
long maxRequestSize, int fileSizeThreshold) {
// Keep empty string default if location is null
if (location != null) {
this.location = location;
} else {
this.location = "";
}
this.maxFileSize = maxFileSize;
this.maxRequestSize = maxRequestSize;
// Avoid threshold values of less than zero as they cause trigger NPEs
// in the Commons FileUpload port for fields that have no data.
if (fileSizeThreshold > 0) {
this.fileSizeThreshold = fileSizeThreshold;
} else {
this.fileSizeThreshold = 0;
}
}

public MultipartConfigElement(MultipartConfig annotation) {
location = annotation.location();
maxFileSize = annotation.maxFileSize();
maxRequestSize = annotation.maxRequestSize();
fileSizeThreshold = annotation.fileSizeThreshold();
}

public String getLocation() {
return location;
}

public long getMaxFileSize() {
return maxFileSize;
}

public long getMaxRequestSize() {
return maxRequestSize;
}

public int getFileSizeThreshold() {
return fileSizeThreshold;
}
}

其中 48 行与我们反编译的字节码文件匹配。明确原因后,我将工程里的依赖调整为:

1
2
3
4
5
6
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.5.56</version>
<scope>provided</scope>
</dependency>

调整后断点暂停的位置正常。我比较了 JavaEE 与 Tomcat 中关于 servlet-api 的类,发现其主要的不同在于注释部分,导致行号无法对上,JavaEE 中 MultipartConfigElement.java 的源码可以参考:servlet-spec/MultipartConfigElement.java at 3.1.0

Reference

Chapter 4. The class File Format - 4.7.12. The LineNumberTable Attribute
Java LineNumberTable: Entry explanation - Stack Overflow