SPARK-2151 Dump shared memory on bad handle for forensic analysis

This commit is contained in:
Eric Harding committed 2019-05-03 17:39:47 -04:00
1 parent 102fefbd92
commit 9f406e83aa
7 files changed
+86 -7

No files matched your search

@@ -67,6 +67,10 @@ namespace mbts.co2
[DllImport("libspark")]
internal unsafe static extern void md_consumer_get_system_md_symbols(raw_usersymbol_callback cb);
[DllImport("libspark")]
internal unsafe static extern void dump_co2_memory_to_disk(IntPtr consumer, string path);
//
// Relay control
+9 -3
View File
@@ -37,7 +37,10 @@ namespace mbts.co2
unsafe void init() {
_raw_sec_cb = (key, buf, len, handle) => {
var security = Co2NativeMethods.deserialize<Security>(buf, len);
var next = new SecurityAndHandle() { security = security, handle = new MDConsumerHandle(handle) };
var next = new SecurityAndHandle() {
security = security,
handle = new MDConsumerHandle(_consumer, security, handle)
};
_securities.Add(next);
_on_security?.Invoke(new List<SecurityAndHandle>(1) { next });
};
@@ -67,7 +70,7 @@ namespace mbts.co2
public MDConsumerHandle try_get_handle(security_key key) {
Co2NativeMethods.BlitPointerTuple raw;
if (Co2NativeMethods.md_consumer_get_handle(_consumer, key, out raw)) {
return new MDConsumerHandle(raw);
return new MDConsumerHandle(this._consumer, null, raw);
}
return null;
}
@@ -77,7 +80,10 @@ namespace mbts.co2
List<SecurityAndHandle> result = new List<SecurityAndHandle>();
Co2NativeMethods.raw_security_callback cb = (key, buf, len, handle) => {
var security = Co2NativeMethods.deserialize<Security>(buf, len);
result.Add(new SecurityAndHandle() { security = new Security(security), handle = new MDConsumerHandle(handle) });
result.Add(
new SecurityAndHandle() {
security = new Security(security),
handle = new MDConsumerHandle(_consumer, security, handle) });
};
Co2NativeMethods.md_consumer_walk_security_list(_consumer, cb);
return result;
+11 -2
View File
@@ -156,13 +156,22 @@ namespace mbts.co2 {
//
// Internal
internal MDConsumerHandle(Co2NativeMethods.BlitPointerTuple tuple) {
internal MDConsumerHandle(
IntPtr consumer,
Security security,
Co2NativeMethods.BlitPointerTuple tuple) {
_header = (ColdHeader*)tuple.cold_header.ToPointer();
_cold = (ColdEntry*)tuple.cold_data.ToPointer();
if (has_depth) {
_hot_depth = (HotDepthEntry*)tuple.hot.ToPointer();
Log.assert(_hot_depth->key == _cold->security_key);
if (_hot_depth->key != _cold->security_key) {
var temp_path = Path.Combine(Path.GetTempPath(), "btsshm");
Co2NativeMethods.dump_co2_memory_to_disk(consumer, temp_path);
Log.assert(
_hot_depth->key == _cold->security_key,
$"Please attach the contents of '${temp_path}' to the bug report you are about to file. security:{security?.Id} cold:{_cold->security_key} hot:{_hot->key}");
}
}
else {
_hot = (HotEntry*)tuple.hot.ToPointer();
+19 -1
View File
@@ -269,6 +269,16 @@ public:
if (!_cold.is_initialized()) return true;
return is_heart_beat_ok(cold_header());
}
void dump_shm_to(const char* directory) const {
_cold.dump_to(directory);
if (_hot) {
_hot->dump_to(directory);
}
if (_hot_depth) {
_hot_depth->dump_to(directory);
}
}
private:
@@ -533,7 +543,7 @@ private:
///////////////////////////////////////////////////////////////////////////////////////////////////
class MDConsumerImpl {
typedef tbb::queuing_mutex Mutex;
Mutex _workmutex;
mutable Mutex _workmutex;
vector<shared_ptr<MDConsumerInst>> _instances;
std::shared_ptr<SyntheticConsumerInst> _sci;
MBOReader* _mbo;
@@ -910,6 +920,12 @@ public:
return result;
}
void dump_shm_to(const char* p) const {
//Mutex::scoped_lock lock(_workmutex);
for (auto& inst : _instances) {
inst->dump_shm_to(p);
}
}
};
@@ -957,6 +973,8 @@ MDConsumerChannel MDConsumer::get_channel(const std::string& symbol, SecurityTyp
MDConsumerChannel MDConsumer::get_channel(const UserSymbol& u) const {
return _pimpl->get_channel(u.symbol(), u.type(), u.exchange());
}
void MDConsumer::dump_shm_to(const char* p) const { _pimpl->dump_shm_to(p); }
MDConsumerChannel::MDConsumerChannel(const shared_ptr<MDConsumerInst>& inst) : _pimpl(inst) {}
int64_t MDConsumerChannel::read_trades(google::protobuf::RepeatedPtrField<co2::TradeMessage>& trades, int64_t cursor) { return _pimpl->read_trades(trades, cursor); }
+2
View File
@@ -67,6 +67,8 @@ public:
}
uintptr_t id() const { return reinterpret_cast<uintptr_t>(_pimpl.get()); }
void dump_shm_to(const char* directory) const;
private:
//BTS_NO_COPY(MDConsumer); COPY OK
std::shared_ptr<MDConsumerImpl> _pimpl;
+6
View File
@@ -177,5 +177,11 @@ CAPI void post_on_scan_security_thread(just_action a) {
md::scan_securities_thread().post(a);
}
CAPI void dump_co2_memory_to_disk(MDConsumer* consumer, const char* path) {
if (consumer) {
consumer->dump_shm_to(path);
}
}
}} //namespace
#endif
+35 -1
View File
@@ -15,8 +15,10 @@
#include <utility>
#include <thread>
#include <atomic>
#include <iostream>
#include <tbb/queuing_mutex.h>
#include <boost/filesystem.hpp>
namespace bts {
namespace utils {
@@ -238,6 +240,38 @@ public:
offset = sizeof(SegmentHeader);
}
}
void dump_to(const char* directory) const {
using namespace std;
boost::system::error_code _;
if (!boost::filesystem::exists(directory, _)) {
boost::filesystem::create_directories(directory, _);
}
if (!boost::filesystem::is_directory(directory)) {
LOG(md,Error, "Error dumping memory to {}", std::string(directory));
return;
}
boost::filesystem::path root(directory);
// OK to hold the lock for a while as we're typically crashing when this is called
tbb::queuing_mutex::scoped_lock lock(_mutex);
for (size_t x = 0; x < _segments.size(); ++x) {
char segment_name[512];
SharedList<THeader, TEntry>::get_segment_name(segment_name, BTS_ARRAYSIZE(segment_name), _name, x);
// create/overwrite output file
std::ofstream writer( (root/segment_name).string(), ios::out | ios::app | ios::binary);
if (writer.is_open()) {
// writer just loves signed types
writer.write(
(const char*)_segments[x].address,
(std::streamsize)_segments[x].size);
}
}
}
private:
BTS_NO_COPY(SharedListReader);
@@ -281,7 +315,7 @@ private:
const std::string _name;
THeader* _header; // null value indicates no memory segment
tbb::queuing_mutex _mutex;
mutable tbb::queuing_mutex _mutex;
std::unique_ptr<LowLevelSharedMemory> _shm;
std::vector<Segment> _segments;
};