File: StackTrace.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (151 lines) | stat: -rw-r--r-- 3,608 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "stdafx.h"
#include "StackTrace.h"
#include "StrBuf.h"
#include "Convert.h"
#include "Utils/StackInfoSet.h"

namespace storm {

	StackTrace::StackTrace(Engine &e) : frames(new (e) Array<Frame>()) {}

	StackTrace::Frame::Frame(void *base, Nat offset, Nat id) : base(base), offset(offset), id(id) {}

	void StackTrace::deepCopy(CloneEnv *env) {
		frames = new (env) Array<Frame>(*frames);
		// No need to call 'deepCopy'.
	}

	class StrBufOut : public GenericOutput {
	public:
		StrBufOut(StrBuf *to) : to(to), frameNumber(0) {}

		virtual void put(const wchar *str) { *to << str; }
		virtual void put(const char *str) { *to << toWChar(to->engine(), str)->v; }
		virtual void put(size_t i) { *to << i; }
		virtual void putHex(size_t i) { *to << hex(i); }

		virtual void nextFrame() {
			if (frameNumber > 0)
				*to << S("\n");
			*to << width(3) << (frameNumber++) << S(": ");
		}
	private:
		StrBuf *to;
		Nat frameNumber;
	};

	void StackTrace::format(StrBuf *to) const {
		StackInfoSet &s = stackInfo();
		StrBufOut adapter(to);

		for (Nat i = 0; i < frames->count(); i++) {
			adapter.nextFrame();

			const Frame &f = frames->at(i);
			if (f.id == Frame::nextThread()) {
				*to << S("-- spawned from thread 0x") << hex(f.ptr()) << S(" --");
			} else {
				s.format(adapter, f.id, f.base, f.offset);
			}
		}
	}

	Str *StackTrace::format() const {
		StrBuf *out = new (frames) StrBuf();
		format(out);
		return out->toS();
	}

	wostream &operator <<(wostream &to, const StackTrace &trace) {
		for (Nat i = 0; i < trace.count(); i++) {
			if (i > 0)
				to << std::endl;
			to << std::setw(3) << i << L": ";
			if (trace[i].id == StackTrace::Frame::nextThread())
				to << L"-- spawned from thread 0x" << trace[i].ptr() << L" --";
			else
				to << L"at 0x" << trace[i].ptr();
		}
		return to;
	}

	void StackTrace::toS(StrBuf *to) const {
		for (Nat i = 0; i < count(); i++) {
			if (i > 0)
				*to << S("\n");
			*to << width(3) << i << S(": ");
			const Frame &frame = frames->at(i);
			if (frame.id == Frame::nextThread())
				*to << S("-- spawned from thread 0x") << hex(frame.ptr()) << S(" --");
			else
				*to << S("at 0x") << hex(frame.ptr());
		}
	}

	class StormGen : public TraceGen {
	public:
		StackTrace &result;

		StormGen(StackTrace &result) : result(result) {}

		void init(size_t count) {
			if (count > 0)
				result.reserve(Nat(count));
		}

		void put(const StackFrame &frame) {
			result.push(StackTrace::Frame(frame.fnBase, frame.offset, frame.id));
		}
	};

	StackTrace collectStackTrace(EnginePtr e) {
		return collectStackTrace(e, (void *)null);
	}

	StackTrace collectStackTrace(EnginePtr e, Nat ignore) {
		StackTrace result(e.v);
		StormGen gen(result);

		createStackTrace(gen, ignore, null);

		return result;
	}

	StackTrace collectStackTrace(EnginePtr e, void *state) {
		StackTrace result(e.v);
		StormGen gen(result);

		createStackTrace(gen, 0, state);

		return result;
	}

	class StormAppendGen : public StormGen {
	public:
		Nat original;

		StormAppendGen(StackTrace &to) : StormGen(to), original(to.count()) {}

		void init(size_t count) {
			if (count > 0)
				result.reserve(Nat(original + count));
		}

		void put(const StackFrame &frame) {
			// Overwrite the first stack frame.
			if (result.count() == original) {
				result.push(StackTrace::Frame(os::UThread::current().threadData(),
														0,
														StackTrace::Frame::nextThread()));
			} else {
				StormGen::put(frame);
			}
		}
	};

	void appendStackTrace(StackTrace &to, Nat ignore) {
		StormAppendGen gen(to);
		createStackTrace(gen, max(Nat(1), ignore) - 1, null);
	}

}