1999-08-31 21:40:21 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-09-28 20:14:13 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:21:17 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-02-01 22:40:48 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-02-01 22:40:48 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:21:17 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-28 20:14:13 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 14:21:17 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 14:21:17 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-02-01 22:40:48 +00:00
|
|
|
|
1999-02-03 15:39:13 +00:00
|
|
|
/* nsDll
|
1999-02-01 22:40:48 +00:00
|
|
|
*
|
|
|
|
* Abstraction of a Dll. Stores modifiedTime and size for easy detection of
|
|
|
|
* change in dll.
|
|
|
|
*
|
|
|
|
* dp Suresh <dp@netscape.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "xcDll.h"
|
1999-08-09 00:19:08 +00:00
|
|
|
#include "nsDebug.h"
|
1999-06-22 14:02:58 +00:00
|
|
|
#include "nsIComponentManager.h"
|
2002-06-14 21:33:05 +00:00
|
|
|
#include "nsIComponentLoaderManager.h"
|
1999-08-09 00:19:08 +00:00
|
|
|
#include "nsIModule.h"
|
2000-01-24 21:28:28 +00:00
|
|
|
#include "nsILocalFile.h"
|
2003-03-05 03:22:12 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
1999-08-13 19:27:58 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2000-01-24 21:28:28 +00:00
|
|
|
#include "nsCRT.h"
|
2000-01-28 09:44:05 +00:00
|
|
|
#include "nsString.h"
|
2001-08-17 02:03:34 +00:00
|
|
|
#include "nsITimelineService.h"
|
2004-05-18 05:39:45 +00:00
|
|
|
#include "nsModule.h"
|
2001-11-28 03:52:34 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#if defined(VMS)
|
2000-02-21 22:56:55 +00:00
|
|
|
#include <lib$routines.h>
|
|
|
|
#include <ssdef.h>
|
2001-11-28 03:52:34 +00:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <signal.h>
|
2000-02-21 22:56:55 +00:00
|
|
|
#endif
|
2001-11-28 03:52:34 +00:00
|
|
|
#endif /* defined(DEBUG) */
|
1999-10-06 01:09:13 +00:00
|
|
|
|
2003-08-05 05:29:10 +00:00
|
|
|
#include "nsTraceRefcntImpl.h"
|
|
|
|
|
2003-05-16 13:24:55 +00:00
|
|
|
#define UNLOAD_DEPENDENT_LIBS
|
|
|
|
#ifdef HPUX
|
|
|
|
#undef UNLOAD_DEPENDENT_LIBS
|
|
|
|
#endif
|
|
|
|
|
2002-01-24 01:29:40 +00:00
|
|
|
#include "nsNativeComponentLoader.h"
|
|
|
|
|
2003-05-03 00:54:20 +00:00
|
|
|
nsDll::nsDll(nsIFile *dllSpec, nsNativeComponentLoader *loader)
|
2003-04-08 20:29:43 +00:00
|
|
|
: m_dllSpec(do_QueryInterface(dllSpec)),
|
|
|
|
m_instance(NULL),
|
|
|
|
m_moduleObject(NULL),
|
2003-05-20 02:02:56 +00:00
|
|
|
m_loader(loader),
|
2003-04-08 20:29:43 +00:00
|
|
|
m_markForUnload(PR_FALSE)
|
1999-02-01 22:40:48 +00:00
|
|
|
{
|
2003-05-03 00:54:20 +00:00
|
|
|
NS_ASSERTION(loader, "Null loader when creating a nsDLL");
|
1999-02-09 20:30:34 +00:00
|
|
|
}
|
|
|
|
|
1999-02-03 15:39:13 +00:00
|
|
|
nsDll::~nsDll(void)
|
1999-02-01 22:40:48 +00:00
|
|
|
{
|
2003-05-05 06:06:57 +00:00
|
|
|
//#if DEBUG_dougt
|
1999-09-28 19:42:06 +00:00
|
|
|
// The dll gets deleted when the dllStore is destroyed. This happens on
|
|
|
|
// app shutdown. At that point, unloading dlls can cause crashes if we have
|
|
|
|
// - dll dependencies
|
|
|
|
// - callbacks
|
|
|
|
// - static dtors
|
|
|
|
// Hence turn it back on after all the above have been removed.
|
2003-05-05 06:06:57 +00:00
|
|
|
//Unload();
|
|
|
|
//#endif
|
1999-02-01 22:40:48 +00:00
|
|
|
}
|
|
|
|
|
2003-04-08 20:29:43 +00:00
|
|
|
void
|
|
|
|
nsDll::GetDisplayPath(nsACString& aLeafName)
|
1999-06-22 14:02:58 +00:00
|
|
|
{
|
2003-04-08 20:29:43 +00:00
|
|
|
m_dllSpec->GetNativeLeafName(aLeafName);
|
|
|
|
|
|
|
|
if (aLeafName.IsEmpty())
|
2004-06-17 00:13:25 +00:00
|
|
|
aLeafName.AssignLiteral("unknown!");
|
1999-06-22 14:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsDll::HasChanged()
|
|
|
|
{
|
2003-05-03 00:54:20 +00:00
|
|
|
nsCOMPtr<nsIComponentLoaderManager> manager = do_QueryInterface(m_loader->mCompMgr);
|
2002-06-14 21:33:05 +00:00
|
|
|
if (!manager)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
1999-06-22 14:02:58 +00:00
|
|
|
// If mod date has changed, then dll has changed
|
2000-01-24 21:28:28 +00:00
|
|
|
PRInt64 currentDate;
|
2001-11-26 08:05:05 +00:00
|
|
|
nsresult rv = m_dllSpec->GetLastModifiedTime(¤tDate);
|
2003-03-05 03:22:12 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return PR_TRUE;
|
2002-06-14 21:33:05 +00:00
|
|
|
PRBool changed = PR_TRUE;
|
|
|
|
manager->HasFileChanged(m_dllSpec, nsnull, currentDate, &changed);
|
|
|
|
return changed;
|
1999-06-22 14:02:58 +00:00
|
|
|
}
|
|
|
|
|
1999-02-03 15:39:13 +00:00
|
|
|
PRBool nsDll::Load(void)
|
1999-02-01 22:40:48 +00:00
|
|
|
{
|
|
|
|
if (m_instance != NULL)
|
|
|
|
{
|
|
|
|
// Already loaded
|
|
|
|
return (PR_TRUE);
|
|
|
|
}
|
1999-06-22 14:02:58 +00:00
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
if (m_dllSpec)
|
1999-06-22 14:02:58 +00:00
|
|
|
{
|
2000-12-23 16:43:32 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2003-08-05 05:29:10 +00:00
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
|
2000-12-23 16:43:32 +00:00
|
|
|
#endif
|
2000-01-24 21:28:28 +00:00
|
|
|
|
2003-05-07 18:39:47 +00:00
|
|
|
// load the component
|
2003-03-05 03:22:12 +00:00
|
|
|
nsCOMPtr<nsILocalFile> lf(do_QueryInterface(m_dllSpec));
|
|
|
|
NS_ASSERTION(lf, "nsIFile here must implement a nsILocalFile");
|
|
|
|
lf->Load(&m_instance);
|
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2003-08-05 05:29:10 +00:00
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
|
2000-01-24 21:28:28 +00:00
|
|
|
if (m_instance) {
|
|
|
|
// Inform refcnt tracer of new library so that calls through the
|
|
|
|
// new library can be traced.
|
2003-04-08 20:29:43 +00:00
|
|
|
nsXPIDLCString displayPath;
|
|
|
|
GetDisplayPath(displayPath);
|
2003-08-05 05:29:10 +00:00
|
|
|
nsTraceRefcntImpl::LoadLibrarySymbols(displayPath.get(), m_instance);
|
2000-01-24 21:28:28 +00:00
|
|
|
}
|
2003-03-05 03:22:12 +00:00
|
|
|
#endif
|
1999-06-22 14:02:58 +00:00
|
|
|
}
|
2000-01-24 21:28:28 +00:00
|
|
|
|
2003-03-05 03:22:12 +00:00
|
|
|
#ifdef SHOULD_IMPLEMENT_BREAKAFTERLOAD
|
2000-01-28 09:44:05 +00:00
|
|
|
// Debugging help for components. Component dlls need to have their
|
|
|
|
// symbols loaded before we can put a breakpoint in the debugger.
|
|
|
|
// This will help figureing out the point when the dll was loaded.
|
2003-04-08 20:29:43 +00:00
|
|
|
nsXPIDLCString displayPath;
|
|
|
|
GetDisplayPath(displayPath);
|
|
|
|
BreakAfterLoad(displayPath.get());
|
2000-01-28 09:44:05 +00:00
|
|
|
#endif
|
|
|
|
|
1999-06-22 14:02:58 +00:00
|
|
|
return ((m_instance == NULL) ? PR_FALSE : PR_TRUE);
|
1999-02-01 22:40:48 +00:00
|
|
|
}
|
|
|
|
|
1999-02-03 15:39:13 +00:00
|
|
|
PRBool nsDll::Unload(void)
|
1999-02-01 22:40:48 +00:00
|
|
|
{
|
2003-04-08 20:29:43 +00:00
|
|
|
if (m_instance == NULL)
|
1999-02-01 22:40:48 +00:00
|
|
|
return (PR_FALSE);
|
1999-08-09 00:19:08 +00:00
|
|
|
|
1999-09-26 18:06:41 +00:00
|
|
|
// Shutdown the dll
|
|
|
|
Shutdown();
|
1999-08-09 00:19:08 +00:00
|
|
|
|
2000-12-23 16:43:32 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2003-08-05 05:29:10 +00:00
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
|
2000-12-23 16:43:32 +00:00
|
|
|
#endif
|
1999-02-01 22:40:48 +00:00
|
|
|
PRStatus ret = PR_UnloadLibrary(m_instance);
|
2000-12-23 16:43:32 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2003-08-05 05:29:10 +00:00
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
|
2000-12-23 16:43:32 +00:00
|
|
|
#endif
|
|
|
|
|
1999-02-01 22:40:48 +00:00
|
|
|
if (ret == PR_SUCCESS)
|
|
|
|
{
|
|
|
|
m_instance = NULL;
|
|
|
|
return (PR_TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (PR_FALSE);
|
|
|
|
}
|
|
|
|
|
1999-02-03 15:39:13 +00:00
|
|
|
void * nsDll::FindSymbol(const char *symbol)
|
1999-02-01 22:40:48 +00:00
|
|
|
{
|
|
|
|
if (symbol == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
// If not already loaded, load it now.
|
|
|
|
if (Load() != PR_TRUE)
|
|
|
|
return (NULL);
|
|
|
|
|
1999-08-31 21:40:21 +00:00
|
|
|
return(PR_FindSymbol(m_instance, symbol));
|
1999-02-01 22:40:48 +00:00
|
|
|
}
|
1999-08-09 00:19:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Component dll specific functions
|
2000-01-24 21:28:28 +00:00
|
|
|
nsresult nsDll::GetDllSpec(nsIFile **fsobj)
|
1999-08-09 00:19:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(m_dllSpec, "m_dllSpec NULL");
|
|
|
|
NS_ASSERTION(fsobj, "xcDll::GetModule : Null argument" );
|
|
|
|
|
|
|
|
*fsobj = m_dllSpec;
|
2000-01-24 21:28:28 +00:00
|
|
|
NS_ADDREF(*fsobj);
|
1999-08-09 00:19:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDll::GetModule(nsISupports *servMgr, nsIModule **cobj)
|
|
|
|
{
|
2003-05-03 00:54:20 +00:00
|
|
|
// using the backpointer of the loader.
|
|
|
|
nsIComponentManager* compMgr = m_loader->mCompMgr;
|
1999-08-13 19:27:58 +00:00
|
|
|
NS_ASSERTION(compMgr, "Global Component Manager is null" );
|
2003-05-03 00:54:20 +00:00
|
|
|
if (!compMgr) return NS_ERROR_UNEXPECTED;
|
1999-08-13 19:27:58 +00:00
|
|
|
|
1999-08-09 00:19:08 +00:00
|
|
|
NS_ASSERTION(cobj, "xcDll::GetModule : Null argument" );
|
|
|
|
|
|
|
|
if (m_moduleObject)
|
|
|
|
{
|
|
|
|
NS_ADDREF(m_moduleObject);
|
|
|
|
*cobj = m_moduleObject;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not already loaded, load it now.
|
|
|
|
if (Load() != PR_TRUE) return NS_ERROR_FAILURE;
|
|
|
|
|
2003-03-05 03:22:12 +00:00
|
|
|
// We need a nsIFile for location
|
|
|
|
if (!m_dllSpec)
|
1999-08-09 00:19:08 +00:00
|
|
|
{
|
2003-03-05 03:22:12 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-08-09 00:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsGetModuleProc proc =
|
|
|
|
(nsGetModuleProc) FindSymbol(NS_GET_MODULE_SYMBOL);
|
|
|
|
|
1999-08-31 21:40:21 +00:00
|
|
|
if (proc == NULL)
|
|
|
|
return NS_ERROR_FACTORY_NOT_LOADED;
|
1999-08-09 00:19:08 +00:00
|
|
|
|
2003-05-03 00:54:20 +00:00
|
|
|
nsresult rv = (*proc) (compMgr, m_dllSpec, &m_moduleObject);
|
1999-08-09 00:19:08 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
NS_ADDREF(m_moduleObject);
|
|
|
|
*cobj = m_moduleObject;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
1999-09-26 18:06:41 +00:00
|
|
|
|
2000-06-27 21:54:28 +00:00
|
|
|
|
|
|
|
// These are used by BreakAfterLoad, below.
|
|
|
|
#ifdef SHOULD_IMPLEMENT_BREAKAFTERLOAD
|
|
|
|
static nsCString *sBreakList[16];
|
|
|
|
static int sBreakListCount = 0;
|
|
|
|
#endif
|
|
|
|
|
1999-09-26 18:06:41 +00:00
|
|
|
nsresult nsDll::Shutdown(void)
|
|
|
|
{
|
|
|
|
// Release the module object if we got one
|
|
|
|
nsrefcnt refcnt;
|
|
|
|
if (m_moduleObject)
|
|
|
|
{
|
|
|
|
NS_RELEASE2(m_moduleObject, refcnt);
|
|
|
|
NS_ASSERTION(refcnt == 0, "Dll moduleObject refcount non zero");
|
|
|
|
}
|
2000-06-27 21:54:28 +00:00
|
|
|
#ifdef SHOULD_IMPLEMENT_BREAKAFTERLOAD
|
|
|
|
for (int i = 0; i < sBreakListCount; i++)
|
|
|
|
{
|
|
|
|
delete sBreakList[i];
|
|
|
|
sBreakList[i] = nsnull;
|
|
|
|
}
|
|
|
|
sBreakListCount = 0;
|
|
|
|
#endif
|
1999-09-26 18:06:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
2003-03-05 03:22:12 +00:00
|
|
|
#ifdef SHOULD_IMPLEMENT_BREAKAFTERLOAD
|
2000-01-28 09:44:05 +00:00
|
|
|
void nsDll::BreakAfterLoad(const char *nsprPath)
|
|
|
|
{
|
|
|
|
static PRBool firstTime = PR_TRUE;
|
|
|
|
|
|
|
|
// return if invalid input
|
|
|
|
if (!nsprPath || !*nsprPath) return;
|
|
|
|
|
|
|
|
// return if nothing to break on
|
2000-06-27 21:54:28 +00:00
|
|
|
if (!firstTime && sBreakListCount == 0) return;
|
2000-01-28 09:44:05 +00:00
|
|
|
|
|
|
|
if (firstTime)
|
|
|
|
{
|
|
|
|
firstTime = PR_FALSE;
|
|
|
|
// Form the list of dlls to break on load
|
|
|
|
nsCAutoString envList(getenv("XPCOM_BREAK_ON_LOAD"));
|
|
|
|
if (envList.IsEmpty()) return;
|
|
|
|
PRInt32 ofset = 0;
|
|
|
|
PRInt32 start = 0;
|
|
|
|
do
|
|
|
|
{
|
2002-02-01 01:53:09 +00:00
|
|
|
ofset = envList.FindChar(':', start);
|
2000-06-27 21:54:28 +00:00
|
|
|
sBreakList[sBreakListCount] = new nsCString();
|
|
|
|
envList.Mid(*(sBreakList[sBreakListCount]), start, ofset);
|
|
|
|
sBreakListCount++;
|
2000-01-28 09:44:05 +00:00
|
|
|
start = ofset + 1;
|
|
|
|
}
|
2000-06-27 21:54:28 +00:00
|
|
|
while (ofset != -1 && 16 > sBreakListCount); // avoiding vc6.0 compiler issue. count < thinks it is starting a template
|
2000-01-28 09:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the dllname part of the string
|
|
|
|
nsCString currentPath(nsprPath);
|
|
|
|
PRInt32 lastSep = currentPath.RFindCharInSet(":\\/");
|
|
|
|
|
2000-06-27 21:54:28 +00:00
|
|
|
for (int i=0; i<sBreakListCount; i++)
|
|
|
|
if (currentPath.Find(*(sBreakList[i]), PR_TRUE, lastSep) > 0)
|
2000-01-28 09:44:05 +00:00
|
|
|
{
|
|
|
|
// Loading a dll that we want to break on
|
|
|
|
// Put your breakpoint here
|
2002-05-11 21:15:06 +00:00
|
|
|
fprintf(stderr, "...Loading module %s\n", nsprPath);
|
2000-01-28 09:44:05 +00:00
|
|
|
// Break in the debugger here.
|
2003-03-07 06:07:56 +00:00
|
|
|
#if defined(__i386) && defined(__GNUC__)
|
2000-01-28 09:44:05 +00:00
|
|
|
asm("int $3");
|
2000-03-25 08:54:22 +00:00
|
|
|
#elif defined(VMS)
|
|
|
|
lib$signal(SS$_DEBUG);
|
2001-11-28 03:52:34 +00:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
raise(SIGTRAP);
|
2000-02-21 22:56:55 +00:00
|
|
|
#endif
|
2000-01-28 09:44:05 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2003-03-05 03:22:12 +00:00
|
|
|
#endif /* SHOULD_IMPLEMENT_BREAKAFTERLOAD */
|