andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** prtrace.c -- NSPR Trace Instrumentation andre@0: ** andre@0: ** Implement the API defined in prtrace.h andre@0: ** andre@0: ** andre@0: ** andre@0: */ andre@0: andre@0: #include andre@0: #include "primpl.h" andre@0: andre@0: andre@0: #define DEFAULT_TRACE_BUFSIZE ( 1024 * 1024 ) andre@0: #define DEFAULT_BUFFER_SEGMENTS 2 andre@0: andre@0: /* andre@0: ** Enumerate states in a RName structure andre@0: */ andre@0: typedef enum TraceState andre@0: { andre@0: Running = 1, andre@0: Suspended = 2 andre@0: } TraceState; andre@0: andre@0: /* andre@0: ** Define QName structure andre@0: */ andre@0: typedef struct QName andre@0: { andre@0: PRCList link; andre@0: PRCList rNameList; andre@0: char name[PRTRACE_NAME_MAX+1]; andre@0: } QName; andre@0: andre@0: /* andre@0: ** Define RName structure andre@0: */ andre@0: typedef struct RName andre@0: { andre@0: PRCList link; andre@0: PRLock *lock; andre@0: QName *qName; andre@0: TraceState state; andre@0: char name[PRTRACE_NAME_MAX+1]; andre@0: char desc[PRTRACE_DESC_MAX+1]; andre@0: } RName; andre@0: andre@0: andre@0: /* andre@0: ** The Trace Facility database andre@0: ** andre@0: */ andre@0: static PRLogModuleInfo *lm; andre@0: andre@0: static PRLock *traceLock; /* Facility Lock */ andre@0: static PRCList qNameList; /* anchor to all QName structures */ andre@0: static TraceState traceState = Running; andre@0: andre@0: /* andre@0: ** in-memory trace buffer controls andre@0: */ andre@0: static PRTraceEntry *tBuf; /* pointer to buffer */ andre@0: static PRInt32 bufSize; /* size of buffer, in bytes, rounded up to sizeof(PRTraceEntry) */ andre@0: static volatile PRInt32 next; /* index to next PRTraceEntry */ andre@0: static PRInt32 last; /* index of highest numbered trace entry */ andre@0: andre@0: /* andre@0: ** Real-time buffer capture controls andre@0: */ andre@0: static PRInt32 fetchLastSeen = 0; andre@0: static PRBool fetchLostData = PR_FALSE; andre@0: andre@0: /* andre@0: ** Buffer write-to-file controls andre@0: */ andre@0: static PRLock *logLock; /* Sync lock */ andre@0: static PRCondVar *logCVar; /* Sync Condidtion Variable */ andre@0: /* andre@0: ** Inter-thread state communication. andre@0: ** Controling thread writes to logOrder under protection of logCVar andre@0: ** the logging thread reads logOrder and sets logState on Notify. andre@0: ** andre@0: ** logSegments, logCount, logLostData must be read and written under andre@0: ** protection of logLock, logCVar. andre@0: ** andre@0: */ andre@0: static enum LogState andre@0: { andre@0: LogNotRunning, /* Initial state */ andre@0: LogReset, /* Causes logger to re-calc controls */ andre@0: LogActive, /* Logging in progress, set only by log thread */ andre@0: LogSuspend, /* Suspend Logging */ andre@0: LogResume, /* Resume Logging => LogActive */ andre@0: LogStop /* Stop the log thread */ andre@0: } logOrder, logState, localState; /* controlling state variables */ andre@0: static PRInt32 logSegments; /* Number of buffer segments */ andre@0: static PRInt32 logEntries; /* number of Trace Entries in the buffer */ andre@0: static PRInt32 logEntriesPerSegment; /* number of PRTraceEntries per buffer segment */ andre@0: static PRInt32 logSegSize; /* size of buffer segment */ andre@0: static PRInt32 logCount; /* number of segments pending output */ andre@0: static PRInt32 logLostData; /* number of lost log buffer segments */ andre@0: andre@0: /* andre@0: ** end Trace Database andre@0: ** andre@0: */ andre@0: andre@0: /* andre@0: ** _PR_InitializeTrace() -- Initialize the trace facility andre@0: */ andre@0: static void NewTraceBuffer( PRInt32 size ) andre@0: { andre@0: /* andre@0: ** calculate the size of the buffer andre@0: ** round down so that each segment has the same number of andre@0: ** trace entries andre@0: */ andre@0: logSegments = DEFAULT_BUFFER_SEGMENTS; andre@0: logEntries = size / sizeof(PRTraceEntry); andre@0: logEntriesPerSegment = logEntries / logSegments; andre@0: logEntries = logSegments * logEntriesPerSegment; andre@0: bufSize = logEntries * sizeof(PRTraceEntry); andre@0: logSegSize = logEntriesPerSegment * sizeof(PRTraceEntry); andre@0: PR_ASSERT( bufSize != 0); andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("NewTraceBuffer: logSegments: %ld, logEntries: %ld, logEntriesPerSegment: %ld, logSegSize: %ld", andre@0: logSegments, logEntries, logEntriesPerSegment, logSegSize )); andre@0: andre@0: andre@0: tBuf = PR_Malloc( bufSize ); andre@0: if ( tBuf == NULL ) andre@0: { andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PRTrace: Failed to get trace buffer")); andre@0: PR_ASSERT( 0 ); andre@0: } andre@0: else andre@0: { andre@0: PR_LOG( lm, PR_LOG_NOTICE, andre@0: ("PRTrace: Got trace buffer of size: %ld, at %p", bufSize, tBuf)); andre@0: } andre@0: andre@0: next = 0; andre@0: last = logEntries -1; andre@0: logCount = 0; andre@0: logLostData = PR_TRUE; /* not really on first call */ andre@0: logOrder = LogReset; andre@0: andre@0: } /* end NewTraceBuffer() */ andre@0: andre@0: /* andre@0: ** _PR_InitializeTrace() -- Initialize the trace facility andre@0: */ andre@0: static void _PR_InitializeTrace( void ) andre@0: { andre@0: /* The lock pointer better be null on this call */ andre@0: PR_ASSERT( traceLock == NULL ); andre@0: andre@0: traceLock = PR_NewLock(); andre@0: PR_ASSERT( traceLock != NULL ); andre@0: andre@0: PR_Lock( traceLock ); andre@0: andre@0: PR_INIT_CLIST( &qNameList ); andre@0: andre@0: lm = PR_NewLogModule("trace"); andre@0: andre@0: bufSize = DEFAULT_TRACE_BUFSIZE; andre@0: NewTraceBuffer( bufSize ); andre@0: andre@0: /* Initialize logging controls */ andre@0: logLock = PR_NewLock(); andre@0: logCVar = PR_NewCondVar( logLock ); andre@0: andre@0: PR_Unlock( traceLock ); andre@0: return; andre@0: } /* end _PR_InitializeTrace() */ andre@0: andre@0: /* andre@0: ** Create a Trace Handle andre@0: */ andre@0: PR_IMPLEMENT(PRTraceHandle) andre@0: PR_CreateTrace( andre@0: const char *qName, /* QName for this trace handle */ andre@0: const char *rName, /* RName for this trace handle */ andre@0: const char *description /* description for this trace handle */ andre@0: ) andre@0: { andre@0: QName *qnp; andre@0: RName *rnp; andre@0: PRBool matchQname = PR_FALSE; andre@0: andre@0: /* Self initialize, if necessary */ andre@0: if ( traceLock == NULL ) andre@0: _PR_InitializeTrace(); andre@0: andre@0: /* Validate input arguments */ andre@0: PR_ASSERT( strlen(qName) <= PRTRACE_NAME_MAX ); andre@0: PR_ASSERT( strlen(rName) <= PRTRACE_NAME_MAX ); andre@0: PR_ASSERT( strlen(description) <= PRTRACE_DESC_MAX ); andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRTRACE: CreateTrace: Qname: %s, RName: %s", qName, rName)); andre@0: andre@0: /* Lock the Facility */ andre@0: PR_Lock( traceLock ); andre@0: andre@0: /* Do we already have a matching QName? */ andre@0: if (!PR_CLIST_IS_EMPTY( &qNameList )) andre@0: { andre@0: qnp = (QName *) PR_LIST_HEAD( &qNameList ); andre@0: do { andre@0: if ( strcmp(qnp->name, qName) == 0) andre@0: { andre@0: matchQname = PR_TRUE; andre@0: break; andre@0: } andre@0: qnp = (QName *)PR_NEXT_LINK( &qnp->link ); andre@0: } while( qnp != (QName *)&qNameList ); andre@0: } andre@0: /* andre@0: ** If we did not find a matching QName, andre@0: ** allocate one and initialize it. andre@0: ** link it onto the qNameList. andre@0: ** andre@0: */ andre@0: if ( matchQname != PR_TRUE ) andre@0: { andre@0: qnp = PR_NEWZAP( QName ); andre@0: PR_ASSERT( qnp != NULL ); andre@0: PR_INIT_CLIST( &qnp->link ); andre@0: PR_INIT_CLIST( &qnp->rNameList ); andre@0: strcpy( qnp->name, qName ); andre@0: PR_APPEND_LINK( &qnp->link, &qNameList ); andre@0: } andre@0: andre@0: /* Do we already have a matching RName? */ andre@0: if (!PR_CLIST_IS_EMPTY( &qnp->rNameList )) andre@0: { andre@0: rnp = (RName *) PR_LIST_HEAD( &qnp->rNameList ); andre@0: do { andre@0: /* andre@0: ** No duplicate RNames are allowed within a QName andre@0: ** andre@0: */ andre@0: PR_ASSERT( strcmp(rnp->name, rName)); andre@0: rnp = (RName *)PR_NEXT_LINK( &rnp->link ); andre@0: } while( rnp != (RName *)&qnp->rNameList ); andre@0: } andre@0: andre@0: /* Get a new RName structure; initialize its members */ andre@0: rnp = PR_NEWZAP( RName ); andre@0: PR_ASSERT( rnp != NULL ); andre@0: PR_INIT_CLIST( &rnp->link ); andre@0: strcpy( rnp->name, rName ); andre@0: strcpy( rnp->desc, description ); andre@0: rnp->lock = PR_NewLock(); andre@0: rnp->state = Running; andre@0: if ( rnp->lock == NULL ) andre@0: { andre@0: PR_ASSERT(0); andre@0: } andre@0: andre@0: PR_APPEND_LINK( &rnp->link, &qnp->rNameList ); /* add RName to QName's rnList */ andre@0: rnp->qName = qnp; /* point the RName to the QName */ andre@0: andre@0: /* Unlock the Facility */ andre@0: PR_Unlock( traceLock ); andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: Create: QName: %s %p, RName: %s %p\n\t", andre@0: qName, qnp, rName, rnp )); andre@0: andre@0: return((PRTraceHandle)rnp); andre@0: } /* end PR_CreateTrace() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(void) andre@0: PR_DestroyTrace( andre@0: PRTraceHandle handle /* Handle to be destroyed */ andre@0: ) andre@0: { andre@0: RName *rnp = (RName *)handle; andre@0: QName *qnp = rnp->qName; andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: Deleting: QName: %s, RName: %s", andre@0: qnp->name, rnp->name)); andre@0: andre@0: /* Lock the Facility */ andre@0: PR_Lock( traceLock ); andre@0: andre@0: /* andre@0: ** Remove RName from the list of RNames in QName andre@0: ** and free RName andre@0: */ andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: Deleting RName: %s, %p", andre@0: rnp->name, rnp)); andre@0: PR_REMOVE_LINK( &rnp->link ); andre@0: PR_Free( rnp->lock ); andre@0: PR_DELETE( rnp ); andre@0: andre@0: /* andre@0: ** If this is the last RName within QName andre@0: ** remove QName from the qNameList and free it andre@0: */ andre@0: if ( PR_CLIST_IS_EMPTY( &qnp->rNameList ) ) andre@0: { andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: Deleting unused QName: %s, %p", andre@0: qnp->name, qnp)); andre@0: PR_REMOVE_LINK( &qnp->link ); andre@0: PR_DELETE( qnp ); andre@0: } andre@0: andre@0: /* Unlock the Facility */ andre@0: PR_Unlock( traceLock ); andre@0: return; andre@0: } /* end PR_DestroyTrace() */ andre@0: andre@0: /* andre@0: ** Create a TraceEntry in the trace buffer andre@0: */ andre@0: PR_IMPLEMENT(void) andre@0: PR_Trace( andre@0: PRTraceHandle handle, /* use this trace handle */ andre@0: PRUint32 userData0, /* User supplied data word 0 */ andre@0: PRUint32 userData1, /* User supplied data word 1 */ andre@0: PRUint32 userData2, /* User supplied data word 2 */ andre@0: PRUint32 userData3, /* User supplied data word 3 */ andre@0: PRUint32 userData4, /* User supplied data word 4 */ andre@0: PRUint32 userData5, /* User supplied data word 5 */ andre@0: PRUint32 userData6, /* User supplied data word 6 */ andre@0: PRUint32 userData7 /* User supplied data word 7 */ andre@0: ) andre@0: { andre@0: PRTraceEntry *tep; andre@0: PRInt32 mark; andre@0: andre@0: if ( (traceState == Suspended ) andre@0: || ( ((RName *)handle)->state == Suspended )) andre@0: return; andre@0: andre@0: /* andre@0: ** Get the next trace entry slot w/ minimum delay andre@0: */ andre@0: PR_Lock( traceLock ); andre@0: andre@0: tep = &tBuf[next++]; andre@0: if ( next > last ) andre@0: next = 0; andre@0: if ( fetchLostData == PR_FALSE && next == fetchLastSeen ) andre@0: fetchLostData = PR_TRUE; andre@0: andre@0: mark = next; andre@0: andre@0: PR_Unlock( traceLock ); andre@0: andre@0: /* andre@0: ** We have a trace entry. Fill it in. andre@0: */ andre@0: tep->thread = PR_GetCurrentThread(); andre@0: tep->handle = handle; andre@0: tep->time = PR_Now(); andre@0: tep->userData[0] = userData0; andre@0: tep->userData[1] = userData1; andre@0: tep->userData[2] = userData2; andre@0: tep->userData[3] = userData3; andre@0: tep->userData[4] = userData4; andre@0: tep->userData[5] = userData5; andre@0: tep->userData[6] = userData6; andre@0: tep->userData[7] = userData7; andre@0: andre@0: /* When buffer segment is full, signal trace log thread to run */ andre@0: if (( mark % logEntriesPerSegment) == 0 ) andre@0: { andre@0: PR_Lock( logLock ); andre@0: logCount++; andre@0: PR_NotifyCondVar( logCVar ); andre@0: PR_Unlock( logLock ); andre@0: /* andre@0: ** Gh0D! This is awful! andre@0: ** Anyway, to minimize lost trace data segments, andre@0: ** I inserted the PR_Sleep(0) to cause a context switch andre@0: ** so that the log thread could run. andre@0: ** I know, it perturbs the universe and may cause andre@0: ** funny things to happen in the optimized builds. andre@0: ** Take it out, lose data; leave it in risk Heisenberg. andre@0: */ andre@0: /* PR_Sleep(0); */ andre@0: } andre@0: andre@0: return; andre@0: } /* end PR_Trace() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(void) andre@0: PR_SetTraceOption( andre@0: PRTraceOption command, /* One of the enumerated values */ andre@0: void *value /* command value or NULL */ andre@0: ) andre@0: { andre@0: RName * rnp; andre@0: andre@0: switch ( command ) andre@0: { andre@0: case PRTraceBufSize : andre@0: PR_Lock( traceLock ); andre@0: PR_Free( tBuf ); andre@0: bufSize = *(PRInt32 *)value; andre@0: NewTraceBuffer( bufSize ); andre@0: PR_Unlock( traceLock ); andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceBufSize: %ld", bufSize)); andre@0: break; andre@0: andre@0: case PRTraceEnable : andre@0: rnp = *(RName **)value; andre@0: rnp->state = Running; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceEnable: %p", rnp)); andre@0: break; andre@0: andre@0: case PRTraceDisable : andre@0: rnp = *(RName **)value; andre@0: rnp->state = Suspended; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceDisable: %p", rnp)); andre@0: break; andre@0: andre@0: case PRTraceSuspend : andre@0: traceState = Suspended; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceSuspend")); andre@0: break; andre@0: andre@0: case PRTraceResume : andre@0: traceState = Running; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceResume")); andre@0: break; andre@0: andre@0: case PRTraceSuspendRecording : andre@0: PR_Lock( logLock ); andre@0: logOrder = LogSuspend; andre@0: PR_NotifyCondVar( logCVar ); andre@0: PR_Unlock( logLock ); andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceSuspendRecording")); andre@0: break; andre@0: andre@0: case PRTraceResumeRecording : andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceResumeRecording")); andre@0: if ( logState != LogSuspend ) andre@0: break; andre@0: PR_Lock( logLock ); andre@0: logOrder = LogResume; andre@0: PR_NotifyCondVar( logCVar ); andre@0: PR_Unlock( logLock ); andre@0: break; andre@0: andre@0: case PRTraceStopRecording : andre@0: PR_Lock( logLock ); andre@0: logOrder = LogStop; andre@0: PR_NotifyCondVar( logCVar ); andre@0: PR_Unlock( logLock ); andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceStopRecording")); andre@0: break; andre@0: andre@0: case PRTraceLockHandles : andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceLockTraceHandles")); andre@0: PR_Lock( traceLock ); andre@0: break; andre@0: andre@0: case PRTraceUnLockHandles : andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRSetTraceOption: PRTraceUnLockHandles")); andre@0: PR_Unlock( traceLock ); andre@0: break; andre@0: andre@0: default: andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PRSetTraceOption: Invalid command %ld", command )); andre@0: PR_ASSERT( 0 ); andre@0: break; andre@0: } /* end switch() */ andre@0: return; andre@0: } /* end PR_SetTraceOption() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(void) andre@0: PR_GetTraceOption( andre@0: PRTraceOption command, /* One of the enumerated values */ andre@0: void *value /* command value or NULL */ andre@0: ) andre@0: { andre@0: switch ( command ) andre@0: { andre@0: case PRTraceBufSize : andre@0: *((PRInt32 *)value) = bufSize; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PRGetTraceOption: PRTraceBufSize: %ld", bufSize )); andre@0: break; andre@0: andre@0: default: andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PRGetTraceOption: Invalid command %ld", command )); andre@0: PR_ASSERT( 0 ); andre@0: break; andre@0: } /* end switch() */ andre@0: return; andre@0: } /* end PR_GetTraceOption() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(PRTraceHandle) andre@0: PR_GetTraceHandleFromName( andre@0: const char *qName, /* QName search argument */ andre@0: const char *rName /* RName search argument */ andre@0: ) andre@0: { andre@0: const char *qn, *rn, *desc; andre@0: PRTraceHandle qh, rh = NULL; andre@0: RName *rnp = NULL; andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: GetTraceHandleFromName:\n\t" andre@0: "QName: %s, RName: %s", qName, rName )); andre@0: andre@0: qh = PR_FindNextTraceQname( NULL ); andre@0: while (qh != NULL) andre@0: { andre@0: rh = PR_FindNextTraceRname( NULL, qh ); andre@0: while ( rh != NULL ) andre@0: { andre@0: PR_GetTraceNameFromHandle( rh, &qn, &rn, &desc ); andre@0: if ( (strcmp( qName, qn ) == 0) andre@0: && (strcmp( rName, rn ) == 0 )) andre@0: { andre@0: rnp = (RName *)rh; andre@0: goto foundIt; andre@0: } andre@0: rh = PR_FindNextTraceRname( rh, qh ); andre@0: } andre@0: qh = PR_FindNextTraceQname( NULL ); andre@0: } andre@0: andre@0: foundIt: andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PR_Counter: GetConterHandleFromName: %p", rnp )); andre@0: return(rh); andre@0: } /* end PR_GetTraceHandleFromName() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(void) andre@0: PR_GetTraceNameFromHandle( andre@0: PRTraceHandle handle, /* handle as search argument */ andre@0: const char **qName, /* pointer to associated QName */ andre@0: const char **rName, /* pointer to associated RName */ andre@0: const char **description /* pointer to associated description */ andre@0: ) andre@0: { andre@0: RName *rnp = (RName *)handle; andre@0: QName *qnp = rnp->qName; andre@0: andre@0: *qName = qnp->name; andre@0: *rName = rnp->name; andre@0: *description = rnp->desc; andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: GetConterNameFromHandle: " andre@0: "QNp: %p, RNp: %p,\n\tQName: %s, RName: %s, Desc: %s", andre@0: qnp, rnp, qnp->name, rnp->name, rnp->desc )); andre@0: andre@0: return; andre@0: } /* end PR_GetTraceNameFromHandle() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(PRTraceHandle) andre@0: PR_FindNextTraceQname( andre@0: PRTraceHandle handle andre@0: ) andre@0: { andre@0: QName *qnp = (QName *)handle; andre@0: andre@0: if ( PR_CLIST_IS_EMPTY( &qNameList )) andre@0: qnp = NULL; andre@0: else if ( qnp == NULL ) andre@0: qnp = (QName *)PR_LIST_HEAD( &qNameList ); andre@0: else if ( PR_NEXT_LINK( &qnp->link ) == &qNameList ) andre@0: qnp = NULL; andre@0: else andre@0: qnp = (QName *)PR_NEXT_LINK( &qnp->link ); andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: FindNextQname: Handle: %p, Returns: %p", andre@0: handle, qnp )); andre@0: andre@0: return((PRTraceHandle)qnp); andre@0: } /* end PR_FindNextTraceQname() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(PRTraceHandle) andre@0: PR_FindNextTraceRname( andre@0: PRTraceHandle rhandle, andre@0: PRTraceHandle qhandle andre@0: ) andre@0: { andre@0: RName *rnp = (RName *)rhandle; andre@0: QName *qnp = (QName *)qhandle; andre@0: andre@0: andre@0: if ( PR_CLIST_IS_EMPTY( &qnp->rNameList )) andre@0: rnp = NULL; andre@0: else if ( rnp == NULL ) andre@0: rnp = (RName *)PR_LIST_HEAD( &qnp->rNameList ); andre@0: else if ( PR_NEXT_LINK( &rnp->link ) == &qnp->rNameList ) andre@0: rnp = NULL; andre@0: else andre@0: rnp = (RName *)PR_NEXT_LINK( &rnp->link ); andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, ("PRTrace: FindNextRname: Rhandle: %p, QHandle: %p, Returns: %p", andre@0: rhandle, qhandle, rnp )); andre@0: andre@0: return((PRTraceHandle)rnp); andre@0: } /* end PR_FindNextTraceRname() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: static PRFileDesc * InitializeRecording( void ) andre@0: { andre@0: char *logFileName; andre@0: PRFileDesc *logFile; andre@0: andre@0: /* Self initialize, if necessary */ andre@0: if ( traceLock == NULL ) andre@0: _PR_InitializeTrace(); andre@0: andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PR_RecordTraceEntries: begins")); andre@0: andre@0: logLostData = 0; /* reset at entry */ andre@0: logState = LogReset; andre@0: andre@0: #ifdef XP_UNIX andre@0: if ((getuid() != geteuid()) || (getgid() != getegid())) { andre@0: return NULL; andre@0: } andre@0: #endif /* XP_UNIX */ andre@0: andre@0: /* Get the filename for the logfile from the environment */ andre@0: logFileName = PR_GetEnv( "NSPR_TRACE_LOG" ); andre@0: if ( logFileName == NULL ) andre@0: { andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("RecordTraceEntries: Environment variable not defined. Exiting")); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Open the logfile */ andre@0: logFile = PR_Open( logFileName, PR_WRONLY | PR_CREATE_FILE, 0666 ); andre@0: if ( logFile == NULL ) andre@0: { andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("RecordTraceEntries: Cannot open %s as trace log file. OS error: %ld", andre@0: logFileName, PR_GetOSError())); andre@0: return NULL; andre@0: } andre@0: return logFile; andre@0: } /* end InitializeRecording() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: static void ProcessOrders( void ) andre@0: { andre@0: switch ( logOrder ) andre@0: { andre@0: case LogReset : andre@0: logOrder = logState = localState; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: LogReset")); andre@0: break; andre@0: andre@0: case LogSuspend : andre@0: localState = logOrder = logState = LogSuspend; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: LogSuspend")); andre@0: break; andre@0: andre@0: case LogResume : andre@0: localState = logOrder = logState = LogActive; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: LogResume")); andre@0: break; andre@0: andre@0: case LogStop : andre@0: logOrder = logState = LogStop; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: LogStop")); andre@0: break; andre@0: andre@0: default : andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("RecordTraceEntries: Invalid logOrder: %ld", logOrder )); andre@0: PR_ASSERT( 0 ); andre@0: break; andre@0: } /* end switch() */ andre@0: return ; andre@0: } /* end ProcessOrders() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: static void WriteTraceSegment( PRFileDesc *logFile, void *buf, PRInt32 amount ) andre@0: { andre@0: PRInt32 rc; andre@0: andre@0: andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("WriteTraceSegment: Buffer: %p, Amount: %ld", buf, amount)); andre@0: rc = PR_Write( logFile, buf , amount ); andre@0: if ( rc == -1 ) andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("RecordTraceEntries: PR_Write() failed. Error: %ld", PR_GetError() )); andre@0: else if ( rc != amount ) andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("RecordTraceEntries: PR_Write() Tried to write: %ld, Wrote: %ld", amount, rc)); andre@0: else andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: PR_Write(): Buffer: %p, bytes: %ld", buf, amount)); andre@0: andre@0: return; andre@0: } /* end WriteTraceSegment() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(void) andre@0: PR_RecordTraceEntries( andre@0: void andre@0: ) andre@0: { andre@0: PRFileDesc *logFile; andre@0: PRInt32 lostSegments; andre@0: PRInt32 currentSegment = 0; andre@0: void *buf; andre@0: PRBool doWrite; andre@0: andre@0: logFile = InitializeRecording(); andre@0: if ( logFile == NULL ) andre@0: { andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PR_RecordTraceEntries: Failed to initialize")); andre@0: return; andre@0: } andre@0: andre@0: /* Do this until told to stop */ andre@0: while ( logState != LogStop ) andre@0: { andre@0: andre@0: PR_Lock( logLock ); andre@0: andre@0: while ( (logCount == 0) && ( logOrder == logState ) ) andre@0: PR_WaitCondVar( logCVar, PR_INTERVAL_NO_TIMEOUT ); andre@0: andre@0: /* Handle state transitions */ andre@0: if ( logOrder != logState ) andre@0: ProcessOrders(); andre@0: andre@0: /* recalculate local controls */ andre@0: if ( logCount ) andre@0: { andre@0: lostSegments = logCount - logSegments; andre@0: if ( lostSegments > 0 ) andre@0: { andre@0: logLostData += ( logCount - logSegments ); andre@0: logCount = (logCount % logSegments); andre@0: currentSegment = logCount; andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("PR_RecordTraceEntries: LostData segments: %ld", logLostData)); andre@0: } andre@0: else andre@0: { andre@0: logCount--; andre@0: } andre@0: andre@0: buf = tBuf + ( logEntriesPerSegment * currentSegment ); andre@0: if (++currentSegment >= logSegments ) andre@0: currentSegment = 0; andre@0: doWrite = PR_TRUE; andre@0: } andre@0: else andre@0: doWrite = PR_FALSE; andre@0: andre@0: PR_Unlock( logLock ); andre@0: andre@0: if ( doWrite == PR_TRUE ) andre@0: { andre@0: if ( localState != LogSuspend ) andre@0: WriteTraceSegment( logFile, buf, logSegSize ); andre@0: else andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: PR_Write(): is suspended" )); andre@0: } andre@0: andre@0: } /* end while(logState...) */ andre@0: andre@0: PR_Close( logFile ); andre@0: PR_LOG( lm, PR_LOG_DEBUG, andre@0: ("RecordTraceEntries: exiting")); andre@0: return; andre@0: } /* end PR_RecordTraceEntries() */ andre@0: andre@0: /* andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(PRIntn) andre@0: PR_GetTraceEntries( andre@0: PRTraceEntry *buffer, /* where to write output */ andre@0: PRInt32 count, /* number to get */ andre@0: PRInt32 *found /* number you got */ andre@0: ) andre@0: { andre@0: PRInt32 rc; andre@0: PRInt32 copied = 0; andre@0: andre@0: PR_Lock( traceLock ); andre@0: andre@0: /* andre@0: ** Depending on where the LastSeen and Next indices are, andre@0: ** copy the trace buffer in one or two pieces. andre@0: */ andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PR_GetTraceEntries: Next: %ld, LastSeen: %ld", next, fetchLastSeen)); andre@0: andre@0: if ( fetchLastSeen <= next ) andre@0: { andre@0: while (( count-- > 0 ) && (fetchLastSeen < next )) andre@0: { andre@0: *(buffer + copied++) = *(tBuf + fetchLastSeen++); andre@0: } andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PR_GetTraceEntries: Copied: %ld, LastSeen: %ld", copied, fetchLastSeen)); andre@0: } andre@0: else /* copy in 2 parts */ andre@0: { andre@0: while ( count-- > 0 && fetchLastSeen <= last ) andre@0: { andre@0: *(buffer + copied++) = *(tBuf + fetchLastSeen++); andre@0: } andre@0: fetchLastSeen = 0; andre@0: andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PR_GetTraceEntries: Copied: %ld, LastSeen: %ld", copied, fetchLastSeen)); andre@0: andre@0: while ( count-- > 0 && fetchLastSeen < next ) andre@0: { andre@0: *(buffer + copied++) = *(tBuf + fetchLastSeen++); andre@0: } andre@0: PR_LOG( lm, PR_LOG_ERROR, andre@0: ("PR_GetTraceEntries: Copied: %ld, LastSeen: %ld", copied, fetchLastSeen)); andre@0: } andre@0: andre@0: *found = copied; andre@0: rc = ( fetchLostData == PR_TRUE )? 1 : 0; andre@0: fetchLostData = PR_FALSE; andre@0: andre@0: PR_Unlock( traceLock ); andre@0: return rc; andre@0: } /* end PR_GetTraceEntries() */ andre@0: andre@0: /* end prtrace.c */