Leaked source code of windows server 2003
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.

1160 lines
36 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1997 Microsoft Corporation. All Rights Reserved.
  5. Component: misc
  6. File: util.cpp
  7. Owner: DGottner
  8. This file contains debugger utility functions
  9. ===================================================================*/
  10. #include "denpre.h"
  11. #pragma hdrstop
  12. #include "vector.h"
  13. #include "debugger.h"
  14. #include "iiscnfg.h"
  15. #include "mdcommsg.h" // for RETURNCODETOHRESULT macro
  16. #include "memchk.h"
  17. #include "vecimpl.h"
  18. /* Win64: This struct is used to package data passed to the thread handler.
  19. * (3 DWORDs are too small in 64 bit world.)
  20. */
  21. struct DebugThreadCallArgs
  22. {
  23. DWORD dwMethod;
  24. IDebugApplication * pDebugAppln;
  25. void * pvArg;
  26. DebugThreadCallArgs(DWORD dwMethod = 0, IDebugApplication *pDebugAppln = 0, void *pvArg = 0)
  27. {
  28. this->dwMethod = dwMethod;
  29. this->pDebugAppln = pDebugAppln;
  30. this->pvArg = pvArg;
  31. }
  32. };
  33. // Published globals
  34. IProcessDebugManager * g_pPDM = NULL; // instance of debugger for this process.
  35. IDebugApplication * g_pDebugApp = NULL; // Root ASP application
  36. IDebugApplicationNode * g_pDebugAppRoot = NULL; // used to create hierarchy tree
  37. CViperActivity * g_pDebugActivity = NULL; // Debugger's activity
  38. DWORD g_dwDebugThreadId = 0; // Thread ID of viper activity
  39. // Globals for debugging
  40. static DWORD g_dwDenaliAppCookie; // Cookie to use to remove app
  41. static HANDLE g_hPDMTermEvent; // PDM terminate event
  42. static vector<DebugThreadCallArgs> *g_prgThreadCallArgs; // for new 64 bit interface.
  43. // This hash structure & CS is used by GetServerDebugRoot()
  44. struct CDebugNodeElem : CLinkElem
  45. {
  46. IDebugApplicationNode *m_pServerRoot;
  47. HRESULT Init(char *szKey, int cchKey)
  48. {
  49. char *szKeyAlloc = new char [cchKey + 1];
  50. if (!szKeyAlloc) return E_OUTOFMEMORY;
  51. return CLinkElem::Init(memcpy(szKeyAlloc, szKey, cchKey + 1), cchKey);
  52. }
  53. ~CDebugNodeElem()
  54. {
  55. if (m_pKey)
  56. delete m_pKey;
  57. }
  58. };
  59. static CHashTable g_HashMDPath2DebugRoot;
  60. static CRITICAL_SECTION g_csDebugLock; // Lock for g_hashMDPath2DebugRoot
  61. /*===================================================================
  62. InvokeDebuggerWithThreadSwitch
  63. Invoke Debugger (or Debugger UI) method from a correct thread
  64. using IDebugThreadCall.
  65. Parameters
  66. IDebugApplication *pDebugAppln to get to debugger UI
  67. DWORD iMethod which method to call
  68. void *Arg call argument
  69. Returns
  70. HRESULT
  71. ===================================================================*/
  72. // GUIDs for debugger events
  73. static const GUID DEBUGNOTIFY_ONPAGEBEGIN =
  74. { 0xfd6806c0, 0xdb89, 0x11d0, { 0x8f, 0x81, 0x0, 0x80, 0xc7, 0x3d, 0x6d, 0x96 } };
  75. static const GUID DEBUGNOTIFY_ONPAGEEND =
  76. { 0xfd6806c1, 0xdb89, 0x11d0, { 0x8f, 0x81, 0x0, 0x80, 0xc7, 0x3d, 0x6d, 0x96 } };
  77. static const GUID DEBUGNOTIFY_ON_REFRESH_BREAKPOINT =
  78. { 0xffcf4b38, 0xfa12, 0x11d0, { 0x8f, 0x3b, 0x0, 0xc0, 0x4f, 0xc3, 0x4d, 0xcc } };
  79. // Local class that implements IDebugCallback
  80. class CDebugThreadDebuggerCall : public IDebugThreadCall
  81. {
  82. public:
  83. STDMETHODIMP QueryInterface(const GUID &, void **);
  84. STDMETHODIMP_(ULONG) AddRef();
  85. STDMETHODIMP_(ULONG) Release();
  86. STDMETHODIMP ThreadCallHandler(DWORD_PTR, DWORD_PTR, DWORD_PTR);
  87. };
  88. HRESULT CDebugThreadDebuggerCall::QueryInterface
  89. (
  90. const GUID &iid,
  91. void **ppv
  92. )
  93. {
  94. if (iid == IID_IUnknown || iid == IID_IDebugThreadCall)
  95. {
  96. *ppv = this;
  97. return S_OK;
  98. }
  99. else
  100. {
  101. *ppv = NULL;
  102. return E_NOINTERFACE;
  103. }
  104. }
  105. ULONG CDebugThreadDebuggerCall::AddRef()
  106. {
  107. return 1;
  108. }
  109. ULONG CDebugThreadDebuggerCall::Release()
  110. {
  111. return 1;
  112. }
  113. HRESULT CDebugThreadDebuggerCall::ThreadCallHandler
  114. (
  115. DWORD_PTR iArg,
  116. DWORD_PTR ,
  117. DWORD_PTR
  118. )
  119. {
  120. // Get arguments
  121. DebugThreadCallArgs *pThreadCallArgs = &(*g_prgThreadCallArgs)[(int)iArg];
  122. IDebugApplication * pDebugAppln = pThreadCallArgs->pDebugAppln;
  123. DWORD dwMethod = pThreadCallArgs->dwMethod;
  124. void * pvArg = pThreadCallArgs->pvArg;
  125. // we won't reference the argument block again, so free it up now.
  126. pThreadCallArgs->dwMethod |= DEBUGGER_UNUSED_RECORD;
  127. BOOL fForceDebugger = (dwMethod & (DEBUGGER_UI_BRING_DOCUMENT_TO_TOP|DEBUGGER_UI_BRING_DOC_CONTEXT_TO_TOP)) != 0;
  128. BOOL fNeedDebuggerUI = (dwMethod & (DEBUGGER_UI_BRING_DOCUMENT_TO_TOP|DEBUGGER_UI_BRING_DOC_CONTEXT_TO_TOP)) != 0;
  129. BOOL fNeedNodeEvents = (dwMethod & DEBUGGER_ON_REMOVE_CHILD) != 0;
  130. BOOL fNeedDebugger = (dwMethod & ~DEBUGGER_ON_DESTROY) != 0;
  131. HRESULT hr = S_OK;
  132. IApplicationDebugger *pDebugger = NULL;
  133. IApplicationDebuggerUI *pDebuggerUI = NULL;
  134. IDebugApplicationNodeEvents *pNodeEvents = NULL;
  135. if (pDebugAppln == NULL)
  136. return E_POINTER;
  137. // Get the debugger
  138. if (fNeedDebugger)
  139. {
  140. hr = pDebugAppln->GetDebugger(&pDebugger);
  141. if (FAILED(hr))
  142. {
  143. // Debugger is not currently debugging our application.
  144. if (!fForceDebugger)
  145. return E_FAIL; // no debugger
  146. // Start the debugger and try again.
  147. hr = pDebugAppln->StartDebugSession();
  148. if (SUCCEEDED(hr))
  149. hr = pDebugAppln->GetDebugger(&pDebugger);
  150. }
  151. // Debugger UI is needed only for some methods
  152. if (SUCCEEDED(hr) && fNeedDebuggerUI)
  153. {
  154. hr = pDebugger->QueryInterface
  155. (
  156. IID_IApplicationDebuggerUI,
  157. reinterpret_cast<void **>(&pDebuggerUI)
  158. );
  159. }
  160. // Debugger UI is needed only for some methods
  161. if (SUCCEEDED(hr) && fNeedNodeEvents)
  162. {
  163. hr = pDebugger->QueryInterface
  164. (
  165. IID_IDebugApplicationNodeEvents,
  166. reinterpret_cast<void **>(&pNodeEvents)
  167. );
  168. }
  169. }
  170. // Call the desired method
  171. if (SUCCEEDED(hr))
  172. {
  173. switch (dwMethod)
  174. {
  175. case DEBUGGER_EVENT_ON_PAGEBEGIN:
  176. {
  177. hr = pDebugger->onDebuggerEvent
  178. (
  179. DEBUGNOTIFY_ONPAGEBEGIN,
  180. static_cast<IUnknown *>(pvArg)
  181. );
  182. break;
  183. }
  184. case DEBUGGER_EVENT_ON_PAGEEND:
  185. {
  186. hr = pDebugger->onDebuggerEvent
  187. (
  188. DEBUGNOTIFY_ONPAGEEND,
  189. static_cast<IUnknown *>(pvArg)
  190. );
  191. break;
  192. }
  193. case DEBUGGER_EVENT_ON_REFRESH_BREAKPOINT:
  194. {
  195. hr = pDebugger->onDebuggerEvent
  196. (
  197. DEBUGNOTIFY_ON_REFRESH_BREAKPOINT,
  198. static_cast<IUnknown *>(pvArg)
  199. );
  200. break;
  201. }
  202. case DEBUGGER_ON_REMOVE_CHILD:
  203. {
  204. hr = pNodeEvents->onRemoveChild
  205. (
  206. static_cast<IDebugApplicationNode *>(pvArg)
  207. );
  208. break;
  209. }
  210. case DEBUGGER_ON_DESTROY:
  211. {
  212. hr = static_cast<IDebugDocumentTextEvents *>(pvArg)->onDestroy();
  213. break;
  214. }
  215. case DEBUGGER_UI_BRING_DOCUMENT_TO_TOP:
  216. {
  217. hr = pDebuggerUI->BringDocumentToTop
  218. (
  219. static_cast<IDebugDocumentText *>(pvArg)
  220. );
  221. break;
  222. }
  223. case DEBUGGER_UI_BRING_DOC_CONTEXT_TO_TOP:
  224. {
  225. hr = pDebuggerUI->BringDocumentContextToTop
  226. (
  227. static_cast<IDebugDocumentContext *>(pvArg)
  228. );
  229. break;
  230. }
  231. default:
  232. hr = E_FAIL;
  233. break;
  234. }
  235. }
  236. // Cleanup
  237. if (pDebuggerUI) pDebuggerUI->Release();
  238. if (pNodeEvents) pNodeEvents->Release();
  239. if (pDebugger) pDebugger->Release();
  240. return hr;
  241. }
  242. // The function calls using IDebugThreadCall
  243. HRESULT InvokeDebuggerWithThreadSwitch
  244. (
  245. IDebugApplication *pDebugAppln,
  246. DWORD dwMethod,
  247. void *pvArg
  248. )
  249. {
  250. // take these arguments and package them up in the array. We will pass the
  251. // index to the callback handler.
  252. //
  253. // first look for a freed up element before creating a new one.
  254. for (int i = g_prgThreadCallArgs->length() - 1; i >= 0; --i)
  255. {
  256. DebugThreadCallArgs *pThreadCallArgs = &(*g_prgThreadCallArgs)[i];
  257. if (pThreadCallArgs->dwMethod & DEBUGGER_UNUSED_RECORD)
  258. {
  259. pThreadCallArgs->dwMethod = dwMethod;
  260. pThreadCallArgs->pDebugAppln = pDebugAppln;
  261. pThreadCallArgs->pvArg = pvArg;
  262. break;
  263. }
  264. }
  265. if (i < 0)
  266. {
  267. HRESULT hr = g_prgThreadCallArgs->append(DebugThreadCallArgs(dwMethod, pDebugAppln, pvArg));
  268. if (FAILED(hr))
  269. return hr;
  270. i = g_prgThreadCallArgs->length() - 1;
  271. }
  272. CDebugThreadDebuggerCall Call;
  273. return pDebugAppln->SynchronousCallInDebuggerThread
  274. (
  275. &Call, i, 0, 0
  276. );
  277. }
  278. /*===================================================================
  279. FCaesars
  280. Query registry to determine if default debugger is Caesar's
  281. (Script Debugger)
  282. ===================================================================*/
  283. BOOL FCaesars()
  284. {
  285. static BOOL fCaesars = 0xBADF00D;
  286. HKEY hKey = NULL;
  287. char szRegPath[_MAX_PATH];
  288. DWORD dwSize = sizeof szRegPath;
  289. // Check to see if Ceasers is registered as the JIT debugger on this machine.
  290. if (fCaesars == 0xBADF00D)
  291. {
  292. fCaesars = FALSE;
  293. if (RegOpenKey(HKEY_CLASSES_ROOT, _T("CLSID\\{834128A2-51F4-11D0-8F20-00805F2CD064}\\LocalServer32"), &hKey) == ERROR_SUCCESS)
  294. {
  295. if (RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE) szRegPath, &dwSize) == ERROR_SUCCESS)
  296. {
  297. char szFile[_MAX_FNAME];
  298. _splitpath(szRegPath, NULL, NULL, szFile, NULL);
  299. if (_stricmp(szFile, "msscrdbg") == 0)
  300. fCaesars = TRUE;
  301. }
  302. CloseHandle (hKey);
  303. }
  304. }
  305. return fCaesars;
  306. }
  307. /*===================================================================
  308. DestroyDocumentTree
  309. Recursively release all the nodes in a document tree.
  310. Parameters
  311. IDebugApplication *pDocRoot root of hierarchy to destroy
  312. ===================================================================*/
  313. void
  314. DestroyDocumentTree(IDebugApplicationNode *pDocRoot)
  315. {
  316. IEnumDebugApplicationNodes *pEnum;
  317. if (SUCCEEDED(pDocRoot->EnumChildren(&pEnum)) && pEnum != NULL)
  318. {
  319. IDebugApplicationNode *pDocNode;
  320. while (pEnum->Next(1, &pDocNode, NULL) == S_OK)
  321. DestroyDocumentTree(pDocNode);
  322. pEnum->Release();
  323. }
  324. // See if this is a directory node
  325. //
  326. IFileNode *pFileNode;
  327. if (SUCCEEDED(pDocRoot->QueryInterface(IID_IFileNode, reinterpret_cast<void **>(&pFileNode))))
  328. {
  329. // This is a directory node, only detach when its document count vanishes)
  330. if (pFileNode->DecrementDocumentCount() == 0)
  331. {
  332. pDocRoot->Detach();
  333. pDocRoot->Close();
  334. pDocRoot->Release();
  335. }
  336. pFileNode->Release();
  337. }
  338. else
  339. {
  340. // This node is a CTemplate (or one of its include files)
  341. pDocRoot->Detach();
  342. pDocRoot->Close();
  343. pDocRoot->Release();
  344. }
  345. }
  346. /*===================================================================
  347. CreateDocumentTree
  348. Takes a path to be rooted at a node "pDocRoot", parses the path,
  349. and creates a node for each component of the path. Returns the
  350. leaf (Since the root is known) as its value.
  351. This function is called from contexts where part of the document
  352. tree may already exist, so EnumChildren is called and nodes are only
  353. created when a child does not exist. When a node exists, we merely
  354. descend into the tree.
  355. NOTE:
  356. The intermediate nodes are created with a CFileNode document
  357. implementation. The leaf node is not given a document provider
  358. - the caller must provide one.
  359. Parameters
  360. wchar_t * szDocPath path of the document
  361. IDebugApplication *pDocParent parent to attach the application tree to
  362. IDebugApplication **ppDocRoot returns root of document hierarchy
  363. IDebugApplication **ppDocLeaf returns document leaf node.
  364. wchar_t ** pwszLeaf name of the leaf node
  365. Returns
  366. HRESULT
  367. ===================================================================*/
  368. HRESULT CreateDocumentTree
  369. (
  370. wchar_t *wszDocPath,
  371. IDebugApplicationNode *pDocParent,
  372. IDebugApplicationNode **ppDocRoot,
  373. IDebugApplicationNode **ppDocLeaf,
  374. wchar_t **pwszLeaf
  375. )
  376. {
  377. HRESULT hr;
  378. BOOL fCreateOnly = FALSE; // Set to TRUE when there is no need to check for duplicate node
  379. *ppDocRoot = *ppDocLeaf = NULL;
  380. // Ignore initial delimiters
  381. while (wszDocPath[0] == '/')
  382. ++wszDocPath;
  383. // Now loop over every component in the path, adding a node for each
  384. while (wszDocPath != NULL)
  385. {
  386. // Get next path component
  387. *pwszLeaf = wszDocPath;
  388. wszDocPath = wcschr(wszDocPath, L'/');
  389. if (wszDocPath)
  390. *wszDocPath++ = L'\0';
  391. // Check to see if this component is already a child or not
  392. BOOL fNodeExists = FALSE;
  393. if (!fCreateOnly)
  394. {
  395. IEnumDebugApplicationNodes *pEnum;
  396. if (SUCCEEDED(pDocParent->EnumChildren(&pEnum)) && pEnum != NULL)
  397. {
  398. IDebugApplicationNode *pDocChild;
  399. while (!fNodeExists && pEnum->Next(1, &pDocChild, NULL) == S_OK)
  400. {
  401. BSTR bstrName = NULL;
  402. if (FAILED(hr = pDocChild->GetName(DOCUMENTNAMETYPE_APPNODE, &bstrName)))
  403. return hr;
  404. if (wcscmp(bstrName, *pwszLeaf) == 0)
  405. {
  406. // The name of this node is equal to the component. Instead of
  407. // creating a new node, descend into the tree.
  408. //
  409. fNodeExists = TRUE;
  410. *ppDocLeaf = pDocChild;
  411. // If '*ppDocRoot' hasn't been assigned to yet, this means that
  412. // this is the first node found (and hence the root of the tree)
  413. //
  414. if (*ppDocRoot == NULL)
  415. {
  416. *ppDocRoot = pDocChild;
  417. (*ppDocRoot)->AddRef();
  418. }
  419. // If this node is a CFileNode structure (we don't require it to be)
  420. // then increment its (recursive) containing document count.
  421. //
  422. IFileNode *pFileNode;
  423. if (SUCCEEDED(pDocChild->QueryInterface(IID_IFileNode, reinterpret_cast<void **>(&pFileNode))))
  424. {
  425. pFileNode->IncrementDocumentCount();
  426. pFileNode->Release();
  427. }
  428. }
  429. SysFreeString(bstrName);
  430. pDocChild->Release();
  431. }
  432. pEnum->Release();
  433. }
  434. }
  435. // Create a new node if the node was not found above. Also, at this point,
  436. // to save time, we always set "fCreateOnly" to TRUE because if we are
  437. // forced to create a node at this level, we will need to create nodes at
  438. // all other levels further down
  439. //
  440. if (!fNodeExists)
  441. {
  442. fCreateOnly = TRUE;
  443. // Create the node
  444. if (FAILED(hr = g_pDebugApp->CreateApplicationNode(ppDocLeaf)))
  445. return hr;
  446. // Create a doc provider for the node - for intermediate nodes only
  447. if (wszDocPath != NULL) // intermediate node
  448. {
  449. CFileNode *pFileNode = new CFileNode;
  450. if (pFileNode == NULL ||
  451. FAILED(hr = pFileNode->Init(*pwszLeaf)) ||
  452. FAILED(hr = (*ppDocLeaf)->SetDocumentProvider(pFileNode)))
  453. {
  454. (*ppDocLeaf)->Release();
  455. return E_OUTOFMEMORY;
  456. }
  457. // New node, only one document (count started at 0, so this will set to 1)
  458. pFileNode->IncrementDocumentCount();
  459. // SetDocumentProvider() AddRef'ed
  460. pFileNode->Release();
  461. }
  462. // If '*ppDocRoot' hasn't been assigned to yet, this means that
  463. // this is the first node created (and hence the root of the tree)
  464. //
  465. if (*ppDocRoot == NULL)
  466. {
  467. *ppDocRoot = *ppDocLeaf;
  468. (*ppDocRoot)->AddRef();
  469. }
  470. // Attach the node
  471. if (FAILED(hr = (*ppDocLeaf)->Attach(pDocParent)))
  472. return hr;
  473. }
  474. // Descend
  475. pDocParent = *ppDocLeaf;
  476. }
  477. if (*ppDocLeaf)
  478. (*ppDocLeaf)->AddRef();
  479. return S_OK;
  480. }
  481. /*===================================================================
  482. Debugger
  483. The purpose of this thread is to create an execution environment for
  484. the Process Debug Manager (PDM). There is only one PDM per process,
  485. and this does not really fit in other threads, so we dedicate a thread to this.
  486. Parameters:
  487. LPVOID params
  488. Points to a BOOL* which will be set to 1 when
  489. this thread is completely initialized.
  490. Returns:
  491. 0
  492. ===================================================================*/
  493. void __cdecl Debugger(void *pvInit)
  494. {
  495. HRESULT hr;
  496. if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
  497. {
  498. // Bug 87857: if we get E_INVALIDARG, we need to do a CoUninitialize
  499. if (hr == E_INVALIDARG)
  500. CoUninitialize();
  501. *static_cast<BOOL *>(pvInit) = TRUE;
  502. return;
  503. }
  504. if (FAILED(CoCreateInstance(
  505. CLSID_ProcessDebugManager,
  506. NULL,
  507. CLSCTX_INPROC_SERVER,
  508. IID_IProcessDebugManager,
  509. reinterpret_cast<void **>(&g_pPDM))))
  510. {
  511. *static_cast<BOOL *>(pvInit) = TRUE;
  512. CoUninitialize();
  513. return;
  514. }
  515. *static_cast<BOOL *>(pvInit) = TRUE;
  516. while (TRUE)
  517. {
  518. DWORD dwRet = MsgWaitForMultipleObjects(1,
  519. &g_hPDMTermEvent,
  520. FALSE,
  521. INFINITE,
  522. QS_ALLINPUT);
  523. if (dwRet == WAIT_OBJECT_0)
  524. break;
  525. MSG msg;
  526. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  527. DispatchMessage(&msg);
  528. }
  529. g_pPDM->Release();
  530. CoUninitialize();
  531. g_pPDM = NULL; // indication that the thread is gone
  532. }
  533. /*===================================================================
  534. HRESULT StartPDM()
  535. kick off the PDM thread
  536. ===================================================================*/
  537. HRESULT StartPDM()
  538. {
  539. BOOL fStarted = FALSE;
  540. g_hPDMTermEvent = IIS_CREATE_EVENT(
  541. "g_hPDMTermEvent",
  542. &g_hPDMTermEvent,
  543. TRUE,
  544. FALSE
  545. );
  546. if( g_hPDMTermEvent == NULL )
  547. return E_FAIL;
  548. _beginthread(Debugger, 0, &fStarted);
  549. while (!fStarted)
  550. Sleep(100);
  551. if (g_pPDM == NULL) // could not create the PDM for some reason
  552. {
  553. CloseHandle(g_hPDMTermEvent);
  554. g_hPDMTermEvent = NULL;
  555. return E_FAIL;
  556. }
  557. return S_OK;
  558. }
  559. /*===================================================================
  560. HRESULT InitDebugging
  561. Initialize everything we need for debugging
  562. ===================================================================*/
  563. HRESULT InitDebugging
  564. (
  565. CIsapiReqInfo *pIReq
  566. )
  567. {
  568. HRESULT hr;
  569. // this stack size should cover the static string directly
  570. // below and the process pid. If the app name is found, the
  571. // buffer is resized.
  572. STACK_BUFFER(tempWszDebugAppName, 128);
  573. // Start the PDM
  574. if (FAILED(hr = StartPDM()))
  575. return hr;
  576. Assert (g_pPDM); // StartPDM succeeds ==> g_pPDM <> NULL
  577. ErrInitCriticalSection(&g_csDebugLock, hr);
  578. if (FAILED(hr))
  579. return hr;
  580. // Create the debug application & give it a name
  581. if (FAILED(hr = g_pPDM->CreateApplication(&g_pDebugApp)))
  582. goto LErrorCleanup;
  583. wchar_t *wszDebugAppName = (wchar_t *)tempWszDebugAppName.QueryPtr();
  584. wcscpy(wszDebugAppName, L"Microsoft Active Server Pages"); // DO NOT LOCALIZE THIS STRING
  585. if (g_fOOP) {
  586. // Bug 154300: If a friendly app. name exists, use it along with the PID for
  587. // WAM identification.
  588. //
  589. // Declare some temporaries
  590. //
  591. DWORD dwApplMDPathLen;
  592. DWORD dwRequiredBuffer = 0;
  593. STACK_BUFFER( tempMDData, 2048 );
  594. BYTE *prgbData = (BYTE *)tempMDData.QueryPtr();
  595. TCHAR *szApplMDPath = pIReq->QueryPszApplnMDPath();
  596. //
  597. // if the webserver returned NULL for ApplnMDPath which is not expected we return an error.
  598. //
  599. if (!szApplMDPath)
  600. {
  601. hr = E_FAIL;
  602. goto LErrorCleanup;
  603. }
  604. // get friendly name from metabase
  605. hr = pIReq->GetAspMDData(
  606. szApplMDPath,
  607. MD_APP_FRIENDLY_NAME,
  608. METADATA_INHERIT,
  609. ASP_MD_UT_APP,
  610. STRING_METADATA,
  611. tempMDData.QuerySize(),
  612. 0,
  613. prgbData,
  614. &dwRequiredBuffer);
  615. if (hr == RETURNCODETOHRESULT(ERROR_INSUFFICIENT_BUFFER))
  616. {
  617. if (tempMDData.Resize(dwRequiredBuffer) == FALSE) {
  618. hr = E_OUTOFMEMORY;
  619. }
  620. else {
  621. prgbData = (BYTE *)tempMDData.QueryPtr();
  622. hr = pIReq->GetAspMDData(
  623. szApplMDPath,
  624. MD_APP_FRIENDLY_NAME,
  625. METADATA_INHERIT,
  626. ASP_MD_UT_APP,
  627. STRING_METADATA,
  628. dwRequiredBuffer,
  629. 0,
  630. prgbData,
  631. &dwRequiredBuffer);
  632. }
  633. }
  634. // For OOP append process id
  635. if (SUCCEEDED(hr) && *reinterpret_cast<wchar_t *>(prgbData) != 0) {
  636. wchar_t *pwszAppName = reinterpret_cast<wchar_t *>(prgbData);
  637. // first thing we need to do is resize the buffer...
  638. if (tempWszDebugAppName.Resize((wcslen(wszDebugAppName) * 2) // string already in buffer
  639. + (wcslen(pwszAppName) * 2) // length of app name
  640. + 20 // max size of a process ID
  641. + 10 // various format chars from below
  642. + 2) == FALSE) { // NULL termination
  643. hr = E_OUTOFMEMORY;
  644. }
  645. else {
  646. wszDebugAppName = (wchar_t *)tempWszDebugAppName.QueryPtr();
  647. _snwprintf(&wszDebugAppName[wcslen(wszDebugAppName)],
  648. (tempWszDebugAppName.QuerySize()/2) - wcslen(wszDebugAppName),
  649. L" (%s, %d)",
  650. reinterpret_cast<wchar_t *>(prgbData),
  651. GetCurrentProcessId());
  652. }
  653. }
  654. else {
  655. _snwprintf(&wszDebugAppName[wcslen(wszDebugAppName)],
  656. (tempWszDebugAppName.QuerySize()/2) - wcslen(wszDebugAppName),
  657. L" (%d)",
  658. GetCurrentProcessId());
  659. }
  660. hr = S_OK;
  661. }
  662. if (FAILED(hr = g_pDebugApp->SetName(wszDebugAppName)))
  663. goto LErrorCleanup;
  664. if (FAILED(hr = g_pPDM->AddApplication(g_pDebugApp, &g_dwDenaliAppCookie)))
  665. goto LErrorCleanup;
  666. if (FAILED(hr = g_pDebugApp->GetRootNode(&g_pDebugAppRoot)))
  667. goto LErrorCleanup;
  668. // Init the hash table used for Keeping track of virtual server roots
  669. if (FAILED(hr = g_HashMDPath2DebugRoot.Init()))
  670. goto LErrorCleanup;
  671. // Create the array for passing data to debug thread
  672. if ((g_prgThreadCallArgs = new vector<DebugThreadCallArgs>) == NULL) {
  673. hr = E_OUTOFMEMORY;
  674. goto LErrorCleanup;
  675. }
  676. return S_OK;
  677. LErrorCleanup:
  678. // Clean up some globals (some thing may be NULL and some not)
  679. if (g_pDebugAppRoot) {
  680. g_pDebugAppRoot->Release();
  681. g_pDebugAppRoot = NULL;
  682. }
  683. if (g_pDebugApp) {
  684. g_pDebugApp->Release();
  685. g_pDebugApp = NULL;
  686. }
  687. // Kill PDM thread if we started it up.
  688. if (g_pPDM) {
  689. SetEvent(g_hPDMTermEvent);
  690. while (g_pPDM)
  691. Sleep(100);
  692. CloseHandle(g_hPDMTermEvent);
  693. g_pPDM = NULL;
  694. DeleteCriticalSection(&g_csDebugLock);
  695. }
  696. return hr;
  697. }
  698. /*===================================================================
  699. UnInitDebugging
  700. Uninitialize debugging
  701. NOTE: WE DO NOT RELEASE THE VIPER DEBUG ACTIVITY.
  702. (EVEN THOUGH INIT CREATES IT)
  703. THIS IS BECAUSE UNINIT MUST BE INVOKED WHILE SCRIPTS ON THE
  704. ACTIVITY ARE STILL RUNNING!
  705. ===================================================================*/
  706. HRESULT UnInitDebugging()
  707. {
  708. // Clear and UnInit the hash tables (containing the application nodes)
  709. CDebugNodeElem *pNukeDebugNode = static_cast<CDebugNodeElem *>(g_HashMDPath2DebugRoot.Head());
  710. while (pNukeDebugNode != NULL)
  711. {
  712. CDebugNodeElem *pNext = static_cast<CDebugNodeElem *>(pNukeDebugNode->m_pNext);
  713. pNukeDebugNode->m_pServerRoot->Detach();
  714. pNukeDebugNode->m_pServerRoot->Close();
  715. pNukeDebugNode->m_pServerRoot->Release();
  716. delete pNukeDebugNode;
  717. pNukeDebugNode = pNext;
  718. }
  719. g_HashMDPath2DebugRoot.UnInit();
  720. DeleteCriticalSection(&g_csDebugLock);
  721. // Unlink the top node
  722. if (g_pDebugAppRoot)
  723. {
  724. g_pDebugAppRoot->Detach();
  725. g_pDebugAppRoot->Close();
  726. g_pDebugAppRoot->Release();
  727. }
  728. // Delete the application
  729. if (g_pDebugApp)
  730. {
  731. Assert (g_pPDM != NULL);
  732. // EXPLICITLY ignore failure result here:
  733. // if Init() failed earlier, then RemoveApplication will fail here.
  734. g_pPDM->RemoveApplication(g_dwDenaliAppCookie);
  735. g_pDebugApp->Close();
  736. g_pDebugApp->Release();
  737. g_pDebugApp = NULL;
  738. }
  739. // Tell the PDM to suicide
  740. if (g_pPDM)
  741. {
  742. SetEvent(g_hPDMTermEvent);
  743. while (g_pPDM)
  744. Sleep(100);
  745. CloseHandle(g_hPDMTermEvent);
  746. }
  747. // delete the argument buffer
  748. delete g_prgThreadCallArgs;
  749. return S_OK;
  750. }
  751. /*===================================================================
  752. GetServerDebugRoot
  753. Each virtual server has its own root in the application tree.
  754. (i.e. the tree looks like
  755. Microsoft ASP
  756. <Virtual Server 1 Name>
  757. <Denali Application Name>
  758. <Files>
  759. <Virtual Server 2 Name>
  760. <Denali Application>
  761. ...
  762. Since there may be multiple applications per each server, the
  763. server nodes are managed at one central location (here) so that
  764. new applications get added to the correct nodes.
  765. ===================================================================*/
  766. HRESULT GetServerDebugRoot
  767. (
  768. CIsapiReqInfo *pIReq,
  769. IDebugApplicationNode **ppDebugRoot
  770. )
  771. {
  772. HRESULT hr = E_FAIL;
  773. STACK_BUFFER( tempMDData, 2048 );
  774. *ppDebugRoot = NULL;
  775. // Get the metabase path for this virtual server from the CIsapiReqInfo
  776. DWORD dwInstanceMDPathLen;
  777. char *szInstanceMDPath;
  778. STACK_BUFFER( instPathBuf, 128 );
  779. if (!SERVER_GET(pIReq, "INSTANCE_META_PATH", &instPathBuf, &dwInstanceMDPathLen))
  780. return HRESULT_FROM_WIN32(GetLastError());
  781. szInstanceMDPath = (char *)instPathBuf.QueryPtr();
  782. // See if we already have a node for this path - If not then create it and add to hash table
  783. EnterCriticalSection(&g_csDebugLock);
  784. CDebugNodeElem *pDebugNode = static_cast<CDebugNodeElem *>(g_HashMDPath2DebugRoot.FindElem(szInstanceMDPath, dwInstanceMDPathLen - 1));
  785. BOOL fDeleteDebugNode = FALSE;
  786. if (!pDebugNode)
  787. {
  788. // Node does not exist, so create a new application node.
  789. pDebugNode = new CDebugNodeElem;
  790. if (pDebugNode == NULL)
  791. {
  792. hr = E_OUTOFMEMORY;
  793. goto LExit;
  794. }
  795. fDeleteDebugNode = TRUE;
  796. if (FAILED(hr = pDebugNode->Init(szInstanceMDPath, dwInstanceMDPathLen - 1)))
  797. {
  798. goto LExit;
  799. }
  800. // Look up server name in metabase.
  801. BYTE *prgbData = (BYTE *)tempMDData.QueryPtr();
  802. DWORD dwRequiredBuffer = 0;
  803. hr = pIReq->GetAspMDDataA(
  804. szInstanceMDPath,
  805. MD_SERVER_COMMENT,
  806. METADATA_INHERIT,
  807. IIS_MD_UT_SERVER,
  808. STRING_METADATA,
  809. tempMDData.QuerySize(),
  810. 0,
  811. prgbData,
  812. &dwRequiredBuffer);
  813. if (hr == RETURNCODETOHRESULT(ERROR_INSUFFICIENT_BUFFER)) {
  814. if (tempMDData.Resize(dwRequiredBuffer) == FALSE) {
  815. hr = E_OUTOFMEMORY;
  816. }
  817. else {
  818. prgbData = reinterpret_cast<BYTE *>(tempMDData.QueryPtr());
  819. hr = pIReq->GetAspMDDataA(
  820. szInstanceMDPath,
  821. MD_SERVER_COMMENT,
  822. METADATA_INHERIT,
  823. IIS_MD_UT_SERVER,
  824. STRING_METADATA,
  825. dwRequiredBuffer,
  826. 0,
  827. prgbData,
  828. &dwRequiredBuffer);
  829. }
  830. }
  831. if (FAILED(hr))
  832. {
  833. // ServerComment does not exist, so construct using server name and port
  834. STACK_BUFFER( serverNameBuff, 16 );
  835. DWORD cbServerName;
  836. STACK_BUFFER( serverPortBuff, 10 );
  837. DWORD cbServerPort;
  838. STACK_BUFFER( debugNodeBuff, 30 );
  839. if (!SERVER_GET(pIReq, "LOCAL_ADDR", &serverNameBuff, &cbServerName)
  840. || !SERVER_GET(pIReq, "SERVER_PORT", &serverPortBuff, &cbServerPort)) {
  841. hr = E_FAIL;
  842. goto LExit;
  843. }
  844. char *szServerName = (char *)serverNameBuff.QueryPtr();
  845. char *szServerPort = (char*)serverPortBuff.QueryPtr();
  846. // resize the debugNodeBuff to hold <serverIP>:<port>'\0'.
  847. if (!debugNodeBuff.Resize(cbServerName + cbServerPort + 2)) {
  848. hr = E_OUTOFMEMORY;
  849. goto LExit;
  850. }
  851. // Syntax is <serverIP:port>
  852. char *szDebugNode = (char *)debugNodeBuff.QueryPtr();
  853. strcpyExA(strcpyExA(strcpyExA(szDebugNode, szServerName), ":"), szServerPort);
  854. // Convert to Wide Char
  855. hr = MultiByteToWideChar(CP_ACP, 0, szDebugNode, -1, reinterpret_cast<wchar_t *>(prgbData), tempMDData.QuerySize() / 2);
  856. if (FAILED(hr))
  857. goto LExit;
  858. }
  859. // We've got the metadata (ServerComment), create a debug node with this name
  860. IDebugApplicationNode *pServerRoot;
  861. if (FAILED(hr = g_pDebugApp->CreateApplicationNode(&pServerRoot)))
  862. goto LExit;
  863. // Create a doc provider for the node
  864. CFileNode *pFileNode = new CFileNode;
  865. if (pFileNode == NULL)
  866. {
  867. hr = E_OUTOFMEMORY;
  868. goto LExit;
  869. }
  870. if (FAILED(hr = pFileNode->Init(reinterpret_cast<wchar_t *>(prgbData))))
  871. goto LExit;
  872. if (FAILED(hr = pServerRoot->SetDocumentProvider(pFileNode)))
  873. goto LExit;
  874. // pFileNode has been AddRef'ed and we don't need it now.
  875. pFileNode->Release();
  876. // Attach to the UI
  877. if (FAILED(pServerRoot->Attach(g_pDebugAppRoot)))
  878. goto LExit;
  879. // OK, Now add this item to the hashtable (this eats the reference from creation)
  880. pDebugNode->m_pServerRoot = pServerRoot;
  881. g_HashMDPath2DebugRoot.AddElem(pDebugNode);
  882. fDeleteDebugNode = FALSE;
  883. }
  884. *ppDebugRoot = pDebugNode->m_pServerRoot;
  885. (*ppDebugRoot)->AddRef();
  886. hr = S_OK;
  887. LExit:
  888. if (fDeleteDebugNode) {
  889. delete pDebugNode;
  890. }
  891. LeaveCriticalSection(&g_csDebugLock);
  892. return hr;
  893. }
  894. /*===================================================================
  895. C F i l e N o d e
  896. Implementation of CFileNode - trivial class
  897. ===================================================================*/
  898. const GUID IID_IFileNode =
  899. { 0x41047bd2, 0xfe1e, 0x11d0, { 0x8f, 0x3f, 0x0, 0xc0, 0x4f, 0xc3, 0x4d, 0xcc } };
  900. CFileNode::CFileNode() : m_cRefs(1), m_cDocuments(0), m_wszName(NULL) {}
  901. CFileNode::~CFileNode() { delete[] m_wszName; }
  902. HRESULT
  903. CFileNode::Init(wchar_t *wszName)
  904. {
  905. if ((m_wszName = new wchar_t [wcslen(wszName) + 1]) == NULL)
  906. return E_OUTOFMEMORY;
  907. wcscpy(m_wszName, wszName);
  908. return S_OK;
  909. }
  910. HRESULT
  911. CFileNode::QueryInterface(const GUID &uidInterface, void **ppvObj)
  912. {
  913. if (uidInterface == IID_IUnknown ||
  914. uidInterface == IID_IDebugDocumentProvider ||
  915. uidInterface == IID_IFileNode)
  916. {
  917. *ppvObj = this;
  918. AddRef();
  919. return S_OK;
  920. }
  921. else
  922. return E_NOINTERFACE;
  923. }
  924. ULONG
  925. CFileNode::AddRef()
  926. {
  927. InterlockedIncrement(reinterpret_cast<long *>(&m_cRefs));
  928. return m_cRefs;
  929. }
  930. ULONG
  931. CFileNode::Release()
  932. {
  933. LONG cRefs = InterlockedDecrement(reinterpret_cast<long *>(&m_cRefs));
  934. if (cRefs)
  935. return cRefs;
  936. delete this;
  937. return 0;
  938. }
  939. HRESULT
  940. CFileNode::GetDocument(IDebugDocument **ppDebugDoc)
  941. {
  942. return QueryInterface(IID_IDebugDocument, reinterpret_cast<void **>(ppDebugDoc));
  943. }
  944. HRESULT
  945. CFileNode::GetName(DOCUMENTNAMETYPE, BSTR *pbstrName)
  946. {
  947. return ((*pbstrName = SysAllocString(m_wszName)) == NULL)? E_OUTOFMEMORY : S_OK;
  948. }