<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Apr 16, 2020, at 2:30 AM, Rui Hong via lldb-dev <<a href="mailto:lldb-dev@lists.llvm.org" class="">lldb-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">Hi LLDB devs,<div class=""><br class=""></div><div class="">I'm working on porting LLDB to work with an existing simulator(which has GDB stub, remote debugging). This simulator used to work with GDB. When using with GDB, the target file(ELF) is loaded by GDB comma<font style="display: inline; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" __editorwarp__="1" class="">nd "load" or "remote put".</font></div><div class=""><font style="display: inline; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" __editorwarp__="1" class="">From a LLVM talk project which is very similar to my project, their </font>target file is loaded by the simulator itself(   ./sim a.out   , something like that), and lldb sets breakpoint, use "gdb-remote" command to connect to the simulator, the program starts to run immediately and stop at the breakpoint.</div><div class="">I can't find any lldb command that is equal to "load" in GDB. And right now when I use "gdb-remote" to connect lldb to my simulator, lldb has command line output "Process 10115 stopped,thread #1, stop reason = signal SIGTRAP,frame #0: 0xffffffffffffffff". Does this mean the program has already started to run? I haven't loaded the binary.</div><div class=""><br class=""></div><div class="">To sum my questions:</div><div class="">1. Does lldb has similar command like "load/<font style="display: inline; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" __editorwarp__="1" class="">remote put</font>" in GDB?</div></div></blockquote><div><br class=""></div>Yes: "target modules load" with the --load option. Te be fair the Ted Woodward, this code didn't exist when he was bringing up his simulator way back when, but it was later added by someone.</div><div><br class=""></div><div>The "target modules load --load --file /path/to/file" can be used to load an ELF file as a dynamic loader would load the ELF file: it will only load the program headers that are PT_LOAD. You can also use the "--set-pc-to-entry" option to this command to set the PC to the entry point that is specified in the ELF file. For this to work, you ELF file has to be statically linked and all addresses should be correct in the file itself. </div><div><br class=""></div><div>If you want to just blast an entire ELF file into memory and not only load PT_LOAD segments, then you can use "memory write --infile /path/to/binfile <addr>".</div><div><br class=""></div><div>Hopefully "target modules load --load" will work for you.</div><div><br class=""></div><div>The code for this command is in CommandObjectTarget.cpp in CommandObjectTargetModulesLoad::DoExecute() if the second "if (load)" statement (line CommandObjectTarget.cpp :2827 for me). What it does is it will ask the ObjectFile for the file you are loading in the command (ObjectFileELF) about what its "loadables" are:</div><div><br class=""></div><div>std::vector<ObjectFile::LoadableData> loadables(objfile->GetLoadableData(*target));<br class=""></div><div><br class=""></div><div>So as long as this function returns what you are interested in loading, then you should be good.</div><div><br class=""></div><div><br class=""></div><div><blockquote type="cite" class=""><div class=""><div class="">2. Does "gdb-remote" command in lldb do the "loading binary" job?</div></div></blockquote><div><br class=""></div><div>It the invocation _can_ do the loading, but it really depends on the GDB server. Typically there are a few ways to use GDB server:</div><div><br class=""></div><div>1 - have _it_ launch the program for you:</div><div>   $ gdbserver [options] -- /path/to/a.out arg1 arg2 arg2</div><div>2 - just launch a GDB server which you will connect to</div><div>   $ gdbserver [options]</div><div>3 - have gdb server attach to a process for you:</div><div>   $ gdbserver [options] --pid 123</div><div class=""><br class=""></div><div class="">When LLDB attaches to a GDB server, it will ask it if it has a process with some GDB remote packets, and if it has one, it is good to go and start debugging (solution 1 and 3 above). If it doesn't, it can send a launch ("A" packet) or attach packet to attach to a process or wait for one (solution 2 above).</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="">3. Will the program start to run immediately after "gdb-remote" command in lldb?</div></div></blockquote><div><br class=""></div>If you have a GDB server, it will have the process stopped because the simple packet send/response doesn't allow it to receive a packet if the process is running. So usually a GDB server will start off stopped and the debugger can auto continue or stop at this entry point (process launch --stop-at-entry 1).</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class="">4. Do I have to let my simulator to load the binary by itself?<br class=""></div></div></blockquote><div><br class=""></div>This is really up to your workflow and how complex your simulator is. If you are only ever loading one executable, then it might be easier to just have your simulator do this launching and stop your simulation right at the entry point and have the GDB server it vends respond to LLDB with information about the process. If you don't have a real process ID, make sure your GDB server makes one up (like process ID of 1). If you want to see the packet traffic, then you can do this prior to sending the "gdb-remote <port>" command in lldb:</div><div><br class=""></div><div>(lldb) log enbable -f /tmp/packets.txt gdb-remote packets</div><div>(lldb) gdb-remote 1234</div><div><br class=""></div><div>We have extended our GDB remote server with new packets. So be sure to read up on what your GDB server _can_ provide, not mandatory, but many of these packets are useful and can provide detailed information to ensure LLDB selects the right targets and plug-ins. Details are in the LLDB code at:</div><div><br class=""></div><div>lldb/docs/lldb-gdb-remote.txt</div><div><br class=""></div><div>If you want to see how these are used, try debugging on a mac or linux and look at the packet log to see how a debug session goes.</div><div><br class=""></div><div>Tricky things you might need to do:</div><div>- make sure your GDB server details the registers that are available for your architecture. This can happen with the xmlRegisters response to the qSupported packet that will follow up with a packet that asks your GDB server for full details on your register for your emulated CPU. It can be very handy to ensure this has all of the info that is needed and allows your simulator to emulate different chips seamlessly. If your stub doesn't support this, consider adding that support. If your stub doesn't support his packet, then you will need to create a register definition python file and set the following setting:</div><div><br class=""></div><div>(lldb) setting set plugin.process.gdb-remote.target-definition-file /path/to/<arch>_target_definition.py</div><div><br class=""></div><div>Example files are available in the LLDB sources with a "_target_definition.py" suffix. For example:</div><div><br class=""></div><div>lldb/examples/python/x86_64_target_definition.py</div><div><br class=""></div><div>- there are also some packets that allow your GDB server to specify where files ended up being loaded. Full blown OS plug-ins will end up using dynamic loader plug-ins that interface with the process to figure out where shared libraries have been loaded. Your GDB server can respond to the "qXfer:libraries-svr4:read" packet to tell LLDB where any files have been loaded. If you just have one file, then using "target modules load --load --file /path/to/a.out" should work and nothing more is needed.</div><div><br class=""></div><div>Let us know how things go!</div><div><br class=""></div><div>Greg</div><div><blockquote type="cite" class=""><div class=""><div class=""><br class=""></div><div class="">Kind regards,</div><div class="">Rui</div><div class=""><br class=""></div>_______________________________________________<br class="">lldb-dev mailing list<br class=""><a href="mailto:lldb-dev@lists.llvm.org" class="">lldb-dev@lists.llvm.org</a><br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev<br class=""></div></blockquote></div><br class=""></body></html>