Retrieves the execution context (i.e. the registers values) for this thread. @type ContextFlags: int @param ContextFlags: Optional, specify which registers to retrieve. Defaults to C{win32.CONTEXT_ALL} which retrieves all registes for the cu
(self, ContextFlags=None, bSuspend=False)
| 464 | # TODO |
| 465 | # A registers cache could be implemented here. |
| 466 | def get_context(self, ContextFlags=None, bSuspend=False): |
| 467 | """ |
| 468 | Retrieves the execution context (i.e. the registers values) for this |
| 469 | thread. |
| 470 | |
| 471 | @type ContextFlags: int |
| 472 | @param ContextFlags: Optional, specify which registers to retrieve. |
| 473 | Defaults to C{win32.CONTEXT_ALL} which retrieves all registes |
| 474 | for the current platform. |
| 475 | |
| 476 | @type bSuspend: bool |
| 477 | @param bSuspend: C{True} to automatically suspend the thread before |
| 478 | getting its context, C{False} otherwise. |
| 479 | |
| 480 | Defaults to C{False} because suspending the thread during some |
| 481 | debug events (like thread creation or destruction) may lead to |
| 482 | strange errors. |
| 483 | |
| 484 | Note that WinAppDbg 1.4 used to suspend the thread automatically |
| 485 | always. This behavior was changed in version 1.5. |
| 486 | |
| 487 | @rtype: dict( str S{->} int ) |
| 488 | @return: Dictionary mapping register names to their values. |
| 489 | |
| 490 | @see: L{set_context} |
| 491 | """ |
| 492 | |
| 493 | # Some words on the "strange errors" that lead to the bSuspend |
| 494 | # parameter. Peter Van Eeckhoutte and I were working on a fix |
| 495 | # for some bugs he found in the 1.5 betas when we stumbled upon |
| 496 | # what seemed to be a deadlock in the debug API that caused the |
| 497 | # GetThreadContext() call never to return. Since removing the |
| 498 | # call to SuspendThread() solved the problem, and a few Google |
| 499 | # searches showed a handful of problems related to these two |
| 500 | # APIs and Wow64 environments, I decided to break compatibility. |
| 501 | # |
| 502 | # Here are some pages about the weird behavior of SuspendThread: |
| 503 | # http://zachsaw.blogspot.com.es/2010/11/wow64-bug-getthreadcontext-may-return.html |
| 504 | # http://stackoverflow.com/questions/3444190/windows-suspendthread-doesnt-getthreadcontext-fails |
| 505 | |
| 506 | # Get the thread handle. |
| 507 | dwAccess = win32.THREAD_GET_CONTEXT |
| 508 | if bSuspend: |
| 509 | dwAccess = dwAccess | win32.THREAD_SUSPEND_RESUME |
| 510 | hThread = self.get_handle(dwAccess) |
| 511 | |
| 512 | # Suspend the thread if requested. |
| 513 | if bSuspend: |
| 514 | try: |
| 515 | self.suspend() |
| 516 | except WindowsError: |
| 517 | # Threads can't be suspended when the exit process event |
| 518 | # arrives, but you can still get the context. |
| 519 | bSuspend = False |
| 520 | |
| 521 | # If an exception is raised, make sure the thread execution is resumed. |
| 522 | try: |
| 523 | if win32.bits == self.get_bits(): |
no test coverage detected