[PATCH] Linux JIT support

This patch adds support for loading debug info from JITed code on Linux through a new JITLoader plugin. The patch was largely written by Keno Fischer.

The patch can be verified like this:

  • <Take any test.c program with a main()>

  • /build/bin/clang -O0 -g -emit-llvm -S test.ll test.c

  • /build/bin/lldb – /build/bin/lli --use-mcjit test.ll

  • br set -f test.c -l

  • r

  • bt (to see stack trace with filenames and lines)

Let me know if there’s anything that should be updated or if there is a better way to submit a larger patch like this.

Thanks,

Andrew

linux-jit.patch (53.2 KB)

Looks pretty good. A few changes I would like to see:

1 - switch away from using std::unique_ptr<> to use std::shared_ptr<> for the JITLoader instances:

    typedef lldb::JITLoaderSP (*JITLoaderCreateInstance) (Process *process, bool force);

Newer plugins are adopting this method in case we ever want to inherit from std::shared_from_this()...

2 - make a JITLoaderList class that encapsulates the storage details of the JITLoader objects (std::vector<std::unique_ptr<JITLoader>> is what you had, we should probably switch to std::vector<JITLoaderSP>) and also protects access to the list with a recursive mutex. LLDB is multi-threaded and we should make sure we never can hose this collection by accessing it from two threads. The class should probably have simple functions like:

class JITLoaderList
{
  JITLoaderList();
  ~JITLoaderList();
  void Append (const JITLoaderSP &jit_loader_sp);
  void Remove (const JITLoaderSP &jit_loader_sp);
  size_t GetSize() const;
  JITLoaderSP GetLoaderAtIndex (size_t idx);
  void DidLaunch();
  void DidAttach();
};

3 - switch any functions that were taking/returning "std::vector<std::unique_ptr<JITLoader>>" over to use the new JITLoaderList class.
4 - Process had these new members added:

+ JITLoaderList m_jit_aps;
+ bool m_did_load_jit_aps;

You might be able to switch over to:
    std::unique_ptr<JITLoaderList> m_jit_loaders_ap;

Then you accessor switches to be:

JITLoaderList &
Process::GetJITLoaders ()
{
    if (!m_jit_loaders_ap) {
  m_jit_loaders_ap.reset (new JITLoaderList());
        JITLoader::LoadPlugins(this, *m_jit_loaders_ap);
    }
    return *m_jit_loaders_ap;
}

5 - Any code that used to be using the iterators can just be replaced with the JITLoaderList::DidLaunch() and JITLoaderList::DidAttach() and the iteration can happen internally in the JITLoaderList class. If you need a generic iterator for the JITLoader objects inside a JITLoaderList class, you can make a:

    void
    ForEach (std::function <bool(JITLoader &)> const &callback);

In the JITLoaderList class. This can work well because thee "ForEach" function can take the mutex to protect the collection's integrity during iteration, and the "bool" return value from the function can indicate if iteration should continue or stop...

Hi Greg, thanks for the feedback. I’ve made the changes you requested here. Regarding #5, there’s no need right now for a generic iterator on the JITLoadersList so I left out that part, iteration only happens internally in the DidAttach() and DidLaunch() methods.

Let me know if there’s anything else that should be updated.

Cheers!
Andrew

linux-jit.2.patch (56.8 KB)

I would rename "gdbjit" to "gdb" as the JITLoaderGDB plug-in name, and then submit this. Nice patch.

Greg

Hi Greg, I’ve renamed the plugin to “gdb” here. I don’t have commit access so if someone else could commit it that would be great, like I mentioned earlier most of the credit for the patch goes to Keno Fischer.

Thanks!

Andrew

linux-jit.3.patch (56.8 KB)