Page MenuHomePhabricator

Remove Platform usages from NativeProcessLinux
AbandonedPublic

Authored by labath on May 18 2016, 7:47 AM.

Details

Summary

this removes the last usage of the Platform plugin in NPL. It was being used for determining the
architecture of a running process locating the ELF file belonging to the process and parsing it.
Given that we are already attached to the process we can replace this with a more reliable method
using the "official" ptrace API for that (it doesn't look very official, but this is what e.g.
strace uses).

Together with a followup change, which I am in the process of preparing, this will shrink
lldb-server by 4%--9% (mostly depending on how large the file is right now -- i.e., if it
includes instruction emulator plugins).

Tested on x86 and arm.

Diff Detail

Event Timeline

labath updated this revision to Diff 57622.May 18 2016, 7:47 AM
labath retitled this revision from to Remove Platform usages from NativeProcessLinux.
labath updated this object.
labath added a subscriber: lldb-commits.

@nitesh.jain: I have checked that this compiles, but I could not verify the functionality for lack of hardware. Could you give this a spin? I am particularly interested in the "debugging 32 bit process with a 64 bit server" use case as that is most likely to break. If it does, you can probably fix it by tweaking the number passed to GetThreadArchitecture -- it should be the size that the ptrace(GETREGSET, NT_PRSTATUS) returns for a 64-bit process.

@uweigand: This should not affect functionality in any way, as you don't do multiarch (afaik), but the changes to the register context were eyeballed, so I'm not sure if it actually compiles.

uweigand edited edge metadata.May 18 2016, 8:12 AM

This patch builds and works fine on s390x-linux.

nitesh.jain edited edge metadata.May 19 2016, 5:47 AM

Let me check and get back to you.

Thanks.

tberghammer accepted this revision.May 19 2016, 7:01 AM
tberghammer edited edge metadata.

Looks good with 1 minor comment inline. Feel free to ignore/postpone it if you want

source/Plugins/Process/Linux/NativeProcessLinux.cpp
2072–2073

Can we just remove this entry method as nobody is using it?

This revision is now accepted and ready to land.May 19 2016, 7:01 AM
nitesh.jain added inline comments.May 20 2016, 5:04 AM
source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
39

Ptrace call with ptrace request "PTRACE_GETREGSET" is failing for MIPS. We are currently investigating the reason for the failure. I will let you know asap.

Thanks

labath added inline comments.May 20 2016, 5:10 AM
source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
39

Thanks for checking it out. I'll hold this for now.

Out of curiosity, what is errno being set to after this? Also, in which scenario does this happen (64->64 or 64->32, 32->32)?

labath added inline comments.May 20 2016, 5:39 AM
source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
39

Also, what is the kernel version you are using? NT_PRSTATUS was apparently added in 3.13, although I'm not sure if all the all the necessary pieces were there already -- (it may have been enabled slightly later).

omjavaid accepted this revision.May 20 2016, 7:14 AM
omjavaid edited edge metadata.

Seems to be causing no regressions on arm-linux.

source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
53

We have tried executing ptrace(NT_PRSTATUS) on MIPS with 3.18. It is able to detect the arch correctly (64->64 and 64->32). However with 3.10 it fails with EIO. Is there any fallback method when ptrace(NT_PRSTATUS) fails?

labath planned changes to this revision.May 23 2016, 8:02 AM
labath added inline comments.
source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
53

Thanks for checking that out.

This makes things a bit tricky. We *could* fallback to the "parse the executable ELF" method of getting the architecture, but that would make this patch a bit pointless, as that is what I am trying to remove in the first place. I suppose we could make the fallback mips-only, but that will not be too ideal either.

I don't know about any other fallbacks. It would be interesting to know how strace does that. I've tried looking at strace source code, but I could not make much out of it. Do you know if a single strace binary is capable of correctly tracing both 32- and 64-bit processes? If so, could you try running the following experiment for me:

  • create a simple c file that does a sleep(1) and exits
  • compile 32- and 64-bit versions of the executable
  • execute the following commands:
strace -o /tmp/32.txt -- strace -o /dev/null -- a.out.32
strace -o /tmp/64.txt -- strace -o /dev/null -- a.out.64

Could you send me the outputs (32.txt, 64.txt). Maybe I'll be able to figure something out from that.

If that does not work, I may have to abandon this.

thanks.

nitesh.jain added inline comments.May 24 2016, 1:25 AM
source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
53

// 32 bit ELF
gcc-4.9 -g -O0 -mabi=64 -mips64r2 simple.c -o a.out.64

// 64 bit ELF
gcc-4.9 -g -O0 -mabi=32 -mips32r2 simple.c -o a.out.32

// Strace for 32 and 64 bit
strace -o /tmp/64.txt -- strace -o /dev/null -- ./a.out.64
strace -o /tmp/32.txt -- strace -o /dev/null -- ./a.out.32

// File command output for both file

a.out.64: ELF 64-bit LSB executable, MIPS, MIPS64 rel2 version 1 (SYSV), dynamically linked, interpreter /lib64/ld.so.1, BuildID[sha1]=6e0f92a8abee292b4b6462d79ec0420a3d8aaad0, for GNU/Linux 2.6.32, not stripped

a.out.32: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux 2.6.32, BuildID[sha1]=c4e09148526975f777e3e70bec85868616d3ce94, not stripped

In case of MIPS, ptrace always return 64 bit data irrespetive of Arch.

labath abandoned this revision.May 25 2016, 8:54 AM
labath added inline comments.
source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp
53

Thank you for helping me investigate this. I've been thinking about it a lot, and I don't see any other way to get around this limitation at the moment. I think we'll have to abandon this approach for now.

labath updated this revision to Diff 60256.Jun 9 2016, 3:17 PM
labath edited edge metadata.

Use an auxv-based approach for detecting the architecture in case of mips.

This revision is now accepted and ready to land.Jun 9 2016, 3:17 PM
labath added a comment.Jun 9 2016, 3:24 PM

@nitesh.jain could you give the new version one more spin. I tried it on the auxv files you sent me, and it should work, but maybe there are other issues we did not notice. And thank you again for the help and all the patience.

Everyone else: The new version has only mips-specific changes, so I don't think there any action for you here.

I will check it and let you know asap.

Thanks

nitesh.jain added inline comments.Jun 10 2016, 2:01 AM
source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.cpp
438

There are three flavours of ABI,

ABIpointer-sizeArchProcesss Type
O324Mips3232 bit
N324Mips6432 bit
N648Mips6464 bit

So not sure whether it will work for "N32", I will check and let you know asap.

labath added inline comments.Jun 10 2016, 12:20 PM
source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.cpp
438

Uh-oh. :)

So, I don't think the ABI should matter much to the native register context. What matters here is the registers and their sizes. I'm guessing the N32 thing is something like the x32 intel abi, wher all registers are 64-bit, but sizeof(void*) is 32-bit. In that case this function will not work correctly, as it will detect it as 32-bit (but the NT_PRSTATUS-based one would, I guess).

Does N32 work on pre-3.13 kernels? (Grepping the source seems to find mentions of it, but i don't know how well it actually worked).

I guess is back to drawing board with this one again. Could you please send me the contents of the N32 auxiliary vector (LD_SHOW_AUXV as well as the actual binary contents). I presume the one you sent me earlier was O32.

cheers,
pl

nitesh.jain added inline comments.Jun 13 2016, 2:15 AM
source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.cpp
438

In N32 ABI, all GPR register are 64 bit and sizeof(void *) is 32 bit.

Yes, N32 ABI works on pre-3.13 kernels. I have send earlier aux for O32 ABI .

Thanks,
Nj

labath abandoned this revision.Jun 14 2016, 7:00 AM

I am abandoning this in favor of D21324, which achieves the same result (no Platform plugin), but still uses the elf-parsing method to keep everything working as is now.

You can give it a try if you want, but I don't anticipate any architecture-specific problems as all the changes are in generic code. Thanks for the patience.

source/Plugins/Process/Linux/NativeProcessLinux.cpp
2072–2073

The function is pure virtual in the base class, so I cannot just remove it.