debug.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. """
  18. Run this file to interrupt a running python process and open an interactive shell.
  19. """
  20. import os
  21. import signal
  22. from RemoteDebugUtils import NamedPipe
  23. from RemoteDebugUtils import pipename
  24. def debug_process(pid):
  25. """Interrupt a running process and debug it."""
  26. os.kill(pid, signal.SIGUSR2) # Signal process.
  27. pipe = NamedPipe(pipename(pid), 1)
  28. try:
  29. while pipe.is_open():
  30. txt=raw_input(pipe.get()) + '\n'
  31. pipe.put(txt)
  32. except EOFError:
  33. pass # Exit.
  34. pipe.close()
  35. def main():
  36. with open("/var/run/ambari-agent/ambari-agent.pid") as f:
  37. pid_str = f.read().strip()
  38. pid = int(pid_str)
  39. debug_process(pid)
  40. if __name__=='__main__':
  41. main()