8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-30 20:03:03 +01:00
firebird-mirror/src/jrd/trace/TraceManager.cpp

479 lines
12 KiB
C++
Raw Normal View History

2009-02-01 23:07:35 +01:00
/*
* PROGRAM: JRD Access Method
* MODULE: TraceManager.cpp
* DESCRIPTION: Trace API manager
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2008 Khorsun Vladyslav
*/
2009-04-04 18:39:31 +02:00
#include "firebird.h"
2009-02-01 23:07:35 +01:00
#include "../../jrd/trace/TraceManager.h"
#include "../../jrd/trace/TraceObjects.h"
2010-10-12 10:02:57 +02:00
#include "../../common/os/path_utils.h"
2010-10-12 19:40:27 +02:00
#include "../../common/ScanDir.h"
#include "../../common/isc_proto.h"
#include "../../common/classes/GetPlugins.h"
2009-02-04 12:43:19 +01:00
2009-02-01 23:07:35 +01:00
#ifdef WIN_NT
2009-04-04 18:39:31 +02:00
#include <process.h>
2009-02-01 23:07:35 +01:00
#endif
using namespace Firebird;
2009-02-02 15:46:24 +01:00
namespace
{
static const char* const NTRACE_PREFIX = "fbtrace";
2009-02-01 23:07:35 +01:00
}
namespace Jrd {
GlobalPtr<StorageInstance, InstanceControl::PRIORITY_DELETE_FIRST> TraceManager::storageInstance;
TraceManager::Factories* TraceManager::factories = NULL;
GlobalPtr<Mutex> TraceManager::init_factories_mtx;
volatile bool TraceManager::init_factories;
2009-02-01 23:07:35 +01:00
2014-09-29 13:03:47 +02:00
bool TraceManager::check_result(ITracePlugin* plugin, const char* module, const char* function,
bool result)
2009-02-01 23:07:35 +01:00
{
if (result)
return true;
2009-04-04 18:39:31 +02:00
if (!plugin)
2009-02-01 23:07:35 +01:00
{
gds__log("Trace plugin %s returned error on call %s, "
"did not create plugin and provided no additional details on reasons of failure",
module, function);
2009-02-01 23:07:35 +01:00
return false;
}
const char* errorStr = plugin->trace_get_error();
2009-02-01 23:07:35 +01:00
if (!errorStr)
{
gds__log("Trace plugin %s returned error on call %s, "
"but provided no additional details on reasons of failure", module, function);
return false;
}
2009-04-04 18:39:31 +02:00
gds__log("Trace plugin %s returned error on call %s.\n\tError details: %s",
2009-02-01 23:07:35 +01:00
module, function, errorStr);
return false;
}
2009-04-04 18:39:31 +02:00
TraceManager::TraceManager(Attachment* in_att) :
attachment(in_att),
2009-02-01 23:07:35 +01:00
service(NULL),
filename(NULL),
trace_sessions(*in_att->att_pool)
2009-02-01 23:07:35 +01:00
{
init();
2009-02-01 23:07:35 +01:00
}
2009-04-04 18:39:31 +02:00
TraceManager::TraceManager(Service* in_svc) :
2009-02-01 23:07:35 +01:00
attachment(NULL),
service(in_svc),
2009-02-01 23:07:35 +01:00
filename(NULL),
trace_sessions(in_svc->getPool())
2009-02-01 23:07:35 +01:00
{
init();
2009-02-01 23:07:35 +01:00
}
2009-04-04 18:39:31 +02:00
TraceManager::TraceManager(const char* in_filename) :
2009-02-01 23:07:35 +01:00
attachment(NULL),
service(NULL),
filename(in_filename),
2009-02-01 23:07:35 +01:00
trace_sessions(*getDefaultMemoryPool())
{
init();
2009-02-01 23:07:35 +01:00
}
2009-04-04 18:39:31 +02:00
TraceManager::~TraceManager()
2009-02-01 23:07:35 +01:00
{
}
void TraceManager::init()
{
// ensure storage is initialized
2009-02-13 22:08:56 +01:00
getStorage();
load_plugins();
changeNumber = 0;
}
void TraceManager::load_plugins()
2009-02-01 23:07:35 +01:00
{
// Initialize all trace needs to false
trace_needs = 0;
2009-02-01 23:07:35 +01:00
if (init_factories)
2009-02-01 23:07:35 +01:00
return;
MutexLockGuard guard(init_factories_mtx, FB_FUNCTION);
if (init_factories)
2009-02-01 23:07:35 +01:00
return;
init_factories = true;
2009-02-04 12:43:19 +01:00
factories = FB_NEW(*getDefaultMemoryPool()) TraceManager::Factories(*getDefaultMemoryPool());
for (GetPlugins<ITraceFactory> traceItr(IPluginManager::TYPE_TRACE); traceItr.hasData(); traceItr.next())
2009-02-01 23:07:35 +01:00
{
FactoryInfo info;
info.factory = traceItr.plugin();
info.factory->addRef();
string name(traceItr.name());
name.copyTo(info.name, sizeof(info.name));
factories->add(info);
2009-02-01 23:07:35 +01:00
}
}
void TraceManager::shutdown()
{
if (init_factories)
{
MutexLockGuard guard(init_factories_mtx, FB_FUNCTION);
if (init_factories)
{
delete factories;
factories = NULL;
init_factories = false;
}
}
}
2009-04-04 18:39:31 +02:00
void TraceManager::update_sessions()
2009-02-01 23:07:35 +01:00
{
SortedArray<ULONG> liveSessions(*getDefaultMemoryPool());
2009-02-02 15:46:24 +01:00
{ // scope
ConfigStorage* storage = getStorage();
2009-02-01 23:07:35 +01:00
StorageGuard guard(storage);
storage->restart();
TraceSession session(*getDefaultMemoryPool());
2009-04-04 18:39:31 +02:00
while (storage->getNextSession(session))
2009-02-01 23:07:35 +01:00
{
2009-04-04 18:39:31 +02:00
if ((session.ses_flags & trs_active) && !(session.ses_flags & trs_log_full))
2009-02-01 23:07:35 +01:00
{
update_session(session);
liveSessions.add(session.ses_id);
}
}
changeNumber = storage->getChangeNumber();
2009-02-01 23:07:35 +01:00
}
// remove sessions not present in storage
2014-07-17 20:48:46 +02:00
FB_SIZE_T i = 0;
2009-02-01 23:07:35 +01:00
while (i < trace_sessions.getCount())
{
2014-07-17 20:48:46 +02:00
FB_SIZE_T pos;
2009-02-01 23:07:35 +01:00
if (liveSessions.find(trace_sessions[i].ses_id, pos)) {
i++;
}
else
{
trace_sessions[i].plugin->release();
2009-02-01 23:07:35 +01:00
trace_sessions.remove(i);
}
}
// nothing to trace, clear needs
if (trace_sessions.getCount() == 0)
{
trace_needs = 0;
}
2009-02-01 23:07:35 +01:00
}
2009-02-02 15:46:24 +01:00
void TraceManager::update_session(const TraceSession& session)
2009-02-01 23:07:35 +01:00
{
// if this session is already known, nothing to do
2014-07-17 20:48:46 +02:00
FB_SIZE_T pos;
2009-02-01 23:07:35 +01:00
if (trace_sessions.find(session.ses_id, pos)) {
return;
}
2009-02-02 15:46:24 +01:00
// if this session is not from administrator, it may trace connections
2009-02-01 23:07:35 +01:00
// only created by the same user
if (!(session.ses_flags & trs_admin))
{
2009-02-02 15:46:24 +01:00
if (attachment)
{
2009-02-01 23:07:35 +01:00
if (!attachment->att_user || attachment->att_user->usr_user_name != session.ses_user)
return;
}
2009-02-02 15:46:24 +01:00
else if (service)
{
2009-02-01 23:07:35 +01:00
if (session.ses_user != service->getUserName())
return;
}
2009-02-02 15:46:24 +01:00
else
{
2009-02-01 23:07:35 +01:00
// failed attachment attempts traced by admin trace only
return;
}
}
MasterInterfacePtr master;
2011-01-16 03:16:15 +01:00
for (FactoryInfo* info = factories->begin(); info != factories->end(); ++info)
2009-02-01 23:07:35 +01:00
{
TraceInitInfoImpl attachInfo(session, attachment, filename);
LocalStatus status;
2014-09-29 13:03:47 +02:00
ITracePlugin* plugin = info->factory->trace_create(&status, &attachInfo);
2009-02-01 23:07:35 +01:00
if (plugin)
{
plugin->addRef();
2009-02-01 23:07:35 +01:00
SessionInfo sesInfo;
sesInfo.plugin = plugin;
sesInfo.factory_info = info;
2009-02-01 23:07:35 +01:00
sesInfo.ses_id = session.ses_id;
trace_sessions.add(sesInfo);
trace_needs |= info->factory->trace_needs();
}
else if (status.getState() & IStatus::STATE_ERRORS)
{
string header;
header.printf("Trace plugin %s returned error on call trace_create.", info->name);
iscLogStatus(header.c_str(), &status);
2009-02-01 23:07:35 +01:00
}
}
}
2009-02-02 15:46:24 +01:00
bool TraceManager::need_dsql_prepare(Attachment* att)
{
return att->att_trace_manager->needs(ITraceFactory::TRACE_EVENT_DSQL_PREPARE);
2009-02-01 23:07:35 +01:00
}
2009-02-02 15:46:24 +01:00
bool TraceManager::need_dsql_free(Attachment* att)
{
return att->att_trace_manager->needs(ITraceFactory::TRACE_EVENT_DSQL_FREE);
2009-02-01 23:07:35 +01:00
}
2009-02-02 15:46:24 +01:00
bool TraceManager::need_dsql_execute(Attachment* att)
{
return att->att_trace_manager->needs(ITraceFactory::TRACE_EVENT_DSQL_EXECUTE);
2009-02-01 23:07:35 +01:00
}
2009-02-02 15:46:24 +01:00
void TraceManager::event_dsql_prepare(Attachment* att, jrd_tra* transaction,
2014-09-29 13:03:47 +02:00
ITraceSQLStatement* statement,
2009-02-01 23:07:35 +01:00
ntrace_counter_t time_millis, ntrace_result_t req_result)
{
TraceConnectionImpl conn(att);
TraceTransactionImpl tran(transaction);
att->att_trace_manager->event_dsql_prepare(&conn, transaction ? &tran : NULL, statement,
time_millis, req_result);
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_dsql_free(Attachment* att, ITraceSQLStatement* statement,
2009-02-01 23:07:35 +01:00
unsigned short option)
{
TraceConnectionImpl conn(att);
att->att_trace_manager->event_dsql_free(&conn, statement, option);
}
2009-04-04 18:39:31 +02:00
void TraceManager::event_dsql_execute(Attachment* att, jrd_tra* transaction,
2014-09-29 13:03:47 +02:00
ITraceSQLStatement* statement, bool started, ntrace_result_t req_result)
2009-02-01 23:07:35 +01:00
{
TraceConnectionImpl conn(att);
TraceTransactionImpl tran(transaction);
att->att_trace_manager->event_dsql_execute(&conn, &tran, statement, started, req_result);
}
#define EXECUTE_HOOKS(METHOD, PARAMS) \
2014-07-17 20:48:46 +02:00
FB_SIZE_T i = 0; \
2009-02-02 15:46:24 +01:00
while (i < trace_sessions.getCount()) \
2009-02-01 23:07:35 +01:00
{ \
SessionInfo* plug_info = &trace_sessions[i]; \
if (check_result(plug_info->plugin, plug_info->factory_info->name, #METHOD, \
plug_info->plugin->METHOD PARAMS)) \
2009-02-01 23:07:35 +01:00
{ \
i++; /* Move to next plugin */ \
} \
else { \
trace_sessions.remove(i); /* Remove broken plugin from the list */ \
} \
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_attach(ITraceDatabaseConnection* connection,
2009-02-01 23:07:35 +01:00
bool create_db, ntrace_result_t att_result)
{
EXECUTE_HOOKS(trace_attach,
(connection, create_db, att_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_detach(ITraceDatabaseConnection* connection, bool drop_db)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_detach, (connection, drop_db));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_transaction_start(ITraceDatabaseConnection* connection,
2014-09-30 21:38:35 +02:00
ITraceTransaction* transaction, unsigned tpb_length, const ntrace_byte_t* tpb,
2009-02-01 23:07:35 +01:00
ntrace_result_t tra_result)
{
EXECUTE_HOOKS(trace_transaction_start,
(connection, transaction, tpb_length, tpb, tra_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_transaction_end(ITraceDatabaseConnection* connection,
ITraceTransaction* transaction, bool commit, bool retain_context,
2009-02-01 23:07:35 +01:00
ntrace_result_t tra_result)
{
EXECUTE_HOOKS(trace_transaction_end,
(connection, transaction, commit, retain_context, tra_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_set_context(ITraceDatabaseConnection* connection,
ITraceTransaction* transaction, ITraceContextVariable* variable)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_set_context,
(connection, transaction, variable));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_proc_execute(ITraceDatabaseConnection* connection, ITraceTransaction* transaction,
ITraceProcedure* procedure, bool started, ntrace_result_t proc_result)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_proc_execute,
(connection, transaction, procedure, started, proc_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_func_execute(ITraceDatabaseConnection* connection, ITraceTransaction* transaction,
ITraceFunction* function, bool started, ntrace_result_t func_result)
{
EXECUTE_HOOKS(trace_func_execute,
(connection, transaction, function, started, func_result));
2014-09-29 13:03:47 +02:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_trigger_execute(ITraceDatabaseConnection* connection, ITraceTransaction* transaction,
ITraceTrigger* trigger, bool started, ntrace_result_t trig_result)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_trigger_execute,
(connection, transaction, trigger, started, trig_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_dsql_prepare(ITraceDatabaseConnection* connection, ITraceTransaction* transaction,
ITraceSQLStatement* statement, ntrace_counter_t time_millis, ntrace_result_t req_result)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_dsql_prepare,
(connection, transaction, statement,
2009-02-01 23:07:35 +01:00
time_millis, req_result));
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_dsql_free(ITraceDatabaseConnection* connection,
ITraceSQLStatement* statement, unsigned short option)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_dsql_free,
(connection, statement, option));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_dsql_execute(ITraceDatabaseConnection* connection, ITraceTransaction* transaction,
ITraceSQLStatement* statement, bool started, ntrace_result_t req_result)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_dsql_execute,
(connection, transaction, statement, started, req_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_blr_compile(ITraceDatabaseConnection* connection,
ITraceTransaction* transaction, ITraceBLRStatement* statement,
2009-02-01 23:07:35 +01:00
ntrace_counter_t time_millis, ntrace_result_t req_result)
{
EXECUTE_HOOKS(trace_blr_compile,
(connection, transaction, statement,
2009-02-01 23:07:35 +01:00
time_millis, req_result));
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_blr_execute(ITraceDatabaseConnection* connection,
ITraceTransaction* transaction, ITraceBLRStatement* statement,
2009-02-01 23:07:35 +01:00
ntrace_result_t req_result)
{
EXECUTE_HOOKS(trace_blr_execute,
(connection, transaction, statement, req_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_dyn_execute(ITraceDatabaseConnection* connection,
ITraceTransaction* transaction, ITraceDYNRequest* request,
2009-02-01 23:07:35 +01:00
ntrace_counter_t time_millis, ntrace_result_t req_result)
{
EXECUTE_HOOKS(trace_dyn_execute,
(connection, transaction, request, time_millis,
2009-02-01 23:07:35 +01:00
req_result));
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_service_attach(ITraceServiceConnection* service, ntrace_result_t att_result)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_service_attach,
(service, att_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_service_start(ITraceServiceConnection* service,
2014-09-30 21:38:35 +02:00
unsigned switches_length, const char* switches,
2009-02-01 23:07:35 +01:00
ntrace_result_t start_result)
{
EXECUTE_HOOKS(trace_service_start,
(service, switches_length, switches, start_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_service_query(ITraceServiceConnection* service,
2014-09-30 21:38:35 +02:00
unsigned send_item_length, const ntrace_byte_t* send_items,
unsigned recv_item_length, const ntrace_byte_t* recv_items,
2009-02-01 23:07:35 +01:00
ntrace_result_t query_result)
{
EXECUTE_HOOKS(trace_service_query,
(service, send_item_length, send_items,
2009-02-01 23:07:35 +01:00
recv_item_length, recv_items, query_result));
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_service_detach(ITraceServiceConnection* service, ntrace_result_t detach_result)
2009-02-01 23:07:35 +01:00
{
EXECUTE_HOOKS(trace_service_detach,
(service, detach_result));
2009-02-01 23:07:35 +01:00
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_error(ITraceConnection* connection, ITraceStatusVector* status, const char* function)
{
2012-05-26 20:05:56 +02:00
EXECUTE_HOOKS(trace_event_error,
(connection, status, function));
}
2014-09-29 13:03:47 +02:00
void TraceManager::event_sweep(ITraceDatabaseConnection* connection, ITraceSweepInfo* sweep,
ntrace_process_state_t sweep_state)
{
EXECUTE_HOOKS(trace_event_sweep,
(connection, sweep, sweep_state));
}
2009-02-01 23:07:35 +01:00
}