<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Jun 11, 2013, at 11:38 AM, Andrey Zaytsev <<a href="mailto:andrey.zaytsev@jetbrains.com">andrey.zaytsev@jetbrains.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Hello, Jim</div><div><br></div><div>The code I was debugging in described case is GCD-related stuff so the issue with locks could be the cause. But issue is gone once we<span class="Apple-converted-space"> </span><b>select</b><span class="Apple-converted-space"> </span>thread/frame - that's weird.</div><br><div><div>On Jun 11, 2013, at 3:38 AM,<span class="Apple-converted-space"> </span><a href="mailto:jingham@apple.com">jingham@apple.com</a><span class="Apple-converted-space"> </span>wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">One of the big dangers with evaluating expressions that run code in the debugee is that the code you run might try to acquire some locked resource that is held by another thread in the program. That will cause the expression evaluation to deadlock. <br><br>One solution is to run the expression evaluation with a timeout, and if it takes too long, cancel the expression, clean up its stack and return an error. However, that can also be dangerous, for instance if the expression you run successfully acquires lock A, tries to get lock B, but that is held on another thread, so it deadlocks. If you cancel the expression evaluation at that point, you will leave lock A stranded, and your program going to grind to a halt as different threads all try to get that resource.<br><br>The obvious solution to this problem is to run all threads when you do, expression evaluation. However, that's not what most users want, they would find it disconcerting to have expression evaluation on one thread to cause some other thread to make progress.<br><br>lldb's solution to this problem is that when we run expressions, we first try running just the thread on which the expression is evaluated, but with some timeout (which is user settable.) If the expression evaluation times out on the one thread, and the "run_others" parameter to RunThreadPlan is passed in as true, we will interrupt the evaluation, and then restart with all threads running. So it is very possible that expression evaluation could cause another thread to get a chance to execute, which could in turn hit a breakpoint, or crash, or whatever else a thread might do while executing...<br><br>Short of the ability to track all locks in the system (a capability which most OS'es don't provide), this is the safest way around this problem. It will fail if the expression you are running tries to acquire a non-recursive lock held on the thread which runs the code. We could work around even that if we made up a debugger thread in the debugee for running expressions, though that would fail if any of the code in the expression tried to access thread specific data. I don't know whether it is possible to fake a thread's thread specific data so it looks like it belongs to another thread. If that's possible, we could probably make that work. OTOH, this particular problem doesn't seem to occur that often.<br><br>Anyway, again without being able to play around with the locks in a program directly, I can't see a way to run general expressions that might acquire locked resources without allowing all threads to run, at least as a fallback.<br></blockquote><div><br></div><div>What does it mean for debugger's front-end? What shall we see after evaluation? Threads will stop in different frames and do we need to refetch all info about all threads/frames/variables to display correct data?</div></div></div></blockquote><div dir="auto"><br></div><div dir="auto">Yes, if other threads get a chance to run, you will need to refresh state. There isn't any notification that this has happened at present. I think it would be hard for us to know that other threads actually got a chance to run, all we can tell is that it could have happened. But still, it might be a good idea to return that fact from the expression evaluation somehow...</div><div dir="auto"><br></div><div dir="auto">Jim</div><br><blockquote type="cite"><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div><div><br></div>Thank you Jim, for such a detailed clarification about LLDB internals. I'll try to investigate the issue deeper and write back if I have some questions.</div><div><br><blockquote type="cite"><br></blockquote><blockquote type="cite">Jim<br><br>On Jun 10, 2013, at 4:18 PM, Andrey Zaytsev <<a href="mailto:andrey.zaytsev@jetbrains.com">andrey.zaytsev@jetbrains.com</a>> wrote:<br><br><blockquote type="cite">Hi Jim,<br><br>You're right that it uses correct context of frame specified. But as far as I can see, real execution is running in other thread. May it cause something like EXC_ARM_BREAKPOINT?<br>And what run_others parameter of Process::RunThreadPlan() does?<br><br><br>On Jun 11, 2013, at 2:07 AM,<span class="Apple-converted-space"> </span><a href="mailto:jingham@apple.com">jingham@apple.com</a><span class="Apple-converted-space"> </span>wrote:<br><br><blockquote type="cite">That is not the intention of the design, and also not what I see:<br><br>(lldb) source list -f foo.c -l 1<br> 1 <span class="Apple-tab-span" style="white-space: pre;"> </span>#include <stdio.h><br> 2 <span class="Apple-tab-span" style="white-space: pre;"> </span><br> 3 <span class="Apple-tab-span" style="white-space: pre;"> </span>int<br> 4 <span class="Apple-tab-span" style="white-space: pre;"> </span>foo (int input)<br> 5 <span class="Apple-tab-span" style="white-space: pre;"> </span>{<br> 6 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> int local_var = input * 5;<br> 7 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> printf ("Local var: %d.\n", local_var);<br> 8 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> return local_var;<br> 9 <span class="Apple-tab-span" style="white-space: pre;"> </span>}<br> 10 <span class="Apple-tab-span" style="white-space: pre;"> </span><br> 11 <span class="Apple-tab-span" style="white-space: pre;"> </span>int<br>(lldb)<span class="Apple-converted-space"> </span><br> 12 <span class="Apple-tab-span" style="white-space: pre;"> </span>main (int argc, char **argv)<br> 13 <span class="Apple-tab-span" style="white-space: pre;"> </span>{<br> 14 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> int local_var = argc;<br> 15 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> printf ("Foo returns: %d.\n", foo (local_var));<br> 16 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> return 1;<br> 17 <span class="Apple-tab-span" style="white-space: pre;"> </span>}<br>(lldb) b s -p "return local_var"<br>Breakpoint 1: where = foo`foo + 41 at foo.c:8, address = 0x0000000100000ef9<br>(lldb) run<br>Process 98518 launched: '/private/tmp/foo' (x86_64)<br>Local var: 5.<br>Process 98518 stopped<br>* thread #1: tid = 0x3663ec, function: foo , stop reason = breakpoint 1.1<br> frame #0: 0x0000000100000ef9 foo`foo at foo.c:8<br> 5 <span class="Apple-tab-span" style="white-space: pre;"> </span>{<br> 6 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> int local_var = input * 5;<br> 7 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> printf ("Local var: %d.\n", local_var);<br>-> 8 <span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-converted-space"> </span> return local_var;<br> 9 <span class="Apple-tab-span" style="white-space: pre;"> </span>}<br> 10 <span class="Apple-tab-span" style="white-space: pre;"> </span><br> 11 <span class="Apple-tab-span" style="white-space: pre;"> </span>int<br><br>So I am in foo, there is a "local_var" in frame 0, where its value is 5, and in frame 1 where its value is 1. So I do:<br><br>(lldb) script<br>Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.<br><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">frame_0 = lldb.thread.GetFrameAtIndex(0)<br>print frame_0.EvaluateExpression("local_var")<br></blockquote></blockquote></blockquote>(int) $0 = 5<br><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">frame_1 = lldb.thread.GetFrameAtIndex(1)<br>print frame_1.EvaluateExpression("local_var")<br></blockquote></blockquote></blockquote>(int) $1 = 1<br><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">quit()<br></blockquote></blockquote></blockquote><br>SBFrame::EvaluateExpression, at least in this example, works on the SBFrame that you call it on.<br><br>Note, here I am using lldb.thread, which is INDEED the command interpreter's currently selected thread. I just did that to shorten the example. If you wanted to get the process/thread/frame independent of entities selected in the Command Interpreter, then you should use the accessors on SBDebugger, SBTarget, SBProcess and SBThread. The Python docs explicitly state that you should NOT use lldb.process/lldb.thread or lldb.frame anywhere but in the interactive script interpreter. They aren't even guaranteed to be set in other contexts.<br><br>Anyway, if you have some example (not using lldb.process/lldb.thread/lldb.frame) where the EvaluateExpression on a SBFrame object is evaluating the expression is the context of some other frame, please file a bug and we will take a look.<br><br>Jim<br><br><br>On Jun 10, 2013, at 1:46 PM, Andrey Zaytsev <<a href="mailto:andrey.zaytsev@jetbrains.com">andrey.zaytsev@jetbrains.com</a>> wrote:<br><br><blockquote type="cite">Hello everyone.<br><br>I just realised thing which leads to crash of debuggee in some cases. We had a bug in our tracker:<span class="Apple-converted-space"> </span><a href="http://youtrack.jetbrains.com/issue/OC-7389">http://youtrack.jetbrains.com/issue/OC-7389</a><br><br>We have some system of value renderers. Each renderer(e.g for NSCollections) evaluates some stuff to get info about collection elements. So does a number of Summary and Synthetic Providers too.<br><br>In SB-API it is implemented with EvaluateExpression function. One of the ways we can evaluate expression is to call lldb::SBFrame::EvaluateExpression() member function. Actually it performs execution on selected thread/frame. But not on the frame we call EvaluateExpression function on. It's very not obvious and in my opinion buggy. Usage of API in this way leads to crashes of debuggee process like in the ticket above. So crashes not only attempt to evaluate expression but attempt to get local variables with dynamic types if it executes target as well.<br><br>So workaround for us was to select specified thread/frame before doing evaluation. So does interpreter's expr command.<br><br><br>--<span class="Apple-converted-space"> </span><br>Andrey Zaytsev<br>Software Developer<br>JetBrains, Inc<br><a href="http://www.jetbrains.com/">http://www.jetbrains.com/</a><br>"Develop with pleasure!"<br><br>_______________________________________________<br>lldb-dev mailing list<br><a href="mailto:lldb-dev@cs.uiuc.edu">lldb-dev@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/lldb-dev</blockquote></blockquote></blockquote></blockquote></div></div></blockquote></div><br></body></html>