-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbody.plsql
More file actions
589 lines (479 loc) · 20.5 KB
/
body.plsql
File metadata and controls
589 lines (479 loc) · 20.5 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
create or replace package body debugger as
procedure abort is/*{*/
runinfo dbms_debug.runtime_info;
ret binary_integer;
begin
continue_(dbms_debug.abort_execution);
end abort;/*}*/
procedure backtrace is/*{*/
pkgs dbms_debug.backtrace_table;
i number;
begin
dbms_debug.print_backtrace(pkgs);
i := pkgs.first();
dbms_output.put_line('backtrace');
while i is not null loop
dbms_output.put_line(' ' || i || ': ' || pkgs(i).name || ' (' || pkgs(i).line# ||')');
i := pkgs.next(i);
end loop;
exception
when others then
dbms_output.put_line(' backtrace exception: ' || sqlcode);
dbms_output.put_line(' ' || sqlerrm(sqlcode));
end backtrace;/*}*/
procedure breakpoints is/*{*/
brkpts dbms_debug.breakpoint_table;
i number;
v_line number;
begin
dbms_debug.show_breakpoints(brkpts);
i := brkpts.first();
dbms_output.put_line('');
while i is not null loop
if v_line is not null then
dbms_output.put( to_char(v_line , '99999'));
null;
else
dbms_output.put(' ');
end if;
dbms_output.put( ' ');
dbms_output.put(to_char(i,'999') || ': ');
dbms_output.put(rpad(coalesce(brkpts(i).name, ' '), 31));
dbms_output.put(rpad(coalesce(brkpts(i).owner,' '), 31));
v_line:=brkpts(i).line#;
dbms_output.put( ' ');
dbms_output.put(libunittype_as_string(brkpts(i).libunittype));
dbms_output.put( ' ');
dbms_output.put(bp_status_as_string (brkpts(i).status ));
dbms_output.put_line('');
i := brkpts.next(i);
end loop;
end breakpoints;/*}*/
function libunittype_as_string(lut binary_integer) /*{*/
/*
dbms_debug.continue can be called with the following breakflags:
o break_next_line ( Break at next source line (step over calls) )
o break_any_call ( Break at next source line (step into calls) )
o break_any_return
o break_return
o break_exception
o break_handler
o abort_execution
As the user of debugger might want to use continue with variying breakflags, continue_ (with the
underscore) is the generic wrapper. (I hope this makes sense)
*/
return varchar2 is
begin
if lut = dbms_debug.libunitType_cursor then return 'Cursor'; end if;
if lut = dbms_debug.libunitType_procedure then return 'Proc' ; end if;
if lut = dbms_debug.libunitType_function then return 'Func' ; end if;
if lut = dbms_debug.libunitType_function then return 'Func' ; end if;
if lut = dbms_debug.libunitType_package then return 'Pkg' ; end if;
if lut = dbms_debug.libunitType_package_body then return 'Pkg Bd'; end if;
if lut = dbms_debug.libunitType_trigger then return 'Trig' ; end if;
if lut = dbms_debug.libunitType_unknown then return 'Unk' ; end if;
return '???';
end libunittype_as_string;/*}*/
function bp_status_as_string(bps binary_integer) return varchar2 is/*{*/
-- "User friendly" name for breakpoint_status_*
begin
if bps = dbms_debug.breakpoint_status_unused then return 'unused' ; end if;
if bps = dbms_debug.breakpoint_status_active then return 'active' ; end if;
if bps = dbms_debug.breakpoint_status_disabled then return 'disabled'; end if;
if bps = dbms_debug.breakpoint_status_remote then return 'remote' ; end if;
return '???';
end bp_status_as_string;/*}*/
procedure continue_(break_flags in number) is/*{*/
ret binary_integer;
v_err varchar2(100);
begin
dbms_output.put_line('');
ret := dbms_debug.continue(
cur_line_,
break_flags,
0 +
dbms_debug.info_getlineinfo +
dbms_debug.info_getbreakpoint +
dbms_debug.info_getstackdepth +
dbms_debug.info_getoerinfo +
0);
if ret = dbms_debug.success then
dbms_output.put_line(' reason for break: ' || str_for_reason_in_runtime_info(cur_line_.reason));
if cur_line_.reason = dbms_debug.reason_knl_exit then
return;
end if;
if cur_line_.reason = dbms_debug.reason_exit then
return;
end if;
--print_runtime_info_with_source(cur_line_,cont_lines_before_, cont_lines_after_,cont_lines_width_);
print_source(cur_line_, cont_lines_before_, cont_lines_after_);
elsif ret = dbms_debug.error_timeout then
dbms_output.put_line(' continue: error_timeout');
elsif ret = dbms_debug.error_communication then
dbms_output.put_line(' continue: error_communication');
else
v_err := general_error(ret);
dbms_output.put_line(' continue: general error' || v_err);
end if;
end continue_;/*}*/
procedure detach is/*{*/
begin
dbms_debug.detach_session;
end detach;/*}*/
procedure continue is/*{*/
/*
continue (calling continue_ with break_flags = 0 ) will run until
the program hits a breakpoint
*/
begin
continue_(0);
end continue;/*}*/
procedure delete_bp(breakpoint in binary_integer) is/*{*/
ret binary_integer;
begin
ret := dbms_debug.delete_breakpoint(breakpoint);
if ret = dbms_debug.success then dbms_output.put_line(' breakpoint deleted');
elsif ret = dbms_debug.error_no_such_breakpt then dbms_output.put_line(' No such breakpoint exists');
elsif ret = dbms_debug.error_idle_breakpt then dbms_output.put_line(' Cannot delete an unused breakpoint');
elsif ret = dbms_debug.error_stale_breakpt then dbms_output.put_line(' The program unit was redefined since the breakpoint was set');
else dbms_output.put_line(' Unknown error');
end if;
end delete_bp;/*}*/
procedure print_var(name in varchar2) is/*{*/
ret binary_integer;
val varchar2(4000);
frame number;
begin
frame := 0;
ret := dbms_debug.get_value(
name,
frame,
val,
null);
if ret = dbms_debug.success then dbms_output.put_line(' ' || name || ' = ' || val);
elsif ret = dbms_debug.error_bogus_frame then dbms_output.put_line(' print_var: frame does not exist');
elsif ret = dbms_debug.error_no_debug_info then dbms_output.put_line(' print_var: Entrypoint has no debug info');
elsif ret = dbms_debug.error_no_such_object then dbms_output.put_line(' print_var: variable ' || name || ' does not exist in in frame ' || frame);
elsif ret = dbms_debug.error_unknown_type then dbms_output.put_line(' print_var: The type information in the debug information is illegible');
elsif ret = dbms_debug.error_nullvalue then dbms_output.put_line(' ' || name || ' = NULL');
elsif ret = dbms_debug.error_indexed_table then dbms_output.put_line(' print_var: The object is a table, but no index was provided.');
else dbms_output.put_line(' print_var: unknown error');
end if;
end print_var;/*}*/
procedure start_debugger(debug_session_id in varchar2) is/*{*/
/*
This is the first call the debugging session must make. It, in turn, calls
dbms_debug.attach_session.
After attaching to the session, it waits for the first event (wait_until_running), which is interpreter starting.
*/
begin
dbms_debug.attach_session(debug_session_id);
--cont_lines_before_ := 5;
--cont_lines_after_ := 5;
--cont_lines_width_ := 100;
wait_until_running;
end start_debugger;/*}*/
--function start_debugee return varchar2 as/*{*/
--/* This is the first call the debugged session must make.
--
-- The return value must be passed to the debugging session and used in start_debugger
--*/
-- debug_session_id varchar2(20);
--begin
-- --select dbms_debug.initialize into debug_session_id from dual;
-- debug_session_id := dbms_debug.initialize;
-- dbms_debug.debug_on;
-- return debug_session_id;
--end start_debugee;/*}*/
procedure print_proginfo(prginfo dbms_debug.program_info) as/*{*/
begin
dbms_output.put_line(' Namespace: ' || str_for_namespace(prginfo.namespace));
dbms_output.put_line(' Name: ' || prginfo.name);
dbms_output.put_line(' owner: ' || prginfo.owner);
dbms_output.put_line(' dblink: ' || prginfo.dblink);
dbms_output.put_line(' Line#: ' || prginfo.Line#);
dbms_output.put_line(' lib unit: ' || prginfo.libunittype);
dbms_output.put_line(' entrypoint: ' || prginfo.entrypointname);
end print_proginfo;/*}*/
procedure print_runtime_info(runinfo dbms_debug.runtime_info) as/*{*/
--rsnt varchar2(40);
begin
--rsnt := str_for_reason_in_runtime_info(runinfo.reason);
dbms_output.put_line('');
dbms_output.put_line('Runtime Info');
dbms_output.put_line('Prg Name: ' || runinfo.program.name);
dbms_output.put_line('Line: ' || runinfo.line#);
dbms_output.put_line('Terminated: ' || runinfo.terminated);
dbms_output.put_line('Breakpoint: ' || runinfo.breakpoint);
dbms_output.put_line('Stackdepth ' || runinfo.stackdepth);
dbms_output.put_line('Interpr depth: ' || runinfo.interpreterdepth);
--dbms_output.put_line('Reason ' || rsnt);
dbms_output.put_line('Reason: ' || str_for_reason_in_runtime_info(runinfo.reason));
print_proginfo(runinfo.program);
end print_runtime_info;/*}*/
procedure print_source (/*{*/
runinfo dbms_debug.runtime_info,
lines_before number default 0,
lines_after number default 0
) is
first_line binary_integer;
last_line binary_integer;
prefix varchar2( 99);
suffix varchar2(4000);
--source_lines vc2_table;
source_lines dbms_debug.vc2_table;
cur_line binary_integer;
cur_real_line number;
begin
first_line := greatest(runinfo.line# - cont_lines_before_,1);
last_line := runinfo.line# + cont_lines_after_ ;
if first_line is null or last_line is null then
dbms_output.put_line('first_line or last_line is null');
print_runtime_info(runinfo);
return;
end if;
if runinfo.program.name is not null and runinfo.program.owner is not null then
dbms_output.put_line('');
dbms_output.put_line(' ' || runinfo.program.owner || '.' || runinfo.program.name);
--select
-- cast(multiset(
for r in (
select
-- 90 is the length in dbms_debug.vc2_table....
rownum line,
substr(text,1,90) text
from
all_source
where
name = runinfo.program.name and
owner = runinfo.program.owner and
type <> 'PACKAGE' and
line >= first_line and
line <= last_line
order by
line )-- as vc2_table)
loop
-- into
-- source_lines
-- from
-- dual;
source_lines(r.line) := r.text;
end loop;
else
dbms_debug.show_source(first_line, last_line, source_lines);
-- select
-- cast(
-- multiset(
-- select culumn_value from
-- table(
-- cast(source_lines_dbms as dbms_debug.vc2_table)
-- )
-- )as vc2_table)
-- into
-- source_lines
-- from
-- dual;
end if;
dbms_output.put_line('');
cur_line := source_lines.first();
while cur_line is not null loop
cur_real_line := cur_line + first_line -1;
-- for r in (select column_value text from table(source_lines)) loop
prefix := to_char(cur_real_line,'9999');
if cur_real_line = runinfo.line# then
prefix := prefix || ' -> ';
else
prefix := prefix || ' ';
end if;
-- TODO, most probably superfluos, 90 is the max width.... (ts, ts)
--if length(r.text) > v_lines_width then
-- suffix := substr(r.text,1,v_lines_width);
--else
-- suffix := r.text;
--end if;
suffix := source_lines(cur_line);
suffix := translate(suffix,chr(10),' ');
suffix := translate(suffix,chr(13),' ');
--dbms_output.put_line(prefix || suffix);
dbms_output.put_line(prefix || suffix);
-- line_printed := 'Y';
cur_line := source_lines.next(cur_line);
--cur_line := cur_line + 1;
end loop;
dbms_output.put_line('');
end print_source;/*}*/
procedure print_runtime_info_with_source(/*{*/
runinfo dbms_debug.runtime_info
) is
begin
print_runtime_info(runinfo);
--dbms_output.put_line('line#: ' || runinfo.line#);
--dbms_output.put_line(' - : ' || (runinfo.line# - cont_lines_before_));
--dbms_output.put_line('first_line: ' || first_line);
--dbms_output.put_line('last_line: ' || last_line);
print_source(runinfo);
end print_runtime_info_with_source;/*}*/
procedure self_check as/*{*/
ret binary_integer;
begin
dbms_debug.self_check(5);
exception
when dbms_debug.pipe_creation_failure then dbms_output.put_line(' self_check: pipe_creation_failure');
when dbms_debug.pipe_send_failure then dbms_output.put_line(' self_check: pipe_send_failure');
when dbms_debug.pipe_receive_failure then dbms_output.put_line(' self_check: pipe_receive_failure');
when dbms_debug.pipe_datatype_mismatch then dbms_output.put_line(' self_check: pipe_datatype_mismatch');
when dbms_debug.pipe_data_error then dbms_output.put_line(' self_check: pipe_data_error');
when others then dbms_output.put_line(' self_check: unknown error');
end self_check;/*}*/
procedure set_breakpoint(/*{*/
/*
Out of the four parameters
p_cursor, p_toplevel, p_body, p_trigger,
at most one should be set to zero. They set the
proginfo.namespace
*/
p_line in number, p_name in varchar2 default null, p_owner in varchar2 default null,
p_cursor in boolean default false,
p_toplevel in boolean default false,
p_body in boolean default false,
p_trigger in boolean default false)
as
proginfo dbms_debug.program_info;
ret binary_integer;
bp binary_integer;
begin
if p_cursor then proginfo.namespace := dbms_debug.namespace_cursor;
elsif p_toplevel then proginfo.namespace := dbms_debug.namespace_pkgspec_or_toplevel;
elsif p_body then proginfo.namespace := dbms_debug.namespace_pkg_body;
elsif p_trigger then proginfo.namespace := dbms_debug.namespace_trigger;
else proginfo.namespace := null;
end if;
proginfo.name := p_name;
proginfo.owner := p_owner;
proginfo.dblink := null;
proginfo.entrypointname := null;
ret := dbms_debug.set_breakpoint(
proginfo,
p_line,
bp);
if ret = dbms_debug.success then dbms_output.put_line(' breakpoint set: ' || bp);
elsif ret = dbms_debug.error_illegal_line then dbms_output.put_line(' set_breakpoint: error_illegal_line');
elsif ret = dbms_debug.error_bad_handle then dbms_output.put_line(' set_breakpoint: error_bad_handle');
else dbms_output.put_line(' set_breakpoint: unknown error (' || ret || ')');
end if;
end set_breakpoint;/*}*/
procedure step is/*{*/
begin
continue_(dbms_debug.break_next_line);
end step;/*}*/
procedure step_into is/*{*/
begin
continue_(dbms_debug.break_any_call);
end step_into;/*}*/
procedure step_out is/*{*/
begin
continue_(dbms_debug.break_any_return);
end step_out; /*}*/
function str_for_namespace(nsp in binary_integer) return varchar2 is/*{*/
nsps varchar2(40);
begin
if nsp = dbms_debug.Namespace_cursor then nsps := 'Cursor (anonymous block)';
elsif nsp = dbms_debug.Namespace_pkgspec_or_toplevel then nsps := 'package, proc, func or obj type';
elsif nsp = dbms_debug.Namespace_pkg_body then nsps := 'package body or type body';
elsif nsp = dbms_debug.Namespace_trigger then nsps := 'Triggers';
else nsps := 'Unknown namespace';
end if;
return nsps;
end str_for_namespace;/*}*/
function str_for_reason_in_runtime_info(rsn in binary_integer) return varchar2 is/*{*/
rsnt varchar2(40);
begin
if rsn = dbms_debug.reason_none then rsnt := 'none';
elsif rsn = dbms_debug.reason_interpreter_starting then rsnt := 'Interpreter is starting.';
elsif rsn = dbms_debug.reason_breakpoint then rsnt := 'Hit a breakpoint';
elsif rsn = dbms_debug.reason_enter then rsnt := 'Procedure entry';
elsif rsn = dbms_debug.reason_return then rsnt := 'Procedure is about to return';
elsif rsn = dbms_debug.reason_finish then rsnt := 'Procedure is finished';
elsif rsn = dbms_debug.reason_line then rsnt := 'Reached a new line';
elsif rsn = dbms_debug.reason_interrupt then rsnt := 'An interrupt occurred';
elsif rsn = dbms_debug.reason_exception then rsnt := 'An exception was raised';
elsif rsn = dbms_debug.reason_exit then rsnt := 'Interpreter is exiting (old form)';
elsif rsn = dbms_debug.reason_knl_exit then rsnt := 'Kernel is exiting';
elsif rsn = dbms_debug.reason_handler then rsnt := 'Start exception-handler';
elsif rsn = dbms_debug.reason_timeout then rsnt := 'A timeout occurred';
elsif rsn = dbms_debug.reason_instantiate then rsnt := 'Instantiation block';
elsif rsn = dbms_debug.reason_abort then rsnt := 'Interpreter is aborting';
else rsnt := 'Unknown reason';
end if;
return rsnt;
end str_for_reason_in_runtime_info;/*}*/
procedure wait_until_running as/*{*/
runinfo dbms_debug.runtime_info;
ret binary_integer;
v_err varchar2(100);
begin
ret:=dbms_debug.synchronize( runinfo, 0 /*+
dbms_debug.info_getstackdepth +
dbms_debug.info_getbreakpoint +
dbms_debug.info_getlineinfo +
dbms_debug.info_getoerinfo +
0 */
);
if ret = dbms_debug.success then
print_runtime_info(runinfo);
elsif ret = dbms_debug.error_timeout then
dbms_output.put_line(' synchronize: error_timeout');
elsif ret = dbms_debug.error_communication then
dbms_output.put_line(' synchronize: error_communication');
else
v_err := general_error(ret);
dbms_output.put_line(' synchronize: general error' || v_err);
--dbms_output.put_line(' synchronize: unknown error');
end if;
end wait_until_running;/*}*/
procedure is_running is/*{*/
begin
if dbms_debug.target_program_running then
dbms_output.put_line(' target (debugee) is running');
else
dbms_output.put_line(' target (debugee) is not running');
end if;
end is_running;/*}*/
function general_error(e in binary_integer) return varchar2 is/*{*/
begin
if e = dbms_debug.error_unimplemented then return 'unimplemented' ; end if;
if e = dbms_debug.error_deferred then return 'deferred' ; end if;
if e = dbms_debug.error_exception then return 'probe exception' ; end if;
if e = dbms_debug.error_communication then return 'communication error' ; end if;
if e = dbms_debug.error_unimplemented then return 'unimplemented' ; end if;
if e = dbms_debug.error_timeout then return 'timeout' ; end if;
return '???';
end general_error;/*}*/
procedure version as/*{*/
major binary_integer;
minor binary_integer;
begin
dbms_debug.probe_version(major,minor);
dbms_output.put_line(' probe version is: ' || major || '.' || minor);
end version;/*}*/
procedure current_prg is/*{*/
ri dbms_debug.runtime_info;
pi dbms_debug.program_info;
ret binary_integer;
begin
ret := dbms_debug.get_runtime_info(
0 +
dbms_debug.info_getlineinfo +
dbms_debug.info_getbreakpoint +
dbms_debug.info_getstackdepth +
dbms_debug.info_getoerinfo +
0,
ri);
pi := ri.program;
print_proginfo(pi);
end current_prg;/*}*/
begin
cont_lines_before_ := 5;
cont_lines_after_ := 5;
end debugger;
/