Unknown Source appearance and Solution

Source: Internet
Author: User

Yesterday I wrote a piece of code in the company, which is very simple. It is to test the use of the dumpStack method of Thread. Because the dumpStack method of Thread is not very commonly used, it is very helpful if you want to see who calls the method at runtime. Return to the topic and view the output result:
Java. lang. Exception: Stack trace
At java. lang. Thread. dumpStack (Unknown Source)
At Common. getInfo (TestDumpStack. java: 21)
At TestDumpStack. main (TestDumpStack. java: 7)
You can see that at java. lang. Thread. dumpStack (Unknown Source) is displayed in the second output line ). Why is the name and number of lines of the Source code file displayed when you write the code, and the jdk class library displays the Unknown Source?
I believe many people may encounter the headache of Unknown Source when debugging code, using log tools to print stack exception information, and checking the debugging information of the line where the code is located. So how did this thing come from?
A. Where does Unknown Source come from?

Unknown Source, as its name implies, is an Unknown Source file. The reason for this problem is that there is no source file debugging information in the class file. Why is there no debugging information in the class file? The answer is simpler. Of course, we didn't specify the debugging information when compiling with the javac command. Because many people are used to writing code using some existing IDES such as eclipse, few people are familiar with jdk's own javac, java, detailed parameters of some commands such as jdb (some jdk commands may be different from those of eclipse ). Haha, But if you often play java in linux, the command will certainly be very familiar. Let's take a look at some important javac parameters:
-G-Generate all debugging information, including local variables. By default, only line number and source file information is generated. all debugging information is generated in the class file, including local variable information. By default, only the row number and source file information of the source code are written.
-G: none-Do not generate any debugging information. No debugging information is generated.
-G :( lines, vars, source)-only part of the debugging information is generated (source code line number, variable, source file information ). Then we will introduce the meanings of lines, vars, and source respectively.
Lines: writes the row number information in the source file to the Class file. This attribute is used to generate a ing between the method byte bitstream offset and the source code line number in the Class file. If this attribute is not specified, we will not see the printed row number in the stack exception information.
The vars: Local variable attribute establishes a ing relationship between the Local variable content in the method stack frame and the Local variable name and descriptor in the source code. With this attribute, we can see the value of the variable during debugging.
Source: If this attribute is specified during compilation, the attribute information of the source file, such as the source file name, is written to the class file.
After talking about this, beginners may be confused. Why do compilation need to specify the debugging information? Haha, if the compilation does not specify the debugging information, how do you debug it? If you do not specify the row number information, you cannot insert breakpoints in ide. This debugging information is very important when we debug the program. However, these compilation options are already default in ide such as eclipse. Some may not believe this yet. Open eclipse and choose Window> Preferences> Java> Compiler. You can see a Classfile Generation at the bottom of the page, by default, all four options are selected.
Which of the following is not specified when the Unknown Source is compiled? After testing, I found that the source option is not specified during javac compilation, so the Unknown Source issue is inevitable.
PS1: in linux, many people use ant to compile javac tasks, and often encounter Unknown Source problems when viewing stack exceptions. When ant is compiled, it is equivalent to specifying-g: none by default, and does not generate any debugging information. Therefore, to view the source code and line number information in log analysis, you must change the dubug attribute in build. xml.
PS2: I think it will be helpful to see the source code package of Log4j's log operation class.

B. in the beginning of the Code introduction, why does the code written by myself show the number of lines with Stack exceptions, while the jdk class library (rt. jar-Runtime Java Archive) the code will show Unknown Source?
The answer is simple, because we directly use the class file compiled by jdk. When the rt. jar source code is compiled and packaged, the debugging information is not put into the class file. Therefore, the Unknown Source is displayed. In fact, the principle is very simple. Under normal circumstances, sun's class library certainly won't have bugs. It must have been debugged many times before, so there is no need to add debugging information, you only need to use it. Therefore, the Unknown Source is normal.
PS: Actually, I think this is similar to the software development version. Generally, dubug and release versions are available. The debug version contains debugging information. However, after the official release, debugging information is certainly not included. If debugging information is included, the version may occupy a lot of space, and debugging information is not required at all.

C. What if we have to trace and debug jdk class libraries such as rt. jar?
Because rt. jar does not contain debugging information during compilation and packaging. If you just want to see the call process, you only need to specify the src.zip of the jdkinstallation directory in Source attachmentunder rt. jar in eclipse. However, if you want to track the variable values of the jdk class library, this will not work. In addition to other methods, you can compile the original src.zip file, specify the compilation parameters, and overwrite the original rt. jar file with the newly compiled rt. jar file. This is completely OK.

D. What if we want to debug other class files without source code?
In fact, it is not difficult to use the decompilation tools such as jad to compile the source code before debugging. However, the premise is that the class file has debugging information.

PS: I saw some code printing stack exception information on the Internet and found that some of them printed the number of lines in the jdk source code. For example
At java. lang. Thread. dumpStack (Thread. java: 1206. I'm curious, probably because I re-compiled the jdk source code or may use different ides or different jdk versions. This is to be verified. If you have any children's shoes, you can contact me.

I finally finished writing. There may be a lot of things to correct. I hope you don't give me any advice. It was a.m. It's time to go to bed. Say good night at dawn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.