<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></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 Jul 30, 2020, at 8:07 PM, 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 have almost finished porting LLDB to my architecture, now LLDB communicates well with my GDB stub of my simulator and can do debugging actions like breakpoint, continue, step, reading memory, reading<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=""> registers and so on. Thanks to all of your kind advice~<br class=""></font></div><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=""><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" class=""><br class=""></div><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" class="">Now I consider adding disassemble function. Our compiler is LLVM(of course after porting it to our new architecture) and a LLVM disassembler has already been implemented, which can do great objdump from the ELF file that LLVM generates, creating a .map file containing disassemble. LLDB leverages the disassembler from LLVM, so our LLDB can easily use that <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="">disassembler plug-in. Here comes problems:</font></div><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" 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="">I found that when LLDB deals with command "</font><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="">disassemble/dis/di", it would detect the current frame, confirm the disassemble address range, send a "m" packet to read the corresponding memory to get the program, and use LLVM </font><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=""><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="">disassembler plug-in to disassemble the code, which I would like to call it "dynamic disassembling". But in our architecture, program and data are in separated memory which have the same address space. When reading memory, we just read the data memory not the program memory, in other words, program/code cannot be accessed from processor, only from ELF file(program/code won't be altered during run time, we use hardware breakpoint). <br class=""></font></font></div><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" 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=""><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=""><br class=""></font></font></div><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" 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=""><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="">So:</font></font></div><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" 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=""><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="">Can LLDB do "static </font></font><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=""><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=""><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=""><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=""><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="">disassembling</font></font></font>"? (which just uses program/code from the ELF executable file without reading memory from the processor during run time)<br class=""></font></font></div></font></div></blockquote><div><br class=""></div>Yes it can. There are two ways to read memory:</div><div><br class=""></div><div>size_t Target::ReadMemory(const Address &addr, bool prefer_file_cache,<br class=""> void *dst, size_t dst_len, Status &error,<br class=""> lldb::addr_t *load_addr_ptr) {<br class=""><br class=""><br class="">size_t Process::ReadMemory(addr_t addr, void *buf, size_t size, Status &error)</div><div><br class=""></div><div><br class=""></div><div><br class=""></div><div>Process::ReadMemory always comes from the program you are debugging and will send a packet to the GDB remote if we are using ProcessGDBRemote.</div><div><br class=""></div><div>Target::ReadMemory has the option to read from sections in the object file if "prefer_file_cache" is set to true.</div><div><br class=""></div><div>In the "disassemble" command the following function does the work in lldb/source/Commands/CommandObjectDisassemble.cpp:</div><div><br class=""></div><div>bool CommandObjectDisassemble::DoExecute(Args &command, CommandReturnObject &result)</div><div><br class=""></div><div>It will call this function in lldb/source/Core/Disassembler.cpp:</div><div><br class=""></div><div>bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,<br class=""> const char *plugin_name, const char *flavor,<br class=""> const ExecutionContext &exe_ctx,<br class=""> const Address &address, Limit limit,<br class=""> bool mixed_source_and_assembly,<br class=""> uint32_t num_mixed_context_lines,<br class=""> uint32_t options, Stream &strm);</div><div><br class=""></div><div>Which then calls this function:</div><div><br class=""></div><div>size_t Disassembler::ParseInstructions(Target &target, Address start,<br class=""> Limit limit, Stream *error_strm_ptr,<br class=""> bool prefer_file_cache);</div><div><br class=""></div><div><br class=""></div><div>Back in Disassembler::Disassemble(...) the call site looks like:</div><div><br class=""></div><div> const bool prefer_file_cache = false;<br class=""> size_t bytes_disassembled = disasm_sp->ParseInstructions(<br class=""> exe_ctx.GetTargetRef(), address, limit, &strm, prefer_file_cache);<br class=""></div><div><br class=""></div><div><br class=""></div><div>So we are hard coding it to always read from memory. </div><div><br class=""></div><div>One way to fix this is to ask the lldb_private::Process subclass (ProcessGDBRemote in this case) if disassembly should prefer the file cache. For almost all architectures we want this to be false. For yours we want this to return true. The question now is how to detect this. One idea is to add key/value pairs to the response to qHostInfo or qProcessInfo queries that LLDB sends to the remote stub. For example, the darwin GDB server "debugserver" responds when debugging a simple a.out program with:</div><div><br class=""></div><div> < 13> send packet: $qHostInfo#9b<br class=""> < 166> read packet: $cputype:16777223;cpusubtype:8;ostype:macosx;watchpoint_exceptions_received:after;vendor:apple;os_version:10.15.5;maccatalyst_version:13.5;endian:little;ptrsize:8;#00<br class=""><br class=""></div><div><br class=""></div><div> < 16> send packet: $qProcessInfo#dc<br class=""> < 193> read packet: $pid:13cad;parent-pid:13cb0;real-uid:24069482;real-gid:6fd32dba;effective-uid:24069482;effective-gid:6fd32dba;cputype:1000007;cpusubtype:8;ptrsize:8;ostype:macosx;vendor:apple;endian:little;#00<br class=""><br class=""></div><div><br class=""></div><div>So it wouldn't be hard to add an extra key/value pair to either of these and then store them in the ProcessGDBRemote class. We would need to add a new virtual function to lldb_private::Process:</div><div><br class=""></div><div>class Process {</div><div> virtual bool PreferFileCacheForDisassembly() const {</div><div> return false;</div><div> }</div><div><br class=""></div><div>};</div><div><br class=""></div><div>Then we would override this in ProcessGDBRemote and it would return the right value.</div><div><br class=""></div><div>I am CC'ing in a few other GDB server experts directly for more feedback.</div><div><br class=""></div><div>Greg</div><div><br class=""><blockquote type="cite" class=""><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=""><br class=""><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" class="">Kind regards,</div><div style="display: block; font-size: 14px; font-family: Verdana; background-color: rgba(0, 0, 0, 0); font-weight: 400; font-style: normal;" class="">Rui</div></font><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>