记录已执行的Java代码的行号 [英] Log line numbers of executed java code

查看:153
本文介绍了记录已执行的Java代码的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写PHP Web应用程序的一部分(将在高中错误查找比赛中使用),用户必须在该应用程序中查找给定Java程序中的错误。作为其一部分,当执行Java程序时,我们要突出显示执行了代码的Java程序源代码的各行。为此,我们所需要的只是已执行源代码的行号,即代码路径(或称为代码覆盖率?)。我们将使用行号突出显示源文件中的行。

I am writing part of a PHP web application (which will be used in a high school bug finding contest) where the user must find bugs in a given Java program. As a part of this, when the Java program executes, we want to highlight the lines of the source of the Java program where the code has executed. To do this, all we need are the line numbers of the source that have been executed, that is, the code path (or is it called code coverage?). We will highlight the lines in the source file using the line numbers.

我们将使用PHP的shell-exec()执行Java程序和该工具来获取代码路径(无论如何)。获得代码路径的行号最简单的方法是什么?

We will be using PHP's shell-exec() to execute the Java program and the tool to get the code path (whatever that will be). What is the easiest way of getting the line numbers of code path?

非常感谢!

这里是描述我们想要的图片

Here is a picture that describes what we would like

推荐答案

PHP会插入代码,这意味着每次您运行该程序时,它都会在源代码上运行。这样做的好处是在读取代码时会炸掉(这使行号打印输出变得微不足道);但是,由于无法进行深度优化(或执行任何运行前错误检查),因此在其他方面通常很昂贵。

PHP interperts the code, which means it runs over the source each time you run the program. This has the benefit of blowing up as the code is read (which makes line number printouts trivial); however, it often is expensive in other ways, as you cannot optimize deeply (or do any pre-runtime error checking).

Java将其代码编译为称为字节码的JVM汇编语言。正在运行的内容通常无法访问(甚至使用)源代码。也就是说,有技巧。编译后的Java类具有添加额外数据的能力,而这些额外数据元素之一是行号表,它是一个索引,允许运行程序集的人在编译器记录行号时查找该行号 。

Java compiles its code into a JVM assembly language called "bytecode." This means that what is running doesn't generally have access to (or even use) the source code. That said, there are techniques. A compiled Java class has the ability to add "extra data" and one of those "extra data elements" is a line number table, which is an index allowing someone running the assembly to "look up" the line number as the compiler recorded it.

这通常可以正常工作,但要考虑到以下因素:编译器通常不会标记每条指令,源代码可能不可用,优化可能会使某些内部块代码无法以有助于指向输入代码文本的方式起作用。

This generally works ok, with the considerations that: compilers often don't mark up every instruction, the source code may not be available, optimization might make certain inner chunks of code not function in ways that facilitate pointing to the input code text.

代码覆盖率工具如何修复这通常是它们插入到代码中(在汇编级别) )大量命令,这些命令实际上可以用作某种格式的日志记录语句,该格式允许该工具确定实际遵循了代码的哪个路径。然后,这会尽可能地通过行号表映射回去,然后用于突出显示原始源文件中的行。

How code coverage tools "fix" this is that they generally insert into the code (at the assembly level) a large number of commands that effectively act as logging statements to a format that allows the tool to determine which path through the code was actually followed. This is then mapped back through the line number table as best as possible and then used to highlight lines within the original source file.

如果您想要分辨率更高的东西(某些东西可以处理执行了一行的哪一部分),那么您需要更深入地研究。最终,您甚至可以考虑编写自己的编译器(或编译器扩展),该代码将存储您自己的自定义行号表,从而克服了当前解决方案的缺点。

If you want something with finer resolution (something that can process which portion of a line was executed) then you need to dig deeper. Eventually you might even consider writing your own compiler (or compiler extension) which will store your own custom line number table that overcomes the shortcomings of the current solutions.

技巧就像抛出异常(如Shiven提到的那样)并解析行号确实有效;但是,它们会使用奇怪的异常处理来污染您的代码,这些异常处理实际上是不是例外的项目,只是为了获得行号。由于代码混乱以及异常的运行时性能通常较差,我倾向于避免使用此类解决方案(但它们确实可以工作)。

Tricks like throwing exceptions (as Shiven has mentioned) and parsing out the line number do work; however, they pollute your code with odd exception processing for items that really aren't exceptional, just to "get the line number". Due to the code clutter and the generally poorer runtime performance of exceptions, I tend to avoid such solutions (but they do work).

无论如何,希望这会给您一个查看为什么它并不总是与PHP完全相同。

Anyway, hopefully this will give you a view as to why it doesn't always work exactly the same way as PHP.

这篇关于记录已执行的Java代码的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆