1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * LSPs are evil little bits of code that are allowed to inject into our
7 * networking stack by Windows. Once they have wormed into our process
8 * they gnaw at our innards until we crash. Here we force the buggers
9 * into the light by recording them in our crash information.
10 * We do the enumeration on a thread because I'm concerned about startup perf
11 * on machines with several LSPs.
14 #include "nsICrashReporter.h"
15 #include "nsISupportsImpl.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsThreadUtils.h"
22 namespace crashreporter
{
24 class LSPAnnotationGatherer
: public nsRunnable
32 nsCOMPtr
<nsIThread
> mThread
;
35 NS_IMPL_THREADSAFE_ISUPPORTS1(LSPAnnotationGatherer
, nsIRunnable
)
38 LSPAnnotationGatherer::Annotate()
40 nsCOMPtr
<nsICrashReporter
> cr
=
41 do_GetService("@mozilla.org/toolkit/crash-reporter;1");
43 if (cr
&& NS_SUCCEEDED(cr
->GetEnabled(&enabled
)) && enabled
) {
44 cr
->AnnotateCrashReport(NS_LITERAL_CSTRING("Winsock_LSP"), mString
);
50 LSPAnnotationGatherer::Run()
52 PR_SetCurrentThreadName("LSP Annotator");
54 mThread
= NS_GetCurrentThread();
58 // Get the size of the buffer we need
59 if (SOCKET_ERROR
!= WSCEnumProtocols(NULL
, NULL
, &size
, &err
) ||
62 NS_NOTREACHED("WSCEnumProtocols suceeded when it should have failed ...");
63 return NS_ERROR_FAILURE
;
66 nsAutoArrayPtr
<char> byteArray(new char[size
]);
67 WSAPROTOCOL_INFOW
* providers
=
68 reinterpret_cast<WSAPROTOCOL_INFOW
*>(byteArray
.get());
70 int n
= WSCEnumProtocols(NULL
, providers
, &size
, &err
);
71 if (n
== SOCKET_ERROR
) {
72 // Lame. We provided the right size buffer; we'll just give up now.
73 NS_WARNING("Could not get LSP list");
74 return NS_ERROR_FAILURE
;
78 for (int i
= 0; i
< n
; i
++) {
79 AppendUTF16toUTF8(nsDependentString(providers
[i
].szProtocol
), str
);
80 str
.AppendLiteral(" : ");
81 str
.AppendInt(providers
[i
].iVersion
);
82 str
.AppendLiteral(" : ");
83 str
.AppendInt(providers
[i
].iSocketType
);
84 str
.AppendLiteral(" : ");
86 wchar_t path
[MAX_PATH
];
88 if (!WSCGetProviderPath(&providers
[i
].ProviderId
, path
,
90 AppendUTF16toUTF8(nsDependentString(path
), str
);
94 str
.AppendLiteral(" \n ");
99 NS_DispatchToMainThread(NS_NewRunnableMethod(this, &LSPAnnotationGatherer::Annotate
));
105 nsCOMPtr
<nsIThread
> thread
;
106 nsCOMPtr
<nsIRunnable
> runnable
=
107 do_QueryObject(new LSPAnnotationGatherer());
108 NS_NewThread(getter_AddRefs(thread
), runnable
);
111 } // namespace crashreporter
112 } // namespace mozilla