Page MenuHomePhabricator

Implement basic DidAttach and DidLaunch for DynamicLoaderWindowsDYLD
AcceptedPublic

Authored by sas on Oct 25 2017, 6:19 PM.

Details

Reviewers
clayborg
zturner
Summary

This commit implements basic DidAttach and DidLaunch for the windows
DynamicLoader plugin which allow us to load shared libraries from the
inferior.

This is an improved version of D12245 and should work much better.

Event Timeline

sas created this revision.Oct 25 2017, 6:19 PM
clayborg accepted this revision.Oct 26 2017, 9:35 AM

Wow. I didn't realize windows support didn't do any of this. Looks good. Zach will want to ok this as he is one of the main windows contributors.

This revision is now accepted and ready to land.Oct 26 2017, 9:35 AM
davide added a subscriber: davide.Oct 26 2017, 9:38 AM

can you please try adding a test for this one? :)

sas added a comment.Oct 26 2017, 9:58 AM

can you please try adding a test for this one? :)

To be fully honest, I'm not 100% sure how I'm supposed to add tests for this. I looked through the directories containing unit tests and didn't find anything specific to DYLD testing.

I'm going to try to sync with @zturner to see if he is still able to run unit tests on Windows. We exclusively debug Windows remotes from a macOS or Linux host, so I don't have any setup to run local windows tests.

In D39314#908115, @sas wrote:

can you please try adding a test for this one? :)

To be fully honest, I'm not 100% sure how I'm supposed to add tests for this. I looked through the directories containing unit tests and didn't find anything specific to DYLD testing.

I'm going to try to sync with @zturner to see if he is still able to run unit tests on Windows. We exclusively debug Windows remotes from a macOS or Linux host, so I don't have any setup to run local windows tests.

I understand. I'm not picking on you, of course, and I appreciate you trying to do that.
From what I can tell, lack of testing can cause a lot of problems in the future [i.e. I could just remove part of your functions, and all the tests would pass anyway].
This is not ideal, from somebody who's trying to get more involved in lldb with a LLVM background :)
I think every change committed to the codebase should have a test associated, unless it's NFC.
Sorry if this sounds like captain obvious, but in case we can't test something, we might consider slowing down and revisiting the testing infrastructure that's there and improve it.
There's of course a tension between adding features and reducing technical debt, but keep checking fixes for new code without tests associated is a slippery road. Happy to discuss this further (and i know @zturner has ideas on how to fix this)

Testing would include a test that dynamically loads a shared library and sets a breakpoint it in. We have these tests, but I am guessing they are disabled on windows probably because they might use "dlopen(...)" which is probably not available on windows? I know we have attach tests as well. Many tests are disabled on windows, but probably shouldn't be. So I would start looking for those tests. The code will need to be window-ized with #ifdefs and such, but it shouldn't be hard. The shared libraries tests will dynamically open a shared library and then verify we can hit a breakpoint in that shared library (a hint that the DYLD worked). So these tests don't need to be windows specific.

zturner edited edge metadata.Oct 26 2017, 10:55 AM

A test would be something like this:

// dll.cpp
BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, void* reserved) {
  return TRUE;
}

int __declspec(dllexport) DllFunc(int n) {
  int x = n * n;  
  return x;  // set breakpoint here
}
// main.cpp
int __declspec(dllimport) DllFunc(int n);

int main(int argc, char ** argv) {
  int x = DllFunc(4);
  int y = DllFunc(8);   // set breakpoint here
  int z = DllFunc(16);
  return x + y + z;
}

Run main.exe in the debugger, when you're stopped at the breakpoint get the value of x and ensure that it's 4.
Step out and get the value of x and ensure that it's 8.
Step in and ensure the value of n is 8, then run and ensure the value of x is 16.
Delete the breakpoint in the dll, then run and ensure the program terminates with a return value of 56

Ahh, you might also run the exact same test again but where you've loaded this inside of main with LoadLibrary instead of relying on early binding by the loader.