-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTrace.bas
More file actions
549 lines (511 loc) · 22 KB
/
StackTrace.bas
File metadata and controls
549 lines (511 loc) · 22 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
Attribute VB_Name = "StackTrace"
#Const DEBUG_MODE = 1
#Const DEBUG_PRINT_MODE = 0
#Const NO_TRACE = 1
Private StackLevel As Long
Private DebugTrace As Collection
Private Counter As Object
Sub WriteStackTrace()
Dim ws As Worksheet
Set ws = Sheet_DebugTrace
If Not DebugTrace Is Nothing Then
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim OutputRow As Long
OutputRow = 1
Do While Len(ws.Cells(OutputRow, 1).FormulaR1C1) > 0
OutputRow = OutputRow + 1
Loop
Dim v As Variant
For Each v In DebugTrace
Dim l As StackTraceLog
Set l = v
With ws
.Cells(OutputRow, 1) = l.Level
.Cells(OutputRow, 2) = l.modName
.Cells(OutputRow, 3) = l.procName
.Cells(OutputRow, 4) = l.argList
.Cells(OutputRow, 5) = l.retValue
' 数式
.Cells(OutputRow, 6).FormulaR1C1 = "=CONCAT(REPT(""|"",RC1-1),""+"",RC2,""."",RC3,""("",RC4,IF(RC5="""","")"","")=""),RC5)"
' ハイパーリンク
.Hyperlinks.Add .Cells(OutputRow, 6), "", .Cells(OutputRow, 6).AddressLocal
OutputRow = OutputRow + 1
End With
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
ws.Calculate
Set DebugTrace = Nothing
Set Counter = Nothing
StackLevel = 0
End If
End Sub
Sub PushStackTrace(modName As String, procName As String, ParamArray args() As Variant)
#If DEBUG_MODE Then
If DebugTrace Is Nothing Then Set DebugTrace = New Collection
If Counter Is Nothing Then Set Counter = CreateObject("Scripting.Dictionary")
StackLevel = StackLevel + 1
Dim argList As String
argList = ""
Dim i As Long
For i = LBound(args) To UBound(args) Step 2
If i > LBound(args) Then argList = argList & ", "
If (i + 1) <= UBound(args) Then
argList = argList & args(i) & ":=" & ArgsToString(args(i + 1))
Else
argList = argList & args(i)
End If
Next
Dim l As New StackTraceLog
With l
.Level = StackLevel
.modName = modName
.procName = procName
.argList = argList
End With
DebugTrace.Add l
#If DEBUG_PRINT_MODE Then
Debug.Print String(StackLevel, "|") & "+" & modName & "." & procName & "(" & argList & ")"
#End If
Dim keyName As String
keyName = modName & "." & procName
If Counter.Exists(keyName) Then
Counter.Item(keyName) = Counter.Item(keyName) + 1
If Counter.Item(keyName) = 10000 Then
Debug.Print "[StackTrace]" & keyName & "の呼出回数が10000回を超えました。"
MsgBox "[StackTrace]" & keyName & "の呼出回数が10000回を超えました。パフォーマンスの影響が大きいため当該プロシージャのデバッグコード削除をおすすめします。"
Stop
End If
Else
Counter.Add keyName, 1
End If
#End If
End Sub
Sub PopStackTrace(modName As String, procName As String, Optional returnValue As Variant)
#If DEBUG_MODE Then
Dim retValue As String
If Not IsMissing(returnValue) Then
retValue = ArgsToString(returnValue)
Dim iRow As Long
iRow = DebugTrace.Count
Dim l As StackTraceLog
Set l = DebugTrace(iRow)
Do While l.Level > StackLevel
iRow = iRow - 1
Set l = DebugTrace(iRow)
Loop
Dim canWriteResult As Boolean
canWriteResult = True
If l.Level <> StackLevel Then canWriteResult = False
If l.modName <> modName Then canWriteResult = False
If l.procName <> procName Then canWriteResult = False
If canWriteResult Then
l.retValue = retValue
End If
End If
StackLevel = StackLevel - 1
#End If
End Sub
Private Function ArgsToString(arg As Variant) As String
On Error Resume Next
Dim result As String
Dim argType As String
argType = TypeName(arg)
Select Case argType
Case "String"
result = """" & arg & """"
Case Else
result = CStr(arg)
End Select
If Err.Number <> 0 Then
Err.Clear
If Right(argType, 2) = "()" Then
argType = Replace(argType, "()", PrintArrayBounds(arg))
End If
result = Replace("[%]", "%", argType)
End If
On Error GoTo 0
ArgsToString = result
End Function
Private Function PrintArrayBounds(arg As Variant) As String
Dim i As Long
Dim res As String
Dim d As String
Err.Clear
On Error Resume Next
i = 0
d = ""
res = "("
Do While Err.Number = 0
i = i + 1
res = res & d & LBound(arg, i) & ".." & UBound(arg, i)
d = ","
Loop
res = res & ")"
Err.Clear
On Error GoTo 0
PrintArrayBounds = res
End Function
Sub EnableDEBUGMODE()
Dim vbEnv As Object
Dim vbComp As Variant
Dim vbMod As Object
Dim modName As String
' VBEの参照
Set vbEnv = Application.VBE
' すべてのVBComponentsを逐次調査する
For Each vbComp In vbEnv.ActiveVBProject.VBComponents
' Module名を取得
Set vbMod = vbComp.CodeModule
modName = vbMod.Name
Dim doAddST As Boolean
doAddST = True
If modName = "StackTraceLog" Then doAddST = False
If modName = "Sheet_DebugTrace" Then doAddST = False
If doAddST Then
If InStr(1, vbMod.Lines(1, 1), "#Const DEBUG_MODE") > 0 Then
vbMod.DeleteLines 1
End If
vbMod.InsertLines 1, "#Const DEBUG_MODE = 1"
End If
Next
End Sub
Sub DisableDEBUGMODE()
Dim vbEnv As Object
Dim vbComp As Variant
Dim vbMod As Object
Dim modName As String
' VBEの参照
Set vbEnv = Application.VBE
' すべてのVBComponentsを逐次調査する
For Each vbComp In vbEnv.ActiveVBProject.VBComponents
' Module名を取得
Set vbMod = vbComp.CodeModule
modName = vbMod.Name
Dim doAddST As Boolean
doAddST = True
If modName = "StackTraceLog" Then doAddST = False
If modName = "Sheet_DebugTrace" Then doAddST = False
If doAddST Then
If InStr(1, vbMod.Lines(1, 1), "#Const DEBUG_MODE") > 0 Then
vbMod.DeleteLines 1
End If
End If
Next
End Sub
Sub AddStackTrace()
Dim vbEnv As Object
Dim vbComp As Variant
Dim vbMod As Object
Dim modName As String
Dim procName As String
Dim lineNum As Long
Dim numLines As Long
Dim defineLineNum As Long
Dim startLineNum As Long
Dim endLineNum As Long
Dim procKind As Long
Dim exitSub As Collection
Dim i As Long
Dim shCodeName As String
shCodeName = "Sheet_DebugTrace"
' VBEの参照
Set vbEnv = Application.VBE
' すべてのVBComponentsを逐次調査する
For Each vbComp In vbEnv.ActiveVBProject.VBComponents
' Module名を取得
Set vbMod = vbComp.CodeModule
modName = vbMod.Name
Dim doAddST As Boolean
doAddST = True
If modName = "StackTrace" Then doAddST = False
If modName = "StackTraceLog" Then doAddST = False
If modName = shCodeName Then doAddST = False
For i = 1 To vbMod.CountOfLines
If i > 10 Then Exit For
If InStr(1, vbMod.Lines(i, 1), "#Const NO_TRACE = 1") > 0 Then doAddST = False
Next
If doAddST Then
' 行数を取得
lineNum = vbMod.CountOfDeclarationLines + 1
numLines = vbMod.CountOfLines
' すべてのプロシージャを調査
Do While lineNum < numLines
' 次のプロシージャの取得
procKind = 0
procName = vbMod.ProcOfLine(lineNum, procKind)
If procName <> "" Then
Set exitSub = New Collection
' プロシージャ定義の開始行の取得
lineNum = vbMod.ProcBodyLine(procName, procKind)
defineLineNum = lineNum
Do While Right(vbMod.Lines(lineNum, 1), 1) = "_"
lineNum = lineNum + 1
Loop
startLineNum = lineNum + 1
' プロシージャ定義の終了行の取得
lineNum = vbMod.procStartLine(procName, procKind) + vbMod.ProcCountLines(procName, procKind)
endLineNum = 0
Do While lineNum >= startLineNum
If Left(vbMod.Lines(lineNum, 1), 3) = "End" Then
endLineNum = lineNum
Exit Do
End If
lineNum = lineNum - 1
Loop
' 挿入可否チェック(定義~Endまでが1行で書かれているインターフェイスクラスの仮想メソッドには挿入するのが面倒なので挿入しないため。)
If endLineNum > startLineNum Then
' 既追加行の削除
Dim cnt As Long
cnt = 0
For i = endLineNum To startLineNum Step -1
If Right(vbMod.Lines(i, 1), Len("'AddStackTrace")) = "'AddStackTrace" Then
Call vbMod.DeleteLines(i)
#If DEBUG_PRINT_MODE Then
Debug.Print "DELETE:" & CStr(i)
#End If
cnt = cnt + 1
End If
If (InStr(1, vbMod.Lines(i, 1), "Exit Sub", vbTextCompare) > 0) Or (InStr(1, vbMod.Lines(i, 1), "Exit Function", vbTextCompare) > 0) Then
Do While InStr(1, vbMod.Lines(i, 1), "Call PopStackTrace", vbTextCompare) > 0
Dim pos1 As Long
Dim pos2 As Long
pos1 = InStr(1, vbMod.Lines(i, 1), "Call PopStackTrace", vbTextCompare) - 1
pos2 = InStr(pos1, vbMod.Lines(i, 1), ":", vbTextCompare) + 1
Dim repLine As String
repLine = Left(vbMod.Lines(i, 1), pos1) & Mid(vbMod.Lines(i, 1), pos2)
#If DEBUG_PRINT_MODE Then
Debug.Print repLine
#End If
vbMod.ReplaceLine i, repLine
Loop
End If
Next
endLineNum = endLineNum - cnt
' Exit Sub/Functionの検索
For i = endLineNum To startLineNum Step -1
If InStr(1, vbMod.Lines(i, 1), "Exit Sub", vbTextCompare) > 0 Then exitSub.Add i
If InStr(1, vbMod.Lines(i, 1), "Exit Function", vbTextCompare) > 0 Then exitSub.Add i
Next
' 解析コードの作成
Dim paramValue As String
paramValue = GetProcedureArguments(vbMod, procName, procKind)
Dim returnValue As String
returnValue = ""
If InStr(1, vbMod.Lines(defineLineNum, 1), "Function ") > 0 Then
returnValue = ", " & procName
ElseIf InStr(1, vbMod.Lines(defineLineNum, 1), "Property Get ") > 0 Then
returnValue = ", " & procName
If Len(paramValue) = 0 Then
paramValue = ", ""[Property-Get]"""
End If
End If
Dim insertLinePush As String
insertLinePush = _
"#If DEBUG_MODE Then 'AddStackTrace" & vbCrLf & _
"Call PushStackTrace(""" & modName & """, """ & procName & """" & paramValue & ") 'AddStackTrace" & vbCrLf & _
"#End If 'AddStackTrace"
Dim insertLinePop As String
insertLinePop = _
"#If DEBUG_MODE Then 'AddStackTrace" & vbCrLf & _
"Call PopStackTrace(""" & modName & """, """ & procName & """" & returnValue & ") 'AddStackTrace" & vbCrLf & _
"#End If 'AddStackTrace"
Dim insertProcPop As String
insertProcPop = _
"Call PopStackTrace(""" & modName & """, """ & procName & """" & returnValue & "): "
' 解析コードの追加
vbMod.InsertLines endLineNum, insertLinePop
For i = 1 To exitSub.Count
' Exit Sub/Function が 1行形式If文で使用されている場合は Exit Sub/Function を insertProcPop : Exit Sub/Function に置き換える
' そうでない場合はinsertLinePopを追加する
If InStr(1, vbMod.Lines(exitSub(i), 1), "If ", vbTextCompare) > 0 Then
If InStr(1, vbMod.Lines(exitSub(i), 1), " Then ", vbTextCompare) > 0 Then
If InStr(1, vbMod.Lines(exitSub(i), 1), "Exit Sub", vbTextCompare) > 0 Then
Call vbMod.ReplaceLine(exitSub(i), Replace(vbMod.Lines(exitSub(i), 1), "Exit Sub", insertProcPop & "Exit Sub", 1, -1, vbTextCompare))
End If
If InStr(1, vbMod.Lines(exitSub(i), 1), "Exit Function", vbTextCompare) > 0 Then
Call vbMod.ReplaceLine(exitSub(i), Replace(vbMod.Lines(exitSub(i), 1), "Exit Function", insertProcPop & "Exit Function", 1, -1, vbTextCompare))
End If
Else
vbMod.InsertLines exitSub(i), insertLinePop
End If
ElseIf InStr(1, vbMod.Lines(exitSub(i), 1), "Else:", vbTextCompare) > 0 Then
If InStr(1, vbMod.Lines(exitSub(i), 1), "Exit Sub", vbTextCompare) > 0 Then
Call vbMod.ReplaceLine(exitSub(i), Replace(vbMod.Lines(exitSub(i), 1), "Exit Sub", insertProcPop & "Exit Sub", 1, -1, vbTextCompare))
End If
If InStr(1, vbMod.Lines(exitSub(i), 1), "Exit Function", vbTextCompare) > 0 Then
Call vbMod.ReplaceLine(exitSub(i), Replace(vbMod.Lines(exitSub(i), 1), "Exit Function", insertProcPop & "Exit Function", 1, -1, vbTextCompare))
End If
Else
vbMod.InsertLines exitSub(i), insertLinePop
End If
Next
vbMod.InsertLines startLineNum, insertLinePush
#If DEBUG_PRINT_MODE Then
Debug.Print "INSERT[" & CStr(endLineNum) & "]:" & insertLinePop
Debug.Print "INSERT[" & CStr(startLineNum) & "]:" & insertLinePush
#End If
End If
#If DEBUG_PRINT_MODE Then
Debug.Print modName & ":" & CStr(startLineNum); "-"; CStr(endLineNum) & " " & procName
#End If
lineNum = vbMod.procStartLine(procName, procKind) + vbMod.ProcCountLines(procName, procKind) + 1
numLines = vbMod.CountOfLines
Set exitSub = Nothing
Else
Exit Do
End If
Loop
End If
Next
End Sub
Sub RemoveStackTrace()
Dim vbEnv As Object
Dim vbComp As Variant
Dim vbMod As Object
Dim modName As String
Dim procName As String
Dim lineNum As Long
Dim numLines As Long
Dim defineLineNum As Long
Dim startLineNum As Long
Dim endLineNum As Long
Dim procKind As Long
Dim exitSub As Collection
Dim shCodeName As String
shCodeName = "Sheet_DebugTrace"
' VBEの参照
Set vbEnv = Application.VBE
' すべてのVBComponentsを逐次調査する
For Each vbComp In vbEnv.ActiveVBProject.VBComponents
' Module名を取得
Set vbMod = vbComp.CodeModule
modName = vbMod.Name
Dim doAddST As Boolean
doAddST = True
If modName = "StackTrace" Then doAddST = False
If modName = "StackTraceLog" Then doAddST = False
If modName = shCodeName Then doAddST = False
If doAddST Then
' 行数を取得
lineNum = vbMod.CountOfDeclarationLines + 1
numLines = vbMod.CountOfLines
' すべてのプロシージャを調査
Do While lineNum < numLines
' 次のプロシージャの取得
procKind = 0
procName = vbMod.ProcOfLine(lineNum, procKind)
If procName <> "" Then
' プロシージャ定義の開始行の取得
lineNum = vbMod.ProcBodyLine(procName, procKind)
defineLineNum = lineNum
Do While Right(vbMod.Lines(lineNum, 1), 1) = "_"
lineNum = lineNum + 1
Loop
startLineNum = lineNum + 1
' プロシージャ定義の終了行の取得
lineNum = vbMod.procStartLine(procName, procKind) + vbMod.ProcCountLines(procName, procKind)
endLineNum = 0
Do While lineNum >= startLineNum
If Left(vbMod.Lines(lineNum, 1), 3) = "End" Then
endLineNum = lineNum
Exit Do
End If
lineNum = lineNum - 1
Loop
' 挿入可否チェック(定義~Endまでが1行で書かれているインターフェイスクラスの仮想メソッドには挿入するのが面倒なので挿入しないため。)
If endLineNum > startLineNum Then
Dim i As Long
Dim cnt As Long
cnt = 0
For i = endLineNum To startLineNum Step -1
If Right(vbMod.Lines(i, 1), Len("'AddStackTrace")) = "'AddStackTrace" Then
Call vbMod.DeleteLines(i)
#If DEBUG_PRINT_MODE Then
Debug.Print "DELETE:" & CStr(i)
#End If
cnt = cnt + 1
End If
If (InStr(1, vbMod.Lines(i, 1), "Exit Sub", vbTextCompare) > 0) Or (InStr(1, vbMod.Lines(i, 1), "Exit Function", vbTextCompare) > 0) Then
Do While InStr(1, vbMod.Lines(i, 1), "Call PopStackTrace", vbTextCompare) > 0
Dim pos1 As Long
Dim pos2 As Long
pos1 = InStr(1, vbMod.Lines(i, 1), "Call PopStackTrace", vbTextCompare) - 1
pos2 = InStr(pos1, vbMod.Lines(i, 1), ":", vbTextCompare) + 1
Dim repLine As String
repLine = Left(vbMod.Lines(i, 1), pos1) & Mid(vbMod.Lines(i, 1), pos2)
#If DEBUG_PRINT_MODE Then
Debug.Print repLine
#End If
vbMod.ReplaceLine i, repLine
Loop
End If
Next
endLineNum = endLineNum - cnt
End If
#If DEBUG_PRINT_MODE Then
Debug.Print modName & ":" & CStr(startLineNum); "-"; CStr(endLineNum) & " " & procName
#End If
lineNum = vbMod.procStartLine(procName, procKind) + vbMod.ProcCountLines(procName, procKind) + 1
numLines = vbMod.CountOfLines
Else
Exit Do
End If
Loop
End If
Next
End Sub
Private Function GetProcedureArguments(vbMod As Object, procName As String, procKind As Long) As String
Dim procStartLine As Long
Dim procdefLineCount As Long
Dim procDef As String
Dim Line As String
Dim i As Long
Dim startPos As Long, endPos As Long
Dim result As String
' プロシージャの開始行を取得
procStartLine = vbMod.ProcBodyLine(procName, procKind)
' プロシージャ定義の行を結合
procdefLineCount = 1
Do
Line = vbMod.Lines(procStartLine + procdefLineCount - 1, 1)
If Right(Line, 1) = "_" Then
procDef = procDef & " " & Left(Trim(Line), Len(Trim(Line)) - 1) ' 末尾の _ のみ削除
Else
procDef = procDef & " " & Trim(Line)
End If
procdefLineCount = procdefLineCount + 1
Loop While Right(Line, 1) = "_"
' 引数リストの開始位置を探す
procDef = Replace(procDef, "()", "")
startPos = InStr(1, procDef, "(")
endPos = InStrRev(procDef, ")")
result = ""
If startPos > 0 And endPos > startPos Then
procDef = Mid(procDef, startPos + 1, endPos - startPos - 1)
' 引数リストを分割
Dim args As Variant
args = Split(procDef, ",")
For i = LBound(args) To UBound(args)
Dim arg As String
arg = Trim(args(i))
' 不要な修飾子を削除
arg = Replace(arg, "ByRef ", "")
arg = Replace(arg, "ByVal ", "")
arg = Replace(arg, "Optional ", "")
arg = Replace(arg, "ParamArray ", "")
' 型情報やデフォルト値を削除
Dim argParts As Variant
argParts = Split(arg, " As ")
argParts = Split(argParts(0), "=")
' 最初の要素を引数名とみなす
If Len(Trim(argParts(0))) > 0 Then
Dim paramName As String
paramName = Trim(argParts(0))
result = result & ", """ & paramName & """, " & paramName
End If
Next i
End If
GetProcedureArguments = result
End Function