Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.1 KiB

  1. //-----------------------------------------------------------------------------
  2. #include "precomp.h"
  3. #pragma hdrstop
  4. extern "C"
  5. {
  6. #include <stdexts.h>
  7. #include <dbgeng.h>
  8. };
  9. #define DEFAULT_STACK_FRAMES 25
  10. #define OINK_THRESHOLD 4.0
  11. //-----------------------------------------------------------------------------
  12. void StackPig(ULONG ulFrames)
  13. {
  14. IDebugClient* pDebugClient;
  15. IDebugControl* pDebugControl;
  16. if (SUCCEEDED(DebugCreate(__uuidof(IDebugClient), (void**)&pDebugClient)))
  17. {
  18. if (SUCCEEDED(pDebugClient->QueryInterface(__uuidof(IDebugControl), (void**)&pDebugControl)))
  19. {
  20. DEBUG_STACK_FRAME rgdsf[DEFAULT_STACK_FRAMES];
  21. DEBUG_STACK_FRAME* pdsf = NULL;
  22. if ((ulFrames > ARRAYSIZE(rgdsf)))
  23. {
  24. // Try allocating a buffer to accomodate the frames requested.
  25. // Failing that fallback to the default size stack variable.
  26. pdsf = (DEBUG_STACK_FRAME *)LocalAlloc(LPTR, sizeof(DEBUG_STACK_FRAME)*ulFrames);
  27. }
  28. if (pdsf == NULL)
  29. {
  30. pdsf = rgdsf;
  31. if ((ulFrames == 0) || (ulFrames > ARRAYSIZE(rgdsf)))
  32. {
  33. ulFrames = ARRAYSIZE(rgdsf);
  34. }
  35. }
  36. if (SUCCEEDED(pDebugControl->GetStackTrace(0, 0, 0, pdsf, ulFrames, &ulFrames)))
  37. {
  38. double dResult = 0.0;
  39. // print the header
  40. Print("StackSize ");
  41. pDebugControl->OutputStackTrace(
  42. DEBUG_OUTCTL_ALL_CLIENTS,
  43. pdsf,
  44. 0,
  45. DEBUG_STACK_COLUMN_NAMES|DEBUG_STACK_FRAME_ADDRESSES);
  46. for (UINT i = 0; !IsCtrlCHit() && (i < ulFrames); i++)
  47. {
  48. (dResult < 0.1) ?
  49. Print(" ") :
  50. Print("%s%4.1fK ", ((dResult >= OINK_THRESHOLD) ? "OINK" : " "), dResult);
  51. pDebugControl->OutputStackTrace(
  52. DEBUG_OUTCTL_ALL_CLIENTS,
  53. &pdsf[i],
  54. 1,
  55. DEBUG_STACK_FRAME_ADDRESSES);
  56. // process' initial ebp is zero, prevent negative result
  57. if ((i+1 == ulFrames) || (pdsf[i+1].FrameOffset == 0))
  58. {
  59. dResult = 0.0;
  60. continue;
  61. }
  62. dResult = (pdsf[i+1].FrameOffset - pdsf[i].FrameOffset)/1024.0;
  63. }
  64. }
  65. if (pdsf != rgdsf)
  66. {
  67. LocalFree(pdsf);
  68. }
  69. pDebugControl->Release();
  70. }
  71. pDebugClient->Release();
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. extern "C" BOOL Istackpig(DWORD dwOpts, LPVOID pArg)
  76. {
  77. StackPig(PtrToUlong(pArg));
  78. return TRUE;
  79. }