This is an archive of the discontinued LLVM Phabricator instance.

Add support for tracing hello-world application on NetBSD
ClosedPublic

Authored by krytarowski on Mar 26 2017, 7:23 AM.

Details

Summary

This patch is a stripped down from features a NetBSD process
code (patch is kept under 2k LOC). This code has assumption that
there is only one thread within a debugged process. The only
debugger trap supported is software breakpoint (TRAP_BRKPT).
The generic platform code requires to add dummy function for
watchpoints etc. These functions are currently empty.
This code is not the final platform support as is and it's treated as
a base to extend, refactor and address issues afterwards.

Supported features:

  • handle software breakpoints,
  • correctly attach to a tracee,
  • support NetBSD specific ptrace(2),
  • monitor process termination,
  • monitor SIGTRAP events,
  • monitor SIGSTOP events,
  • monitor other signals events,
  • resume the whole process,
  • get memory region info perms,
  • read memory from tracee,
  • write memory to tracee,
  • read ELF AUXV,
  • x86_64 GPR read and write code

For the generic framework include:

  • halt,
  • detach,
  • signal,
  • kill,
  • allocatememory,
  • deallocatememory,
  • update threads,
  • getarchitecture,
  • getfileloadaddress,
  • and others.

This code has preliminary AddThread code.

Out of interest in this patch:

  • exec() traps,
  • hardware debug register traps,
  • single step trap,
  • thread creation/termination trap,
  • process fork(2), vfork(2) and vfork(2) done traps,
  • syscall entry and exit trap,
  • threads,
  • FPR registers,
  • retrieving tracee's thread name,
  • non x86_64 support.

This code can be used to start a hello world application and trace it.

This code can be used by other BSD systems as a starting point to get similar
capabilities.

Sponsored by <The NetBSD Foundation>

Diff Detail

Repository
rL LLVM

Event Timeline

krytarowski created this revision.Mar 26 2017, 7:23 AM
kettenis edited edge metadata.Mar 26 2017, 8:04 AM

On OpenBSD the register context used in core dumps uses the same layout as ptrace(2). That's not the case on all OSes, but I believe that is the case for NetBSD as well. Would it be possible to re-use the register context data structures defined in the source/Plugins/Process/Utility/ directory?

On OpenBSD the register context used in core dumps uses the same layout as ptrace(2). That's not the case on all OSes, but I believe that is the case for NetBSD as well. Would it be possible to re-use the register context data structures defined in the source/Plugins/Process/Utility/ directory?

I will research it.

Right now I'm not sure how to optimize reading GPR. If possible I would reschedule it for later.

labath edited edge metadata.Mar 28 2017, 9:40 AM

I wasn't able to go into this too deeply, but here is what I have after a quick pass. I won't be able to review this thoroughly that soon, but I think it can go in after you take my comments into consideration.

source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
450 ↗(On Diff #93071)

this comment does not make sense in this context

620 ↗(On Diff #93071)

This will return a success value. You probably wan't return Error("Unimplemented"); or something like that.

624 ↗(On Diff #93071)

same here

666 ↗(On Diff #93071)

return Error("Unimplemented");

801 ↗(On Diff #93071)

you could probably use process id instead of WAIT_ANY. The reason we needed -1 on linux is because each thread is reported separately.

source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
32 ↗(On Diff #93071)

How about using LLDB_LOG here?

142 ↗(On Diff #93071)

"Unimplemented" (and below as well).

I wasn't able to go into this too deeply, but here is what I have after a quick pass. I won't be able to review this thoroughly that soon, but I think it can go in after you take my comments into consideration.

Thank you!

I will apply the changes as suggested.

Apply changes from review.

This code is used as a base for further improvements, I'm going to commit it... debugging hello world still works.

$ lldb ./hello                                                                                                                                                                       
(lldb) target create "./hello"
Current executable set to './hello' (x86_64).
(lldb) r
Process 3955 launched: './hello' (x86_64)
Hello world!
Process 3955 exited with status = 0 (0x00000000) 
(lldb) version
lldb version 5.0.0 (http://llvm.org/svn/llvm-project/lldb/trunk revision 298810)
(lldb) platform status
  Platform: host
    Triple: x86_64-unknown-netbsd7.99
OS Version: 7.99.66 (0799006600)
    Kernel: NetBSD 7.99.66 (GENERIC) #5: Tue Mar 28 17:42:09 CEST 2017  root@chieftec:/public/netbsd-root/sys/arch/amd64/compile/GENERIC
  Hostname: 127.0.0.1
WorkingDir: /public/lldb_devel
    Kernel: NetBSD
   Release: 7.99.66
   Version: NetBSD 7.99.66 (GENERIC) #5: Tue Mar 28 17:42:09 CEST 2017  root@chieftec:/public/netbsd-root/sys/arch/amd64/compile/GENERIC
(lldb) 
This revision was automatically updated to reflect the committed changes.