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.

1579 lines
32 KiB

  1. #include "stdafx.h"
  2. #include "emsvc.h"
  3. #include "rwfile.h"
  4. HRESULT
  5. CopyGuid
  6. (
  7. IN unsigned char *pGuidStreamIn,
  8. OUT unsigned char *pGuidStreamOut
  9. )
  10. {
  11. ATLTRACE(_T("CopyGuid\n"));
  12. _ASSERTE(pGuidStreamIn != NULL && pGuidStreamOut != NULL);
  13. if(pGuidStreamIn == NULL || pGuidStreamOut == NULL){
  14. return E_INVALIDARG;
  15. }
  16. memcpy((void *)pGuidStreamOut, (void *)pGuidStreamIn, sizeof GUID);
  17. return S_OK;
  18. }
  19. HRESULT
  20. CreateAndAssignGuid
  21. (
  22. OUT unsigned char *pGuidStream
  23. )
  24. {
  25. ATLTRACE(_T("CreateAndAssignGuid\n"));
  26. _ASSERTE(pGuidStream != NULL);
  27. HRESULT hr = E_FAIL;
  28. if( pGuidStream == NULL ){
  29. hr = E_INVALIDARG;
  30. return hr;
  31. }
  32. hr = CoCreateGuid((GUID*)pGuidStream);
  33. return hr;
  34. }
  35. inline PEMSession AllocSession ( PEmObject pEmObj, PGENTHREAD pEmThrd )
  36. {
  37. ATLTRACE(_T("AllocSession\n"));
  38. PEMSession pSess = new EMSession;
  39. do {
  40. if ( pSess == NULL )
  41. break;
  42. ZeroMemory( (void *) pSess, sizeof EMSession );
  43. pSess->pEmObj = pEmObj;
  44. pSess->pThread = pEmThrd;
  45. pSess->hrDebug = S_OK;
  46. }
  47. while (false);
  48. return pSess;
  49. }
  50. inline void DeallocSession ( PEMSession &pSess )
  51. {
  52. ATLTRACE(_T("DeallocSession\n"));
  53. _ASSERTE(pSess);
  54. if ( pSess ) {
  55. delete pSess;
  56. pSess = NULL;
  57. }
  58. }
  59. inline PEmObject AllocEmObject ( PEmObject pEmObj )
  60. {
  61. ATLTRACE(_T("AllocEmObject\n"));
  62. PEmObject pEmNewObj = new EmObject;
  63. do {
  64. if ( pEmNewObj == NULL )
  65. break;
  66. ZeroMemory( (void *) pEmNewObj, sizeof EmObject );
  67. if( pEmObj ){
  68. memcpy((void *) pEmNewObj, (void *) pEmObj, sizeof EmObject);
  69. }
  70. }
  71. while (false);
  72. return pEmNewObj;
  73. }
  74. inline void DeallocEmObject( PEmObject &pEmObj )
  75. {
  76. ATLTRACE(_T("DeallocEmObject\n"));
  77. _ASSERTE(pEmObj);
  78. if ( pEmObj ) {
  79. delete pEmObj;
  80. pEmObj = NULL;
  81. }
  82. }
  83. PGENTHREAD EmAllocThread
  84. (
  85. IN PEmObject pEmObj
  86. )
  87. {
  88. ATLTRACE(_T("EmAllocThread\n"));
  89. return new CEMSessionThread(pEmObj);
  90. }
  91. void EmDeallocThread ( PGENTHREAD pThread )
  92. {
  93. ATLTRACE(_T("EmDeallocThread\n"));
  94. if ( pThread ) {
  95. delete pThread;
  96. pThread = NULL;
  97. }
  98. }
  99. CExcepMonSessionManager::CExcepMonSessionManager ()
  100. {
  101. ATLTRACE(_T("CExcepMonSessionManager::CExcepMonSessionManager ()\n"));
  102. m_pcs = new CGenCriticalSection;
  103. }
  104. CExcepMonSessionManager::~CExcepMonSessionManager()
  105. {
  106. ATLTRACE(_T("CExcepMonSessionManager::~CExcepMonSessionManager ()\n"));
  107. CleanUp();
  108. delete m_pcs;
  109. }
  110. void
  111. CExcepMonSessionManager::CleanUp()
  112. {
  113. ATLTRACE(_T("CExcepMonSessionManager::CleanUp()\n"));
  114. POSITION pos = NULL;
  115. unsigned char *pGuid = NULL;
  116. PEMSession pEmSess = NULL;
  117. __try {
  118. pos = m_SessTable.GetStartPosition();
  119. while( pos )
  120. {
  121. m_SessTable.GetNextAssoc(pos, pGuid, pEmSess);
  122. RemoveSession(pGuid);
  123. }
  124. }
  125. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  126. _ASSERTE( false );
  127. }
  128. }
  129. HRESULT
  130. CExcepMonSessionManager::AddSession
  131. (
  132. IN PEmObject pEmObj,
  133. OUT PEMSession *ppNewEmSess
  134. )
  135. {
  136. ATLTRACE(_T("CExcepMonSessionManager::AddSession\n"));
  137. _ASSERTE(pEmObj != NULL);
  138. HRESULT hr = E_FAIL;
  139. PEMSession pEMSession = NULL;
  140. PEmObject pNewEmObj = NULL;
  141. PGENTHREAD pEmThrd = NULL;
  142. __try {
  143. do {
  144. if(pEmObj == NULL){
  145. hr = E_INVALIDARG;
  146. break;
  147. }
  148. pNewEmObj = AllocEmObject(pEmObj);
  149. _ASSERTE(pNewEmObj != NULL);
  150. if(pNewEmObj == NULL){
  151. hr = E_OUTOFMEMORY;
  152. break;
  153. }
  154. hr = CreateAndAssignGuid(pNewEmObj->guidstream);
  155. FAILEDHR_BREAK(hr);
  156. hr = CopyGuid(pNewEmObj->guidstream, pEmObj->guidstream);
  157. FAILEDHR_BREAK(hr);
  158. pEmThrd = EmAllocThread(pNewEmObj);
  159. _ASSERTE(pEmThrd != NULL);
  160. if(pEmThrd == NULL){
  161. hr = E_OUTOFMEMORY;
  162. break;
  163. }
  164. pEMSession = AllocSession(pNewEmObj, pEmThrd);
  165. _ASSERTE(pEMSession != NULL);
  166. if(pEMSession == NULL){
  167. hr = E_OUTOFMEMORY;
  168. break;
  169. }
  170. }
  171. while ( false );
  172. if( FAILED(hr) ){
  173. if(pNewEmObj) DeallocEmObject( pNewEmObj );
  174. if(pEmThrd) EmDeallocThread(pEmThrd);
  175. if(pEMSession) DeallocSession(pEMSession);
  176. }
  177. else{
  178. m_pcs->WriteLock();
  179. pNewEmObj->nStatus &= STAT_SESS_NOT_STARTED;
  180. //
  181. // Using pNewEmObj->guidstream itself as the key will
  182. // save some memory.
  183. //
  184. m_SessTable.SetAt(pNewEmObj->guidstream, pEMSession);
  185. *ppNewEmSess = pEMSession;
  186. m_pcs->WriteUnlock();
  187. }
  188. }
  189. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  190. if(pNewEmObj) DeallocEmObject( pNewEmObj );
  191. if(pEmThrd) EmDeallocThread(pEmThrd);
  192. if(pEMSession) DeallocSession(pEMSession);
  193. hr = E_UNEXPECTED;
  194. _ASSERTE( false );
  195. }
  196. return hr;
  197. }
  198. HRESULT
  199. CExcepMonSessionManager::RemoveSession
  200. (
  201. IN unsigned char *pGuidStream
  202. )
  203. {
  204. ATLTRACE(_T("CExcepMonSessionManager::RemoveSession\n"));
  205. HRESULT hr = E_FAIL;
  206. POSITION pos = NULL;
  207. unsigned char *pGuid = NULL;
  208. PEMSession pRecEmSess = NULL;
  209. m_pcs->WriteLock();
  210. __try {
  211. do {
  212. pos = m_SessTable.GetStartPosition();
  213. while( pos ){
  214. m_SessTable.GetNextAssoc( pos, pGuid, pRecEmSess );
  215. _ASSERTE(pGuid != NULL && pRecEmSess != NULL);
  216. if(memcmp(pGuidStream, pGuid, sizeof GUID) == 0){
  217. m_SessTable.RemoveKey ( pGuid );
  218. hr = S_OK;
  219. break;
  220. }
  221. }
  222. } while ( false );
  223. m_pcs->WriteUnlock();
  224. if (SUCCEEDED (hr) ) {
  225. hr = InternalRemoveSession ( pRecEmSess );
  226. }
  227. }
  228. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  229. hr = E_UNEXPECTED;
  230. _ASSERTE( false );
  231. }
  232. return ( hr );
  233. }
  234. HRESULT
  235. CExcepMonSessionManager::GetSession
  236. (
  237. IN unsigned char *pGuid,
  238. OUT PPEMSession ppEmSess
  239. )
  240. {
  241. ATLTRACE(_T("CExcepMonSessionManager::GetSession\n"));
  242. _ASSERTE(pGuid != NULL);
  243. _ASSERTE(ppEmSess != NULL);
  244. HRESULT hr = E_FAIL;
  245. POSITION pos = NULL;
  246. unsigned char *pRecGuid = NULL;
  247. PEMSession pRecEmSess = NULL;
  248. __try {
  249. do
  250. {
  251. if( pGuid == NULL ||
  252. ppEmSess == NULL){
  253. hr = E_INVALIDARG;
  254. break;
  255. }
  256. m_pcs->ReadLock();
  257. pos = m_SessTable.GetStartPosition();
  258. hr = S_FALSE;
  259. while(pos){
  260. m_SessTable.GetNextAssoc( pos, pRecGuid, pRecEmSess);
  261. if(memcmp((void *)pGuid, (void *)pRecGuid, sizeof pGuid) == 0){
  262. if(ppEmSess) *ppEmSess = pRecEmSess;
  263. hr = S_OK;
  264. break;
  265. }
  266. }
  267. m_pcs->ReadUnlock();
  268. }
  269. while( false );
  270. }
  271. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  272. hr = E_UNEXPECTED;
  273. _ASSERTE( false );
  274. }
  275. return hr;
  276. }
  277. HRESULT
  278. CExcepMonSessionManager::GetSession
  279. (
  280. IN UINT nPid,
  281. IN BSTR bstrImageName,
  282. OUT PEMSession *ppEmSess
  283. )
  284. {
  285. ATLTRACE(_T("CExcepMonSessionManager::GetSession\n"));
  286. HRESULT hr = E_FAIL;
  287. POSITION pos = NULL;
  288. unsigned char *pRecGuid = NULL;
  289. PEMSession pRecEmSess = NULL;
  290. __try {
  291. do
  292. {
  293. m_pcs->ReadLock();
  294. pos = m_SessTable.GetStartPosition();
  295. *ppEmSess = NULL;
  296. hr = S_FALSE;
  297. while( pos )
  298. {
  299. m_SessTable.GetNextAssoc(pos, pRecGuid, pRecEmSess);
  300. if( ((UINT)pRecEmSess->pEmObj->nId == nPid) &&
  301. (_tcsicmp(pRecEmSess->pEmObj->szName, bstrImageName) == 0) ) {
  302. *ppEmSess = pRecEmSess;
  303. hr = S_OK;
  304. //
  305. // Here is an open issue.. 1
  306. //
  307. if(pRecEmSess->pEmObj->nStatus & STAT_SESS_DEBUG_IN_PROGRESS) {
  308. break;
  309. }
  310. else {
  311. continue;
  312. }
  313. }
  314. }
  315. m_pcs->ReadUnlock();
  316. }
  317. while( false );
  318. }
  319. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  320. hr = E_UNEXPECTED;
  321. _ASSERTE( false );
  322. }
  323. return hr;
  324. }
  325. HRESULT
  326. CExcepMonSessionManager::GetSessionStatus
  327. (
  328. IN unsigned char *pGuid,
  329. OUT LONG *plSessStaus /* = NULL */,
  330. OUT HRESULT *plHr /* = NULL */
  331. )
  332. {
  333. ATLTRACE(_T("CExcepMonSessionManager::GetSessionStatus\n"));
  334. _ASSERTE( pGuid != NULL );
  335. HRESULT hr = E_FAIL;
  336. unsigned char *pRecGuid = NULL;
  337. PEMSession pEmSess = NULL;
  338. __try {
  339. do
  340. {
  341. if( pGuid == NULL ) { hr = E_INVALIDARG; break; }
  342. hr = EMERROR_INVALIDPROCESS;
  343. if(GetSession( pGuid, &pEmSess ) == S_OK){
  344. if( plSessStaus ) *plSessStaus = pEmSess->pEmObj->nStatus;
  345. if( plHr ) *plHr = pEmSess->pEmObj->hr;
  346. hr = S_OK;
  347. }
  348. }
  349. while( false );
  350. }
  351. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  352. hr = E_UNEXPECTED;
  353. _ASSERTE( false );
  354. }
  355. return hr;
  356. }
  357. HRESULT
  358. CExcepMonSessionManager::SetSessionStatus
  359. (
  360. IN unsigned char *pGuid,
  361. IN LONG lSessStaus,
  362. IN HRESULT lHr,
  363. IN LPCTSTR lpszErrText,
  364. IN bool bRetainOrphanState /* = true */,
  365. IN bool bSetEndTime /* = false */
  366. )
  367. {
  368. ATLTRACE(_T("CExcepMonSessionManager::SetSessionStatus\n"));
  369. HRESULT hr = E_FAIL;
  370. PEMSession pEMSession = NULL;
  371. __try {
  372. do
  373. {
  374. if( pGuid == NULL ){
  375. hr = E_INVALIDARG;
  376. break;
  377. }
  378. hr = EMERROR_INVALIDPROCESS;
  379. m_pcs->WriteLock();
  380. if(GetSession(pGuid, &pEMSession) == S_OK){
  381. bool bOrphan = ((pEMSession->pEmObj->nStatus & STAT_ORPHAN) == STAT_ORPHAN);
  382. pEMSession->pEmObj->nStatus = lSessStaus;
  383. // should be calling OrphanThisSession(..) instead.
  384. if( bRetainOrphanState && bOrphan ) {
  385. pEMSession->pEmObj->nStatus |= STAT_ORPHAN;
  386. }
  387. pEMSession->pEmObj->hr = lHr;
  388. if ( lpszErrText ) {
  389. _tcsncpy (
  390. pEMSession->pEmObj->szBucket1,
  391. lpszErrText,
  392. ( sizeof (pEMSession->pEmObj->szBucket1)/sizeof (TCHAR) ) - 1 );
  393. }
  394. if( bSetEndTime ) {
  395. pEMSession->pEmObj->dateEnd = _Module.GetCurrentTime();
  396. }
  397. hr = S_OK;
  398. }
  399. m_pcs->WriteUnlock();
  400. }
  401. while( false );
  402. }
  403. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  404. hr = E_UNEXPECTED;
  405. _ASSERTE( false );
  406. }
  407. return hr;
  408. }
  409. HRESULT
  410. CExcepMonSessionManager::UpdateSessObject
  411. (
  412. IN unsigned char *pGuid,
  413. IN PEmObject pEmObj
  414. )
  415. {
  416. ATLTRACE(_T("CExcepMonSessionManager::UpdateSessObject\n"));
  417. _ASSERTE( pGuid != NULL );
  418. _ASSERTE( pEmObj != NULL );
  419. HRESULT hr = E_FAIL;
  420. unsigned char *pRecGuid = NULL;
  421. PEMSession pRecEmSess = NULL;
  422. __try {
  423. do
  424. {
  425. if( pGuid == NULL ||
  426. pEmObj == NULL ){
  427. hr = E_INVALIDARG;
  428. break;
  429. }
  430. hr = EMERROR_INVALIDPROCESS;
  431. m_pcs->WriteLock();
  432. if(GetSession(pGuid, &pRecEmSess) == S_OK){
  433. memcpy((void *) pRecEmSess->pEmObj, (void *) pEmObj, sizeof EmObject);
  434. hr = S_OK;
  435. }
  436. m_pcs->WriteUnlock();
  437. }
  438. while( false );
  439. }
  440. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  441. hr = E_UNEXPECTED;
  442. _ASSERTE( false );
  443. }
  444. return hr;
  445. }
  446. HRESULT
  447. CExcepMonSessionManager::InternalRemoveSession
  448. (
  449. PEMSession pEMSession
  450. )
  451. {
  452. ATLTRACE(_T("CExcepMonSessionManager::InternalRemoveSession\n"));
  453. _ASSERTE( pEMSession != NULL );
  454. __try {
  455. if( pEMSession == NULL ){ return E_INVALIDARG; }
  456. if ( pEMSession->pThread ) {
  457. // pEMSession->pThread->Stop ();
  458. EmDeallocThread ( pEMSession->pThread );
  459. }
  460. if( pEMSession->pEmObj ) {
  461. DeallocEmObject( pEMSession->pEmObj );
  462. }
  463. DeallocSession ( pEMSession );
  464. }
  465. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  466. _ASSERTE( false );
  467. }
  468. return S_OK;
  469. }
  470. HRESULT
  471. CExcepMonSessionManager::StopAllThreads()
  472. {
  473. ATLTRACE(_T("CExcepMonSessionManager::StopAllThreads\n"));
  474. HRESULT hr = E_FAIL;
  475. m_pcs->ReadLock ();
  476. __try {
  477. hr = InternalStopAllThreads ();
  478. }
  479. __except (EXCEPTION_EXECUTE_HANDLER, 1) {
  480. _ASSERTE (false);
  481. }
  482. m_pcs->ReadUnlock ();
  483. return (hr);
  484. };
  485. HRESULT
  486. CExcepMonSessionManager::InternalStopAllThreads()
  487. {
  488. ATLTRACE(_T("CExcepMonSessionManager::InternalStopAllThreads\n"));
  489. unsigned char *pGuid = NULL;
  490. PEMSession pEMSession = NULL;
  491. POSITION pos = NULL;
  492. m_pcs->ReadLock();
  493. __try {
  494. for ( pos = m_SessTable.GetStartPosition ( ) ;
  495. pos != NULL ; ) {
  496. m_SessTable.GetNextAssoc ( pos, pGuid, pEMSession );
  497. if(pEMSession->pThread) {
  498. pEMSession->pThread->Stop();
  499. }
  500. }
  501. }
  502. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  503. _ASSERTE( false );
  504. }
  505. m_pcs->ReadUnlock();
  506. return S_OK;
  507. }
  508. /**********************/
  509. HRESULT
  510. CExcepMonSessionManager::IsAlreadyBeingDebugged
  511. (
  512. IN PEmObject pEmObj
  513. )
  514. {
  515. ATLTRACE(_T("CExcepMonSessionManager::IsAlreadyBeingDebugged\n"));
  516. _ASSERTE(pEmObj != NULL);
  517. HRESULT hr = E_FAIL;
  518. POSITION pos = NULL;
  519. unsigned char *pGuid = NULL;
  520. PEMSession pRecEmSess = NULL;
  521. __try {
  522. do
  523. {
  524. if( pEmObj == NULL ) {
  525. hr = E_INVALIDARG;
  526. break;
  527. }
  528. m_pcs->ReadLock();
  529. pos = m_SessTable.GetStartPosition();
  530. hr = EMERROR_INVALIDPROCESS;
  531. while(pos){
  532. m_SessTable.GetNextAssoc( pos, pGuid, pRecEmSess );
  533. // This should never happen.
  534. _ASSERTE(pRecEmSess != NULL && pGuid != NULL);
  535. //
  536. // This is not a fool proof test. But this is the best
  537. // that can be done to test if it is being debugged..
  538. //
  539. if( (pRecEmSess->pEmObj->nId == pEmObj->nId) &&
  540. (_tcscmp(pRecEmSess->pEmObj->szName, pEmObj->szName) == 0) ) {
  541. hr = S_FALSE;
  542. if( HIWORD(pRecEmSess->pEmObj->nStatus) > HIWORD(STAT_SESS_NOT_STARTED) ) {
  543. hr = S_OK;
  544. }
  545. break;
  546. }
  547. }
  548. m_pcs->ReadUnlock();
  549. }
  550. while( false );
  551. }
  552. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  553. hr = E_UNEXPECTED;
  554. _ASSERTE( false );
  555. }
  556. return hr;
  557. }
  558. HRESULT
  559. CExcepMonSessionManager::GetNumberOfStoppedSessions
  560. (
  561. OUT DWORD *pdwNumOfStoppedSessions
  562. )
  563. {
  564. ATLTRACE(_T("CExcepMonSessionManager::GetNumberOfStoppedSessions\n"));
  565. _ASSERTE( pdwNumOfStoppedSessions != NULL);
  566. HRESULT hr = E_INVALIDARG;
  567. POSITION pos = NULL;
  568. unsigned char *pGuid = NULL;
  569. PEMSession pEmSess = NULL;
  570. __try {
  571. if( pdwNumOfStoppedSessions == NULL ){ return hr; }
  572. *pdwNumOfStoppedSessions = 0L;
  573. m_pcs->ReadLock();
  574. pos = m_SessTable.GetStartPosition();
  575. hr = S_FALSE;
  576. while( pos ){
  577. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  578. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  579. if(pEmSess->pEmObj->nStatus & STAT_SESS_STOPPED){
  580. *pdwNumOfStoppedSessions += 1;
  581. hr = S_OK;
  582. }
  583. }
  584. m_pcs->ReadUnlock();
  585. }
  586. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  587. hr = E_UNEXPECTED;
  588. _ASSERTE( false );
  589. }
  590. return hr;
  591. }
  592. HRESULT
  593. CExcepMonSessionManager::GetFirstStoppedSession
  594. (
  595. OUT POSITION *ppos,
  596. OUT unsigned char **ppGuid,
  597. OUT PEMSession *ppEmSess
  598. )
  599. {
  600. ATLTRACE(_T("CExcepMonSessionManager::GetFirstStoppedSession\n"));
  601. _ASSERTE( ppos != NULL);
  602. HRESULT hr = E_INVALIDARG;
  603. POSITION pos = NULL;
  604. unsigned char *pGuid = NULL;
  605. PEMSession pEmSess = NULL;
  606. __try {
  607. if( ppos == NULL ){ return hr; }
  608. m_pcs->ReadLock();
  609. pos = m_SessTable.GetStartPosition();
  610. hr = S_FALSE;
  611. while( pos ){
  612. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  613. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  614. if(pEmSess->pEmObj->nStatus & STAT_SESS_STOPPED){
  615. if(ppGuid) *ppGuid = pGuid;
  616. if(ppEmSess) *ppEmSess = pEmSess;
  617. *ppos = pos;
  618. hr = S_OK;
  619. break;
  620. }
  621. }
  622. m_pcs->ReadUnlock();
  623. }
  624. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  625. hr = E_UNEXPECTED;
  626. _ASSERTE( false );
  627. }
  628. return hr;
  629. }
  630. HRESULT
  631. CExcepMonSessionManager::GetNextStoppedSession
  632. (
  633. IN OUT POSITION *ppos,
  634. OUT unsigned char **ppGuid,
  635. OUT PEMSession *ppEmSess
  636. )
  637. {
  638. ATLTRACE(_T("CExcepMonSessionManager::GetNextStoppedSession\n"));
  639. _ASSERTE( ppos != NULL);
  640. HRESULT hr = E_INVALIDARG;
  641. POSITION pos = NULL;
  642. unsigned char *pGuid = NULL;
  643. PEMSession pEmSess = NULL;
  644. __try {
  645. if( ppos == NULL ){ return hr; }
  646. m_pcs->ReadLock();
  647. pos = *ppos;
  648. hr = S_FALSE;
  649. while( pos ){
  650. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  651. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  652. if(pEmSess->pEmObj->nStatus & STAT_SESS_STOPPED){
  653. if(ppGuid) *ppGuid = pGuid;
  654. if(ppEmSess) *ppEmSess = pEmSess;
  655. *ppos = pos;
  656. hr = S_OK;
  657. break;
  658. }
  659. }
  660. m_pcs->ReadUnlock();
  661. }
  662. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  663. hr = E_UNEXPECTED;
  664. _ASSERTE( false );
  665. }
  666. return hr;
  667. }
  668. BOOL
  669. CExcepMonSessionManager::IsPortInUse
  670. (
  671. IN UINT nPort
  672. )
  673. {
  674. BOOL bRet = FALSE;
  675. POSITION pos = NULL;
  676. unsigned char *pGuid = NULL;
  677. PEMSession pEmSess = NULL;
  678. __try {
  679. m_pcs->ReadLock();
  680. pos = m_SessTable.GetStartPosition();
  681. while( pos ){
  682. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  683. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  684. if( ((CEMSessionThread *)pEmSess->pThread)->m_nPort == nPort ){
  685. bRet = TRUE;
  686. break;
  687. }
  688. }
  689. m_pcs->ReadUnlock();
  690. }
  691. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  692. _ASSERTE( false );
  693. }
  694. return bRet;
  695. }
  696. HRESULT
  697. CExcepMonSessionManager::GetFirstSession
  698. (
  699. OUT POSITION *ppos,
  700. OUT PEMSession *ppEmSess
  701. )
  702. {
  703. ATLTRACE(_T("CExcepMonSessionManager::GetFirstStoppedSession\n"));
  704. _ASSERTE( ppos != NULL);
  705. HRESULT hr = E_INVALIDARG;
  706. POSITION pos = NULL;
  707. unsigned char *pGuid = NULL;
  708. PEMSession pEmSess = NULL;
  709. __try {
  710. if( ppos == NULL ){ return hr; }
  711. m_pcs->ReadLock();
  712. pos = m_SessTable.GetStartPosition();
  713. hr = S_FALSE;
  714. if( pos != NULL ) {
  715. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  716. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  717. if(ppEmSess) *ppEmSess = pEmSess;
  718. *ppos = pos;
  719. hr = S_OK;
  720. }
  721. m_pcs->ReadUnlock();
  722. }
  723. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  724. hr = E_UNEXPECTED;
  725. _ASSERTE( false );
  726. }
  727. return hr;
  728. }
  729. HRESULT
  730. CExcepMonSessionManager::GetNextSession
  731. (
  732. IN OUT POSITION *ppos,
  733. OUT PEMSession *ppEmSess
  734. )
  735. {
  736. ATLTRACE(_T("CExcepMonSessionManager::GetNextSession\n"));
  737. _ASSERTE( ppos != NULL );
  738. HRESULT hr = E_INVALIDARG;
  739. POSITION pos = NULL;
  740. unsigned char *pGuid = NULL;
  741. PEMSession pEmSess = NULL;
  742. __try {
  743. if( ppos == NULL ){ return hr; }
  744. m_pcs->ReadLock();
  745. pos = *ppos;
  746. hr = S_FALSE;
  747. while( pos ){
  748. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  749. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  750. if(ppEmSess) *ppEmSess = pEmSess;
  751. *ppos = pos;
  752. hr = S_OK;
  753. }
  754. m_pcs->ReadUnlock();
  755. }
  756. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  757. hr = E_UNEXPECTED;
  758. _ASSERTE( false );
  759. }
  760. return hr;
  761. }
  762. HRESULT
  763. CExcepMonSessionManager::GetNumberOfSessions
  764. (
  765. OUT DWORD *pdwNumOfSessions
  766. )
  767. {
  768. ATLTRACE(_T("CExcepMonSessionManager::GetNumberOfSessions\n"));
  769. _ASSERTE( pdwNumOfSessions != NULL );
  770. HRESULT hr = E_INVALIDARG;
  771. POSITION pos = NULL;
  772. unsigned char *pGuid = NULL;
  773. PEMSession pEmSess = NULL;
  774. __try {
  775. if( pdwNumOfSessions == NULL ){ return hr; }
  776. *pdwNumOfSessions = 0L;
  777. m_pcs->ReadLock();
  778. pos = m_SessTable.GetStartPosition();
  779. hr = S_FALSE;
  780. while( pos ){
  781. m_SessTable.GetNextAssoc( pos, pGuid, pEmSess );
  782. _ASSERTE(pGuid != NULL && pEmSess != NULL);
  783. *pdwNumOfSessions += 1;
  784. hr = S_OK;
  785. }
  786. m_pcs->ReadUnlock();
  787. }
  788. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  789. hr = E_UNEXPECTED;
  790. _ASSERTE( false );
  791. }
  792. return hr;
  793. }
  794. HRESULT
  795. CExcepMonSessionManager::UpdateSessObject
  796. (
  797. IN unsigned char *pGuid,
  798. IN DWORD dwEmObjFlds,
  799. IN PEmObject pEmObj
  800. )
  801. {
  802. return CExcepMonSessionManager::UpdateSessObject (
  803. pGuid, dwEmObjFlds, pEmObj->type, pEmObj->type2,
  804. pEmObj->guidstream, pEmObj->nId, pEmObj->szName,
  805. pEmObj->szSecName, pEmObj->nStatus, pEmObj->dateStart,
  806. pEmObj->dateEnd, pEmObj->szBucket1, pEmObj->dwBucket1,
  807. pEmObj->hr
  808. );
  809. }
  810. HRESULT
  811. CExcepMonSessionManager::UpdateSessObject
  812. (
  813. IN unsigned char *pGuid,
  814. IN DWORD dwEmObjFlds,
  815. IN short type,
  816. IN short type2,
  817. IN unsigned char *pguidstream,
  818. IN LONG nId,
  819. IN TCHAR *pszName,
  820. IN TCHAR *pszSecName,
  821. IN LONG nStatus,
  822. IN DATE dateStart,
  823. IN DATE dateEnd,
  824. IN TCHAR *pszBucket1,
  825. IN DWORD dwBucket1,
  826. IN HRESULT hrr
  827. )
  828. {
  829. ATLTRACE(_T("CExcepMonSessionManager::UpdateSessObject\n"));
  830. _ASSERTE( pGuid != NULL );
  831. HRESULT hr = E_FAIL;
  832. unsigned char *pRecGuid = NULL;
  833. PEMSession pRecEmSess = NULL;
  834. __try {
  835. do
  836. {
  837. if( pGuid == NULL ){
  838. hr = E_INVALIDARG;
  839. break;
  840. }
  841. hr = EMERROR_INVALIDPROCESS;
  842. m_pcs->WriteLock();
  843. if(GetSession(pGuid, &pRecEmSess) == S_OK){
  844. if( dwEmObjFlds & EMOBJ_FLD_TYPE ) {
  845. pRecEmSess->pEmObj->type = type;
  846. }
  847. if( dwEmObjFlds & EMOBJ_FLD_TYPE2 ) {
  848. pRecEmSess->pEmObj->type2 = type2;
  849. }
  850. if( dwEmObjFlds & EMOBJ_FLD_GUIDSTREAM ) {
  851. memcpy((void*)pRecEmSess->pEmObj->guidstream, pguidstream, sizeof pRecEmSess->pEmObj->guidstream );
  852. }
  853. if( dwEmObjFlds & EMOBJ_FLD_SZNAME ) {
  854. _tcsncpy(pRecEmSess->pEmObj->szName, pszName, sizeof pRecEmSess->pEmObj->szName / sizeof TCHAR );
  855. }
  856. if( dwEmObjFlds & EMOBJ_FLD_SZSECNAME ) {
  857. _tcsncpy(pRecEmSess->pEmObj->szSecName, pszSecName, sizeof pRecEmSess->pEmObj->szSecName / sizeof TCHAR );
  858. }
  859. if( dwEmObjFlds & EMOBJ_FLD_NSTATUS ) {
  860. pRecEmSess->pEmObj->nStatus = nStatus;
  861. }
  862. if( dwEmObjFlds & EMOBJ_FLD_DATESTART ) {
  863. pRecEmSess->pEmObj->dateStart = dateStart;
  864. }
  865. if( dwEmObjFlds & EMOBJ_FLD_DATEEND ) {
  866. pRecEmSess->pEmObj->dateEnd = dateEnd;
  867. }
  868. if( dwEmObjFlds & EMOBJ_FLD_SZBUCKET1 ) {
  869. _tcsncpy(pRecEmSess->pEmObj->szBucket1, pszBucket1, sizeof pRecEmSess->pEmObj->szBucket1 / sizeof TCHAR );
  870. }
  871. if( dwEmObjFlds & EMOBJ_FLD_DWBUCKET1 ) {
  872. pRecEmSess->pEmObj->dwBucket1 = dwBucket1;
  873. }
  874. if( dwEmObjFlds & EMOBJ_FLD_HR ) {
  875. pRecEmSess->pEmObj->hr = hrr;
  876. }
  877. hr = S_OK;
  878. }
  879. m_pcs->WriteUnlock();
  880. }
  881. while( false );
  882. }
  883. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  884. hr = E_UNEXPECTED;
  885. _ASSERTE( false );
  886. }
  887. return hr;
  888. }
  889. HRESULT
  890. CExcepMonSessionManager::IsSessionOrphaned
  891. (
  892. IN unsigned char *pGuid
  893. )
  894. {
  895. _ASSERTE( pGuid != NULL );
  896. PEMSession pEmSess = NULL;
  897. HRESULT hr = E_FAIL;
  898. __try
  899. {
  900. if( pGuid == NULL ) { hr = E_INVALIDARG; goto qIsSessionOrphaned; }
  901. hr = GetSession(pGuid, &pEmSess);
  902. if( FAILED(hr) ) { goto qIsSessionOrphaned; }
  903. hr = S_FALSE;
  904. if( pEmSess->pEmObj->nStatus & STAT_ORPHAN ) { hr = S_OK; }
  905. qIsSessionOrphaned:
  906. if(FAILED(hr)){}
  907. }
  908. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  909. hr = E_UNEXPECTED;
  910. _ASSERTE( false );
  911. }
  912. return hr;
  913. }
  914. HRESULT
  915. CExcepMonSessionManager::OrphanThisSession
  916. (
  917. IN unsigned char *pGuid
  918. )
  919. {
  920. _ASSERTE(pGuid != NULL);
  921. HRESULT hr = E_FAIL,
  922. hrSess = E_FAIL;
  923. LONG lSessStatus = 0L;
  924. __try
  925. {
  926. if( !pGuid ) { hr = E_INVALIDARG; goto qOrphanThisSession; }
  927. hr = GetSessionStatus(pGuid, &lSessStatus, &hrSess);
  928. if( FAILED(hr) ) goto qOrphanThisSession;
  929. lSessStatus += STAT_ORPHAN;
  930. hr = SetSessionStatus( pGuid, lSessStatus, hrSess );
  931. qOrphanThisSession:
  932. if( FAILED(hr) ) {}
  933. }
  934. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  935. hr = E_UNEXPECTED;
  936. _ASSERTE( false );
  937. }
  938. return hr;
  939. }
  940. HRESULT
  941. CExcepMonSessionManager::AdoptThisSession
  942. (
  943. IN unsigned char *pGuid
  944. )
  945. {
  946. _ASSERTE(pGuid != NULL);
  947. HRESULT hr = E_FAIL,
  948. hrSess = E_FAIL;
  949. LONG lSessStatus = 0L;
  950. __try
  951. {
  952. if( !pGuid ) { hr = E_INVALIDARG; goto qOrphanThisSession; }
  953. hr = GetSessionStatus(pGuid, &lSessStatus, &hrSess);
  954. if( FAILED(hr) ) goto qOrphanThisSession;
  955. if( lSessStatus & STAT_ORPHAN ) { lSessStatus -= STAT_ORPHAN; }
  956. hr = SetSessionStatus( pGuid, lSessStatus, hrSess, NULL, false );
  957. qOrphanThisSession:
  958. if( FAILED(hr) ) {}
  959. }
  960. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  961. hr = E_UNEXPECTED;
  962. _ASSERTE( false );
  963. }
  964. return hr;
  965. }
  966. HRESULT
  967. CExcepMonSessionManager::PersistSessions
  968. (
  969. IN LPCTSTR lpFilePath
  970. )
  971. {
  972. HRESULT hr = E_FAIL;
  973. DWORD dwWritten = 0L;
  974. POSITION pos = NULL;
  975. unsigned char *pGuid = NULL;
  976. PEMSession pEmSess = NULL;
  977. LPVOID lpvSess = NULL;
  978. DWORD dwBytes = 0L;
  979. /*
  980. HRESULT hrDebug;
  981. PGENTHREAD pThread;
  982. PEmObject pEmObj;
  983. */
  984. CRWFile fileSessLog;
  985. m_pcs->WriteLock();
  986. if( !lpFilePath ) { hr = E_INVALIDARG; goto qPersistSessions; }
  987. hr = fileSessLog.InitFile(
  988. lpFilePath,
  989. GENERIC_WRITE,
  990. FILE_SHARE_READ,
  991. NULL,
  992. CREATE_ALWAYS,
  993. FILE_ATTRIBUTE_NORMAL,
  994. NULL
  995. );
  996. if( FAILED(hr) ) { goto qPersistSessions; }
  997. for ( pos = m_SessTable.GetStartPosition(); pos != NULL; ) {
  998. m_SessTable.GetNextAssoc ( pos, pGuid, pEmSess );
  999. if(pEmSess) {
  1000. /* dwBytes = sizeof HRESULT + sizeof PGENTHREAD + sizeof EmObject + 1;
  1001. lpvSess = malloc( dwBytes );
  1002. if( !lpvSess ) { hr = E_OUTOFMEMORY; goto qPersistSessions; }
  1003. memcpy( lpvSess, &(pEmSess->hrDebug), sizeof HRESULT );
  1004. memcpy( (void*)((long)lpvSess + sizeof HRESULT ),
  1005. (void*)&(pEmSess->pThread),
  1006. sizeof PGENTHREAD
  1007. );
  1008. memcpy( (void*)((long)lpvSess + sizeof HRESULT + sizeof PGENTHREAD),
  1009. (void*)(pEmSess->pEmObj),
  1010. sizeof EmObject
  1011. );
  1012. */
  1013. hr = fileSessLog.Write(
  1014. (LPVOID)pEmSess->pEmObj,
  1015. sizeof EmObject,
  1016. &dwWritten,
  1017. NULL
  1018. );
  1019. // if( !lpvSess ) { free( lpvSess ); lpvSess = NULL; }
  1020. }
  1021. }
  1022. hr = S_OK;
  1023. qPersistSessions:
  1024. // if( !lpvSess ) { free( lpvSess ); lpvSess = NULL; }
  1025. if( FAILED(hr) ){}
  1026. m_pcs->WriteUnlock();
  1027. return hr;
  1028. }
  1029. HRESULT
  1030. CExcepMonSessionManager::InitSessionsFromLog
  1031. (
  1032. IN LPCTSTR lpFilePath,
  1033. IN EmStatusHiWord lStatusHi,
  1034. IN EmStatusLoWord lStatusLo
  1035. )
  1036. {
  1037. HRESULT hr = E_FAIL;
  1038. DWORD dwRead = 0L;
  1039. EmObject EmObj;
  1040. CRWFile fileSessLog;
  1041. m_pcs->WriteLock();
  1042. if( !lpFilePath ) { hr = E_INVALIDARG; goto qInitSessionsFromLog; }
  1043. hr = fileSessLog.InitFile(
  1044. lpFilePath,
  1045. GENERIC_READ,
  1046. FILE_SHARE_READ,
  1047. NULL,
  1048. OPEN_EXISTING,
  1049. FILE_ATTRIBUTE_NORMAL,
  1050. NULL
  1051. );
  1052. if( FAILED(hr) ) { goto qInitSessionsFromLog; }
  1053. do
  1054. {
  1055. hr = fileSessLog.Read(
  1056. (LPVOID)&EmObj,
  1057. sizeof EmObj,
  1058. &dwRead,
  1059. NULL
  1060. );
  1061. if( hr != S_OK ) break;
  1062. if( lStatusHi && (EmObj.nStatus & lStatusHi) ) {
  1063. hr = AddLoggedSession( &EmObj );
  1064. }
  1065. } while ( hr == S_OK );
  1066. hr = S_OK;
  1067. qInitSessionsFromLog:
  1068. if( FAILED(hr) ){}
  1069. m_pcs->WriteUnlock();
  1070. return hr;
  1071. }
  1072. HRESULT
  1073. CExcepMonSessionManager::AddLoggedSession
  1074. (
  1075. IN PEmObject pEmObj
  1076. )
  1077. {
  1078. ATLTRACE(_T("CExcepMonSessionManager::AddLoggedSession\n"));
  1079. _ASSERTE(pEmObj != NULL);
  1080. HRESULT hr = E_FAIL;
  1081. PEMSession pEMSession = NULL;
  1082. PEmObject pNewEmObj = NULL;
  1083. PGENTHREAD pEmThrd = NULL;
  1084. __try {
  1085. do {
  1086. if(pEmObj == NULL){
  1087. hr = E_INVALIDARG;
  1088. break;
  1089. }
  1090. pNewEmObj = AllocEmObject(pEmObj);
  1091. _ASSERTE(pNewEmObj != NULL);
  1092. if(pNewEmObj == NULL){
  1093. hr = E_OUTOFMEMORY;
  1094. break;
  1095. }
  1096. pEmThrd = NULL;
  1097. pEMSession = AllocSession(pNewEmObj, pEmThrd);
  1098. _ASSERTE(pEMSession != NULL);
  1099. if(pEMSession == NULL){
  1100. hr = E_OUTOFMEMORY;
  1101. break;
  1102. }
  1103. hr = S_OK;
  1104. }
  1105. while ( false );
  1106. if( FAILED(hr) ){
  1107. if(pNewEmObj) DeallocEmObject( pNewEmObj );
  1108. if(pEmThrd) EmDeallocThread(pEmThrd);
  1109. if(pEMSession) DeallocSession(pEMSession);
  1110. }
  1111. else{
  1112. m_pcs->WriteLock();
  1113. //
  1114. // Using pNewEmObj->guidstream itself as the key will
  1115. // save some memory.
  1116. //
  1117. m_SessTable.SetAt(pNewEmObj->guidstream, pEMSession);
  1118. m_pcs->WriteUnlock();
  1119. }
  1120. }
  1121. __except ( EXCEPTION_EXECUTE_HANDLER, 1 ) {
  1122. if(pNewEmObj) DeallocEmObject( pNewEmObj );
  1123. if(pEmThrd) EmDeallocThread(pEmThrd);
  1124. if(pEMSession) DeallocSession(pEMSession);
  1125. hr = E_UNEXPECTED;
  1126. _ASSERTE( false );
  1127. }
  1128. return hr;
  1129. }