Delphi XE 中调试代码
我是一名长期的 Delphi 开发人员,过去我在开发时使用第三方工具进行日志记录和调试(称为 Smart Inspect),但是现在我已经升级到 Delphi XE,我想尝试使用 IDE 进行调试。
我的问题是,给定一个函数,比如
procedure MyFunction;
var
str : string;
begin
str := 'Foo';
//Debug, show value of str?
str := AnotherFunction(str);
//Debug, show value of str?
end;
我如何调试并获取 str 的值,而不做类似的愚蠢事情 显示消息(str);
如果某处有视频(或文章),那么我非常乐意阅读。
有没有办法观察/输出变量的值。
I am a long time Delphi dev and in the past I use a third party tool for logging and debugging while developing (called Smart Inspect) however now that I've upgraded to Delphi XE I want to try and use the IDE for debugging.
My question is, given a function like
procedure MyFunction;
var
str : string;
begin
str := 'Foo';
//Debug, show value of str?
str := AnotherFunction(str);
//Debug, show value of str?
end;
how can I debug and get the value of str, without doing stupid things like
showmessage(str);
if there is a video somewhere (or article) then I am more than happy to read.
Is there a way to watch/output the value of variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只想使用 IDE 调试器,请执行以下操作:
此类消息将发送到“事件日志”调试器窗格。右键单击窗格并选择“属性”。在那里,您可以根据需要过滤(“消息”)/突出显示(“颜色”)跟踪消息。
If you want to use the IDE Debugger only, then do the following:
Such messages will be send to "Event Log" debugger pane. Right click on the pane and choose "Properties". There you can filter ("Messages") / hilight ("Colors") the trace messages as you whish.
嗯,Delphi XE 带有 CodeSite 日志记录,但我感觉你在谈论调试器本身。
如果在例程中放置断点,调试器在遇到断点时就会中断。从那里,您可以在 IDE 左侧看到“本地变量”窗格和“监视”窗格。局部变量将显示所有局部变量的值,而监视允许您设置将跟踪其值的表达式。
您还可以使用 Inspect (
Alt-F5
) 获得类似于手表的内容,但具有更详细的信息(尤其是对象等结构化类型)。此外,评估/修改 (Ctrl-F7
) 将允许您输入表达式并评估它们。它不像 Inspect 那样详细,但它为您提供了更多的灵活性。如果您熟悉这些工具,您会发现调试更加容易。
Well, Delphi XE comes with CodeSite logging, but I get the feeling you're talking about the debugger itself.
If you place a breakpoint in a routine, it will break to the debugger when it hits it. From there, you've got a Local Variables pane and a Watches pane along the left side of the IDE. Local Variables will show the value of all locals, and Watches lets you set up expressions whose value it will keep track of.
You can also get something similar to a watch, but with more detailed information (especially for structured types such as objects) by using Inspect (
Alt-F5
). Also, the Evaluate/Modify (Ctrl-F7
) will allow you to type in expressions and evaluate them. It's not quite as detailed as Inspect, but it gives you a lot more flexibility.If you familiarize yourself with these tools, you'll find debugging much easier.
1) 您可以使用 OutputDebugString 函数将字符串输出到调试窗口
2) 您可以使用 CodeSite Express。我推荐使用 CodeRage 5 中的视频作为使用 CodeSite 的起点
1) You can use OutputDebugString Function to output string to debug window
2) You can use CodeSite Express. I recommend video from CodeRage 5 as a starting point for using CodeSite
其他答案都是正确的。
我个人最喜欢的技术(与 da-soft 的答案相同)是创建一个断点,将一条消息记录到事件日志中,其中包含我想要记录的值,并且实际上并不“中断”(即,执行继续无需点击“运行”图标)。然后,每次到达该行代码时,我都会收到消息以及日志中的值。由于我可以返回并读取历史记录以及查看当前值,因此我发现这比仅使用调试器监视窗口更有用。
但由于 Delphi XE 包含 CodeSite,因此您可以做的远远超出断点中的表达式求值的范围。但是,代码站点要求您修改代码以添加一些日志记录。但它比消息框好得多。
您还可以使用 OutputDebugString(PChar(s)) 将任何字符串输出到调试器。由于它可以包含您想要的任何内容,因此这是一种非常好的调试方式,但不会向最终用户显示内容。
在我的许多应用程序中,我有一个特殊的跟踪缓冲区,它是循环的(也就是说,它只保留最后 500 行左右)。当我看到问题时,我不仅会得到堆栈回溯,还会保存内存中的跟踪日志,因此我有一些关于问题发生之前发生的情况的历史记录。
您还可以查看 Log 4 Delphi 项目。
Other answers are all correct.
My personal favorite technique (same as the answer by da-soft) is to create a breakpoint, that logs a message to the event log, containing a value that I want logged, and does not actually "break" (that is, execution continues without you hitting the Run icon). Then every time that line of code is reached, I get my message, and my values in the log. Since I can go back and read the history, as well as see the current values, I find this more useful than merely using the debugger watch window.
But since Delphi XE includes CodeSite, you can go far beyond what expression evaluation in breakpoints does for you. Code Site however requires that you modify your code to add some logging. But it's much better than a message box.
You can also use OutputDebugString(PChar(s)) to output any string to the debugger. Since this can contain whatever you want, it's a very nice way to debug but not show stuff to the end user.
In many of my applications, I have a special trace buffer, which is circular (that is, it keeps only the last 500 or so lines). When ever I see a problem, not only do I get a stack traceback, I also save that in-memory trace log, so I have some history on what was going on just before my problem.
You can also check out the Log 4 Delphi project.
我更喜欢调试器提示。进入调试器后,将鼠标移动到代码中任意位置的“str”,您将看到其当前值。您还可以通过鼠标突出显示某些语句并对其进行评估。例如,突出显示“AnotherFunction(str)”并将鼠标放在其上。
I prefer debugger hints. After breaking to the debugger move your mouse to the "str" anywhere in your code and you will see its current value. Also you can highlight some statement by a mouse and evaluate it. For example highlight "AnotherFunction(str)" and place your mouse over it.
其他答案都没有问题,但我只是想添加这些有用的功能。
Nothing wrong with any of the other answers but I just wanted to add these useful functions.