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.

165 lines
3.4 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. dbmon.c
  5. Abstract:
  6. A simple program to print strings passed to OutputDebugString when
  7. the app printing the strings is not being debugged.
  8. Author:
  9. Kent Forschmiedt (kentf) 30-Sep-1994
  10. Revision History:
  11. --*/
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. int _cdecl
  16. main(
  17. int argc,
  18. char ** argv
  19. )
  20. /*++
  21. Routine Description:
  22. Arguments:
  23. Return Value:
  24. --*/
  25. {
  26. HANDLE AckEvent;
  27. HANDLE ReadyEvent;
  28. HANDLE SharedFile;
  29. LPVOID SharedMem;
  30. LPSTR String;
  31. DWORD ret;
  32. DWORD LastPid;
  33. LPDWORD pThisPid;
  34. BOOL DidCR;
  35. SECURITY_ATTRIBUTES sa;
  36. SECURITY_DESCRIPTOR sd;
  37. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  38. sa.bInheritHandle = TRUE;
  39. sa.lpSecurityDescriptor = &sd;
  40. if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
  41. fprintf(stderr,"unable to InitializeSecurityDescriptor, err == %d\n",
  42. GetLastError());
  43. exit(1);
  44. }
  45. if(!SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE)) {
  46. fprintf(stderr,"unable to SetSecurityDescriptorDacl, err == %d\n",
  47. GetLastError());
  48. exit(1);
  49. }
  50. AckEvent = CreateEvent(&sa, FALSE, FALSE, "DBWIN_BUFFER_READY");
  51. if (!AckEvent) {
  52. fprintf(stderr,
  53. "dbmon: Unable to create synchronization object, err == %d\n",
  54. GetLastError());
  55. exit(1);
  56. }
  57. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  58. fprintf(stderr, "dbmon: already running\n");
  59. exit(1);
  60. }
  61. ReadyEvent = CreateEvent(&sa, FALSE, FALSE, "DBWIN_DATA_READY");
  62. if (!ReadyEvent) {
  63. fprintf(stderr,
  64. "dbmon: Unable to create synchronization object, err == %d\n",
  65. GetLastError());
  66. exit(1);
  67. }
  68. SharedFile = CreateFileMapping(
  69. (HANDLE)-1,
  70. &sa,
  71. PAGE_READWRITE,
  72. 0,
  73. 4096,
  74. "DBWIN_BUFFER");
  75. if (!SharedFile) {
  76. fprintf(stderr,
  77. "dbmon: Unable to create file mapping object, err == %d\n",
  78. GetLastError());
  79. exit(1);
  80. }
  81. SharedMem = MapViewOfFile(
  82. SharedFile,
  83. FILE_MAP_READ,
  84. 0,
  85. 0,
  86. 512);
  87. if (!SharedMem) {
  88. fprintf(stderr,
  89. "dbmon: Unable to map shared memory, err == %d\n",
  90. GetLastError());
  91. exit(1);
  92. }
  93. String = (LPSTR)SharedMem + sizeof(DWORD);
  94. pThisPid = SharedMem;
  95. LastPid = 0xffffffff;
  96. DidCR = TRUE;
  97. SetEvent(AckEvent);
  98. for (;;) {
  99. ret = WaitForSingleObject(ReadyEvent, INFINITE);
  100. if (ret != WAIT_OBJECT_0) {
  101. fprintf(stderr, "dbmon: wait failed; err == %d\n", GetLastError());
  102. exit(1);
  103. } else {
  104. if (LastPid != *pThisPid) {
  105. LastPid = *pThisPid;
  106. if (!DidCR) {
  107. putchar('\n');
  108. DidCR = TRUE;
  109. }
  110. }
  111. if (DidCR) {
  112. printf("%3u: ", LastPid);
  113. }
  114. printf("%s", String);
  115. DidCR = (*String && (String[strlen(String) - 1] == '\n'));
  116. SetEvent(AckEvent);
  117. }
  118. }
  119. return 0;
  120. }