<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Menlo-Regular;
        panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>lldb-mi, in <lldb>/tools/lldb-mi , loads the shared library and uses the SB APIs. That might be a good start for you. main() is in MIDriverMain.cpp.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'><o:p> </o:p></span></p><div><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>--<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>Qualcomm Innovation Center, Inc.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project<o:p></o:p></span></p></div><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'> lldb-dev [mailto:lldb-dev-bounces@lists.llvm.org] <b>On Behalf Of </b>meister via lldb-dev<br><b>Sent:</b> Tuesday, August 29, 2017 1:41 PM<br><b>To:</b> clayborg@gmail.com<br><b>Cc:</b> lldb-dev@lists.llvm.org<br><b>Subject:</b> Re: [lldb-dev] Linking the lldb C++API/library with other projects<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Greg,<o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>We are developing a compiler for Common Lisp that uses LLVM as the backend and interoperates with C++ - it has its own REPL and built in compiler.   <o:p></o:p></p></div><div><p class=MsoNormal>Our compiler generates llvm-ir that we link directly with llvm-ir generated from the C++ code using LTO.<o:p></o:p></p></div><div><p class=MsoNormal>I’ve exposed much of the LLVM C++ API and Clang ASTMatcher C++ API for automatic analysis and refactoring of our C++ code.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>The Python API’s are not that useful to us.   <o:p></o:p></p></div><div><p class=MsoNormal>Although - maybe launching lldb as a separate process to get the backtrace with a python script may be a reasonable thing to do - if that’s all that I want to do.<o:p></o:p></p></div><div><p class=MsoNormal>I’d also like to explore accessing lexical variables and setting breakpoints and watchpoints using the C++ API.<o:p></o:p></p></div><div><p class=MsoNormal>The C++ API’s - they are much more straightforward to work with for us.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>I am already using ‘backtrace’ - but I don’t get function arguments with that sadly.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Best,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>.Chris.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal>On Aug 29, 2017, at 2:30 PM, Greg Clayton <<a href="mailto:clayborg@gmail.com">clayborg@gmail.com</a>> wrote:<o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal><o:p> </o:p></p><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal>On Aug 29, 2017, at 11:17 AM, meister <<a href="mailto:chris.schaf@verizon.net">chris.schaf@verizon.net</a>> wrote:<o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'>Dear Greg,<br><br>Thank you very much for your detailed and thoughtful response.<br><br>A couple of followup questions based on what you said:<br><br>(1)  You say: "since LLDB can't be used to backtrace itself…"<br>Do I (a) need to fork another process and call the LLDB API’s to get backtraces for the original process or (b) can I simply create another thread and call LLDB API’s to interogate other threads using the SBThread API?  Or can I do both of these?</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>You can do the first, not the second. LLDB attaches to processes via ptrace which means it will suspend all threads in the process it is attaching to, so you can't do this to yourself. Forking another process will do the trick. You can also do all of this from Python! The entire LLDB API is exposed to python and python can be used on the command line from a python script, or from within LLDB itself by using the "script" command which drops you into an embedded python interpreter.  On Mac, if you know Xcode is installed, you can just run a python script to do what you need. Let me know if this sounds interesting. On linux, you could use the installed lldb to do the backtraces using a python script as well.<o:p></o:p></p></div><div><p class=MsoNormal><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'><br>(2) When you say "so if you are looking to backtrace things in your current process you should probably use other APIs.”<br>By "other APIs” do you mean other SBxxxx class API’s like SBThread? or do you mean other API’s entirely? If the latter could you give an example?</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Other APIs not in LLDB if you must stay in process:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>$ man backtrace<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>If you launch another process, it will be able to backtrace your current process and you can use LLDB's APIs.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'><br>(3) If I call LLDB from my code like this - how would you recommend distributing this?<br><br>(a) When building from source should I have the build system pull lldb from a the llvm github repo?</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>You might think about locking onto they latest public release of lldb. This might be more stable than just grabbing top of tree.<o:p></o:p></p></div><div><p class=MsoNormal><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'>(b) Can I ship the lldb framework on OS X and lldblib.so (LInux) with my binary release?</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>Yes. You might think about using python and just using the installed LLDB? <o:p></o:p></p></div><div><p class=MsoNormal><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'>(c) On OS X - can I use the builtin lldb library? I haven’t checked if header files are available.</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>Header files are not available, but our API is stable. You could theoretically link against the top of tree LLDB.framework and tell it to use the system version (in /Applications/Xcode.app/Contents/SharedFrameworks, or somewhere for linux)<o:p></o:p></p></div><div><p class=MsoNormal><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'>(d) On Linux - can I use package manager installed versions of lldb?<span class=apple-converted-space> </span></span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>Yes. If you go this route, I would suggest using a python script. Then you don't even need to link against lldb! <o:p></o:p></p></div><div><p class=MsoNormal><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'><br>For some of these I realize that I'll have to do some legwork to figure out what is available from package managers etc.<br><br>(4) Since I have to debug from a separate process does the following sound reasonable.<br>(i) My program detects an error and enters into its debugger.<br>(ii) It forks a debugging process and that interacts with the user who uses it to debug the main process.<br>(iii) The debugger process shuts down and the main process continues.<br><br>I’d be doing this from within a Common Lisp programming environment called SLIME in Emacs - I have no idea right now if it’s possible to have the integrated debugger in SLIME work with a separate debugging process. Fun, fun, fun.</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>That sounds like it can work. If you want to actually use the debugger when there is an issue, you will not want to use python. But if you are just trying to get backtraces, then a  python script might work, plus it will insulate you from having to link against lldb.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Menlo-Regular",serif'>Thank you!</span><o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>No worries, we are here to help!<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><p class=MsoNormal>Let me know what you think about my above comments,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Greg Clayton<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div></div><p class=MsoNormal><o:p> </o:p></p></div></div></blockquote></div><p class=MsoNormal><o:p> </o:p></p></div></div></div></body></html>