DBA Data[Home] [Help]

PACKAGE BODY: OWAPUB.DEBUG_CARTX

Source


1 PACKAGE BODY DEBUG_CARTX as
2 
3 -- ***************************************************************************
4 --  Copyright (c) 1991, 1998 by Oracle Corporation.  All Rights Reserved.
5 --
6 --  NAME
7 --    DEBUG_CARTX Package body
8 --  DESCRIPTION
9 --    This package implements PL/SQL cartridge debugging using OPB.
10 --
11 --  NOTES
12 --    The user of this package should have Execute privilege on the stored
13 --    procedure to be debugged.
14 --    This package is compatible with Oracle Database of version 7.3.4 and
15 --    8.0.4 or above, Oracle Procedure Builder 6.1 and above.
16 --    This package is dependent on WebDB 2.1 or above.
17 --
18 --    MODIFIED  MM/DD/YY   Comments
19 --
20 --    jmojnida  10/26/98   Created
21 --
22 -- ***************************************************************************
23 
24 	DEBUG_CARTX_BYE			constant varchar2(20) := 'DEBUG_CARTX_BYE';
25 	DEBUG_CARTX_RES_OVER  constant integer := -100;
26 
27 	procedure insertmessage(msg varchar2) is
28 	begin
29 	  -- delete from scott.output;
30 	  -- insert into output values(msg);
31 	  -- commit;
32 	null;
33 	end;
34 
35   procedure cleanup_pipes(pipename  varchar2) is
36   status integer;
37   begin
38   	/*  Clean up the pipes...  just in case if we have stale messages in them.
39   	    This function should be called as a last call to the pipes.
40   	    Never send a message or ack and call this immediately.
41   	    If we do this, we may remove the pipe prematurely.
42   	*/
43 	 sys.dbms_pipe.purge(pipeName);
44 	 sys.dbms_pipe.purge(pipeName || 'Res');
45 
46 	 status := sys.dbms_pipe.remove_pipe(pipeName);
47 	 status := sys.dbms_pipe.remove_pipe(pipeName || 'Res');
48   end;
49 
50 	PROCEDURE sendAck(PipeName varchar2, msg integer, status OUT integer) IS
51 	errNo			integer;
52 	BEGIN
53 	  status := sys.dbms_pipe.create_pipe(PipeName, 8192, FALSE);
54 
55 	  if (status = 0) then
56 	  	sys.dbms_pipe.Pack_Message(msg);
57 	  	status := sys.dbms_pipe.Send_Message(PipeName, 3600);
58 	  end if;
59 	end;
60 
61 	PROCEDURE sendAck(PipeName varchar2, msg varchar2, status OUT integer) IS
62 	errNo			integer;
63 	BEGIN
64 	  status := sys.dbms_pipe.create_pipe(PipeName, 8192, FALSE);
65 
66 	  if (status = 0) then
67 	  	sys.dbms_pipe.Pack_Message(msg);
68 	  	status := sys.dbms_pipe.Send_Message(PipeName, 3600);
69 	  end if;
70 	end;
71 
72   PROCEDURE sendAck(PipeName varchar2, numMsg integer,
73                     msg varchar2, status OUT integer) IS
74 	errNo			integer;
75 	BEGIN
76 	  status := sys.dbms_pipe.create_pipe(PipeName, 8192, FALSE);
77 
78 	  if (status = 0) then
79 	  	sys.dbms_pipe.Pack_Message(NumMsg);
80 	  	sys.dbms_pipe.Pack_Message(msg);
81 	  	status := sys.dbms_pipe.Send_Message(PipeName, 3600);
82 	  end if;
83 	end;
84 
85 	FUNCTION waitForAck(pipename varchar2, res out varchar2) RETURN INTEGER is
86 	msg varchar2(1000);
87 	status integer;
88 	msgType integer;
89 	numMsg integer;
90 	begin
91 		msg := '';
92 		res := '';
93 		numMsg := 0;
94 
95 		status := sys.dbms_pipe.Receive_Message(PipeName);
96 	  if (status = 0) then
97 	  	msgType := sys.dbms_pipe.Next_Item_Type;
98 	  	if (msgType = 9) then
99   			sys.dbms_pipe.unpack_message(res);
100   		elsif (msgType = 6) then
101   			sys.dbms_pipe.unpack_message(numMsg);
102   			res := to_char(numMsg);
103   		else
104   			res :=  to_char(msgType);
105   		end if;
106   	end if;
107 
108 	  return numMsg;
109 	end;
110 
111   PROCEDURE handleAborts(stopflag  integer, SessID varchar2) is
112   begin
113 
114   	htp.htmlopen;
115 		htp.headopen;
116 
117     if (stopflag = DEBUG_CARTX_ERR_CRTPROC) then
118  	    htp.title('Cartridge Debug Error');
119 			htp.headclose;
120 			htp.bodyopen;
121 			htp.bold('Error in getting proper debug information.' || htf.paragraph);
122 		  htp.print('Please recompile the procedure and try again.');
123     elsif (stopflag = DEBUG_CARTX_ABORT) then
124     	htp.title('Debug process aborted by the user');
125 			htp.headclose;
126 			htp.bodyopen;
127 			htp.bold('Debug process was aborted by the user.' || htf.paragraph);
128 			elsif (stopflag = DEBUG_CARTX_SUSPEND) then
129     	htp.title('Debug process suspeneded');
130 			htp.headclose;
131 			htp.bodyopen;
132 			htp.bold('Debug process was suspended abruptly.' || htf.paragraph);
133 		elsif (stopflag =   DEBUG_CARTX_ERR_INIT) then
134     	htp.title('Cartridge Debug Error');
135 			htp.headclose;
136 			htp.bodyopen;
137 			htp.bold('An error occurred while initilizing the CGI environment.'
138                 || htf.paragraph);
139 		elsif (stopflag =   DEBUG_CARTX_ERR_SNDRES) then
140     	htp.title('Cartridge Debug Error');
141 			htp.headclose;
142 			htp.bodyopen;
143 			htp.bold('An error occurred while accessing the result.'
144                || htf.paragraph);
145 		elsif (stopflag =   DEBUG_CARTX_ERR_RDSRC) then
146     	htp.title('Cartridge Debug Error');
147 			htp.headclose;
148 			htp.bodyopen;
149 			htp.bold('An error occurred while getting the command information.'
150                || htf.paragraph);
151 		elsif (stopflag =   DEBUG_CARTX_ERR) then
152     	htp.title('Cartridge Debug Error');
153 			htp.headclose;
154 			htp.bodyopen;
155 			htp.bold('A generic error occurred.' || htf.paragraph);
156 			htp.print('Please start the debug session afresh.');
157 		elsif (stopflag = DEBUG_CARTX_ERR_PROC_EXISTS) then
158 			htp.title('Cartridge Debug Error');
159 			htp.headclose;
160 			htp.bodyopen;
161 			htp.bold('Could not create a stub with name DE_' || SessID
162                || htf.paragraph);
163 			htp.print('Please rename the existing procedure or'
164                || ' start the debug session afresh.');
165 	  end if;
166 
167 	  htp.print(htf.paragraph);
168 		htp.bodyclose;
169 		htp.htmlclose;
170   --raise "PDE_QUIT_DEBUG_CARTX";
171   end;
172 
173 
174  	PROCEDURE getInternalDebugSession(SessionId out varchar2) IS
175 	BEGIN
176   	SessionId := sys.dbms_pipe.unique_session_name;
177 	end;
178 
179   PROCEDURE fmtSessionId (SessionId  in out varchar2) IS
180   BEGIN
181     SessionId := 'ORA$PIPE$' || SessionId;
182   END;
183 
184 
185   --Public functions.
186 
187 PROCEDURE misc_GetArgType(owner_name varchar2, proc_name varchar2,
188                           arg_name varchar2, arg_type_name OUT varchar2,
189                           package_name varchar2 default NULL) is
190   datatype  varchar2(30);
191   typename  varchar2(30);
192   typesubname  varchar2(30);
193   curs  integer;
194   qry   varchar2(500);
195   ignore  integer;
196   BEGIN
197   --  Check the datatype of the argument and if it is
198   --    PL/SQL Table type we need to get its subtype too.
199 
200     curs := 0;
201     qry := 'select DATA_TYPE from ALL_ARGUMENTS ';
202     if (package_name IS NOT NULL) then
203        qry := qry || 'where OWNER = :owner_name and ';
204        qry := qry || 'PACKAGE_NAME = :package_name and ';
205        qry := qry || 'OBJECT_NAME = :proc_name and ARGUMENT_NAME = :arg_name';
206     else
207     	qry := qry || 'where OWNER = :owner_name and OBJECT_NAME = :proc_name ';
208       qry := qry || 'and ARGUMENT_NAME = :arg_name';
209     end if;
210 
211     curs := sys.dbms_sql.open_cursor;
212 
213     --sys.dbms_sql.parse(curs, qry, 1);
214     sys.dbms_sys_sql.parse_as_user(curs, qry, 1);
215 
216     if (package_name IS NOT NULL) then
217         DBMS_SQL.BIND_VARIABLE(curs, ':package_name', package_name);
218     end if;
219 
220     DBMS_SQL.BIND_VARIABLE(curs, ':proc_name', proc_name);
221     DBMS_SQL.BIND_VARIABLE(curs, ':owner_name', owner_name);
222     DBMS_SQL.BIND_VARIABLE(curs, ':arg_name', arg_name);
223 
224     sys.dbms_sql.define_column(curs, 1, datatype, 30);
225     ignore := sys.dbms_sql.execute(curs);
226     if (sys.dbms_sql.fetch_rows(curs) > 0) then
227 		  	sys.dbms_sql.Column_value(curs, 1, datatype);
228     end if;
229     sys.dbms_sql.close_cursor(curs);
230     curs := 0;
231     if (datatype = 'PL/SQL TABLE') then
232 
233        qry := 'select DATA_TYPE, TYPE_NAME, TYPE_SUBNAME from ALL_ARGUMENTS ';
234        if (package_name IS NOT NULL) then
235           qry := qry || 'where OWNER = :owner_name and ';
236           qry := qry || 'PACKAGE_NAME = :package_name and ';
237           qry := qry || 'OBJECT_NAME = :proc_name';
238        else
239        	  qry := qry || 'where OWNER = :owner_name and ';
240           qry := qry || 'OBJECT_NAME = :proc_name';
241        end if;
242 
243        qry := qry || ' and ARGUMENT_NAME = :arg_name';
244 
245        curs := sys.dbms_sql.open_cursor;
246 
247        --sys.dbms_sql.parse(curs, qry, 1);
248        sys.dbms_sys_sql.parse_as_user(curs, qry, 1);
249 
250        sys.dbms_sql.define_column(curs, 1, datatype, 30);
251        sys.dbms_sql.define_column(curs, 2, typename, 30);
252        sys.dbms_sql.define_column(curs, 3, typesubname, 30);
253 
254        if (package_name IS NOT NULL) then
255           DBMS_SQL.BIND_VARIABLE(curs, ':package_name', package_name);
256        end if;
257 
258        DBMS_SQL.BIND_VARIABLE(curs, ':proc_name', proc_name);
259        DBMS_SQL.BIND_VARIABLE(curs, ':owner_name', owner_name);
260        DBMS_SQL.BIND_VARIABLE(curs, ':arg_name', arg_name);
261 
262        ignore := sys.dbms_sql.execute(curs);
263 
264 		  if (sys.dbms_sql.fetch_rows(curs) > 0) then
265 		  	sys.dbms_sql.Column_value(curs, 1, datatype);
266 		  	sys.dbms_sql.Column_value(curs, 2, typename);
267 		  	sys.dbms_sql.Column_value(curs, 3, typesubname);
268 		  	arg_type_name := typename || '.' || typesubname;
269 		  end if;
270       sys.dbms_sql.close_cursor(curs);
271 
272     else
273 	     arg_type_name := datatype;
274     end if;
275 
276 
277     exception
278     	when others then
279     	if (curs > 0) then
280     		  sys.dbms_sql.close_cursor(curs);
281     	end if;
282     	arg_type_name := '';
283  end;
284 
285   PROCEDURE getDebugSessionId(SessionId out varchar2) IS
286 	BEGIN
287     	getInternalDebugSession(SessionId);
288     	SessionId := substr (SessionId, 10);
289 
290 	end;
291 
292 	PROCEDURE getDebugSession IS
293 		SessionId		varchar2(50);
294 		UserName    varchar2(30);
295 	BEGIN
296   	getInternalDebugSession(SessionId);
297     SessionId := substr (SessionId, 10);
298 
299 	  htp.htmlopen;
300 		htp.headopen;
301 		htp.title('Get Debug Session ID');
302 		htp.headclose;
303 		htp.bodyopen;
304 		htp.bold('Debug Session ID:  ' || SessionId || htf.paragraph);
305 	  htp.bold ('User Name:  ' || User || htf.paragraph);
306 	  htp.print('Please use the above session id in the Procedure Builder' ||
307               ' to debug cartridge calls.');
308 	  htp.print(htf.paragraph);
309 
310 		htp.bodyclose;
311 		htp.htmlclose;
312 
313 	end;
314 
315 	PROCEDURE cleanupDebugsession(SessionId  varchar2) IS
316  		status integer;
317  	  SessID  varchar2(30);
318 	BEGIN
319 		SessID := SessionId;
320 		fmtSessionID(SessID);
321 
322 		htp.htmlopen;
323 		htp.headopen;
324 	  htp.title('Cleanup Debug Session');
325 	  htp.headclose;
326 	  htp.bodyopen;
327 
328 		--Send a 'bye' message to all waiting processes
329 	  sendAck(SessId, DEBUG_CARTX_QUIT, status);
330 	  sendAck(SessId || 'Res', DEBUG_CARTX_QUIT, status);
331 
332 	  --Now clean the pipes.
333 	  sys.dbms_pipe.purge(SessID);
334 	  sys.dbms_pipe.purge(SessID || 'Res');
335 	  status := sys.dbms_pipe.remove_pipe(SessID);
336 	  status := sys.dbms_pipe.remove_pipe(SessID || 'Res');
337 
338 	  --Now that we have successfully removed the pipes send a message.
339 	  htp.print('SessionID ' || SessionId || ' has been successfully reset.');
340 	  htp.bodyclose;
341 		htp.htmlclose;
342 
343 	  exception
344 	  	when others then
345 	  --Just handle any exception
346 	  		htp.print('SessionID ' || SessionId || ' could not be reset.');
347 	  		htp.print(htf.paragraph);
348 	  		htp.print(to_char(SQLCODE) || ': ' || SQLERRM);
349 	  		htp.bodyclose;
350 				htp.htmlclose;
351 	end;
352 
353  	PROCEDURE resetDebugsession IS
354  		SessionId  varchar2(50);
355  		status integer;
356 	BEGIN
357     htp.htmlopen;
358 		htp.headopen;
359 	  htp.title('Reset Debug Session');
360 	  htp.headclose;
361 	  htp.bodyopen;
362 
363 		getInternalDebugSession(SessionId);
364 		--Send a 'bye' message to all waiting processes
365 	  sendAck(SessionId, DEBUG_CARTX_QUIT, status);
366 	  sendAck(SessionId || 'Res', DEBUG_CARTX_QUIT, status);
367 	  --Now clean the pipes.
368 	  sys.dbms_pipe.purge(SessionID);
369 	  sys.dbms_pipe.purge(SessionID || 'Res');
370 	  status := sys.dbms_pipe.remove_pipe(SessionID);
371 	  status := sys.dbms_pipe.remove_pipe(SessionID || 'Res');
372 
373 	  --Now that we have successfully removed the pipes send a message.
374 	  htp.print('SessionID: ' || substr(SessionId, 10) ||
375               ' is successfully reset.');
376 	  htp.print(htf.paragraph);
377 	  htp.bodyclose;
378 		htp.htmlclose;
379 
380 	  exception
381 	  	when others then
382 	  		htp.print('SessionID: ' || substr(SessionId, 10) ||
383                   ' could not be reset.');
384 	  		htp.print(htf.paragraph);
385 	  		htp.print(to_char(SQLCODE) || ': ' || SQLERRM);
386 	  		htp.bodyclose;
387 				htp.htmlclose;
388 	end;
389 
390 
391 	PROCEDURE tgt_init_cgi_env(num_params in number,
392                            param_name in owa.vc_arr,
393                            param_val  in owa.vc_arr)is
394   pipename    varchar2(30);
395   resPipeName varchar2(30);
396 	errNo			integer;
397 	counter   integer;
398 	msg				varchar2(1000);
399 	status		integer;
400 	res				varchar2(100);
401 	ack       integer;
402 	SessID    varchar2(30);
403   begin
404 
405 	 getInternalDebugSession(pipeName);
406 
407 
408 	 resPipeName := pipeName || 'Res';
409 
410    status := sys.dbms_pipe.create_pipe(PipeName, 8192, FALSE);
411    if (status = 0) then
412 	  	sys.dbms_pipe.Pack_Message(num_params);
413 
414 			FOR counter IN 1 .. num_params LOOP
415 	  		msg := param_name(counter);
416 	  		sys.dbms_pipe.Pack_Message(msg);
417 			END LOOP;
418 
419 		  counter := 1;
420 		  FOR counter IN 1 .. num_params LOOP
421 	  		msg := param_val(counter);
422 	  		sys.dbms_pipe.Pack_Message(msg);
423 			END LOOP;
424 		  status := sys.dbms_pipe.Send_Message(PipeName, 3600);
428       	 getDebugSessionId(SessId);
425 		  ack := waitForAck(resPipeName, res);
426 
427       if (ack < DEBUG_CARTX_SUCCESS) then
429       	 handleAborts(ack, Sessid);
430       	 cleanup_pipes(PipeName);
431       	 raise PDE_QUIT_DEBUG_CARTX;
432 		  end if;
433       else
434       	htp.htmlopen;
435 		    htp.headopen;
436 		    htp.print('An error occurred.  May be insufficient privileges.');
437 		    htp.print(htf.paragraph);
438 			  htp.bodyclose;
439 				htp.htmlclose;
440 				cleanup_pipes(PipeName);
441 	  	  raise PDE_CTX_TGT;
442 	  end if;
443 
444   end;
445 
446   PROCEDURE tgt_Exec_Proc(procname varchar2) is
447   status		integer;
448   res				varchar2(100);
449   ack       integer;
450   pipeName  varchar2(30);
451   resPipeName varchar2(30);
452   sessId     varchar2(30);
453   begin
454    getInternalDebugSession(pipeName);
455 	 resPipeName := pipeName || 'Res';
456 
457 	  status := sys.dbms_pipe.create_pipe(pipeName, 8192, FALSE);
458 	  if (status = 0) then
459 	  	sys.dbms_pipe.Pack_Message(procname);
460 	  	status := sys.dbms_pipe.Send_Message(pipeName, 3600);
461 		  ack := waitForAck(resPipeName, res);
462 
463       if (ack < DEBUG_CARTX_SUCCESS) then
464       	 getDebugSessionId(SessId);
465       	 handleAborts(ack, Sessid);
466       	 cleanup_pipes(pipeName);
467 		     raise PDE_QUIT_DEBUG_CARTX;
468 		  end if;
469 		end if;
470   end;
471 
472   PROCEDURE tgt_exec_block(source varchar2) is
473   PipeName  varchar2(30);
474   res				varchar2(500);
475   ack       integer;
476   status    integer;
477   SessId    varchar2(30);
478   begin
479   	getInternalDebugSession(PipeName);
480   	status := sys.dbms_pipe.create_pipe(PipeName, 8192, FALSE);
481 	  if (status = 0) then
482 	  	sys.dbms_pipe.Pack_Message(source);
483 	  	status := sys.dbms_pipe.Send_Message(PipeName, 3600);
484 	  	ack := waitForAck(PipeName || 'Res', res);
485 
486       if (ack < DEBUG_CARTX_SUCCESS) then
487       	 getDebugSessionId(SessId);
488       	 handleAborts(ack, Sessid);
489       	 cleanup_pipes(PipeName);
490       	 raise PDE_QUIT_DEBUG_CARTX;
491 		  end if;
492 	  end if;
493   end;
494 
495   PROCEDURE tgt_fetch_results(sDownloadFileList out varchar2,
496                               nCompress out number) is
497  	pipeName   varchar2(30);
498  	resPipeName  varchar2(30);
499  	status     integer;
500  	numLines   integer;
501  	msg        varchar2(256);
502  	numMsg		 integer;
503  	msgType		 integer;
504  	ack        integer;
505  	SessId     varchar2(30);
506  	begin
507 
508  		getInternalDebugSession(pipeName);
509 	 	resPipeName := pipeName || 'Res';
510 
511     insertmessage ('Waiting in fetch results.');
512 	 	--First receive the downloadfilelist and its properties.
513 	 	status := sys.dbms_pipe.Receive_Message(resPipename);
514 		if (status = 0) then
515 		 	msgType := sys.dbms_pipe.Next_Item_Type;
516 		 	if (msgType = 9) then
517 					sys.dbms_pipe.unpack_message(sDownloadFileList);
518 		 	elsif (msgType = 6) then
519 		 		  sys.dbms_pipe.unpack_message(ack);
520 		 		  insertmessage (to_char(ack));
521 		 		  getDebugSessionId(SessId);
522       	  handleAborts(ack, Sessid);
523 		 		  cleanup_pipes(pipeName);
524 		 		  sDownloadFileList := ' ';
525 			    nCompress := -1;
526 		 		  return;
527 		 -- raise PDE_QUIT_DEBUG_CARTX;
528 		 	end if;
529 
530 
531 	 		msgType := sys.dbms_pipe.Next_Item_Type;
532 		 	if (msgType = 6) then
533 					sys.dbms_pipe.unpack_message(nCompress);
534 		 	end if;
535 
536 			sendAck(pipename, DEBUG_CARTX_SUCCESS, status);
537 		 	else
538 		 		htp.htmlopen;
539 		    htp.headopen;
540 		    htp.print('An error occurred while recieving the results. '
541                   || SQLERRM);
542 		    htp.print(htf.paragraph);
543 			  htp.bodyclose;
544 				htp.htmlclose;
545 				cleanup_pipes(pipeName);
546 			  raise PDE_CTX_TGT;
547 		end if;
548 
549 		if (sDownloadFileList IS NULL) then
550 				sDownloadFileList := ' ';
551 			 nCompress := -1;
552 		end if;
553 
554 		if (sDownloadFileList = DEBUG_CARTX_BYE) then
555 			cleanup_pipes(pipeName);
556 			raise PDE_QUIT_DEBUG_CARTX;
557 		end if;
558 
559 		msg := '';
560 		--Now receive the htp page data.
561 		loop
562 			status := sys.dbms_pipe.Receive_Message(resPipename);
563 		 	if (status = 0) then
564 		 		 msgType := sys.dbms_pipe.Next_Item_Type;
565 
566 		  		if (msgType = 9) then
567 								sys.dbms_pipe.unpack_message(msg);
568 								numLines := to_number(msg);
569 					elsif(msgType = 6) then
570 								sys.dbms_pipe.unpack_message(numLines);
571 		  		else
572 		  					--Weird type.  Something is wrong... Get out...
573 		  					numLines := DEBUG_CARTX_RES_OVER;
574 		  		end if;
575 
576 				  if (numLines = DEBUG_CARTX_RES_OVER) then
577 				  		sendAck(pipename, DEBUG_CARTX_SUCCESS, status);
578 				  		exit;
579 				  end if;
580 
581 					if (numLines > 0) then
582 			  			FOR counter IN 1 .. numLines LOOP
583 			  				msgType := sys.dbms_pipe.Next_Item_Type;
584 								if (msgType = 9) then
585 						  				sys.dbms_pipe.unpack_message(msg);
586 						  				htp.prn(msg);
587 								elsif(msgType = 6) then
588 											sys.dbms_pipe.unpack_message(numMsg);
589 						  				htp.prn(numMsg);
590 								else
591 										--We are into some weird msgType.  Just get out.
592 									exit;
593 								end if;
594 			  			END LOOP;
595 		  		end if;
599 		end loop;
596 						/*  Send ack for receiving a message.  */
597 		  			sendAck(pipename, DEBUG_CARTX_SUCCESS, status);
598 		 	end if;
600 
601 
602 exception
603 	when PDE_QUIT_DEBUG_CARTX then
604 	return;
605 end;
606 
607 
608   PROCEDURE dbg_init_cgi_env(SessID varchar2, status IN OUT integer,
609                              err_info IN OUT varchar2) is
610   num_params  number;
611   param_name  owa.vc_arr;
612   param_val   owa.vc_arr;
613   counter			integer;
614   msg					varchar2(100);
615   msgType			integer;
616   SessionID   varchar2(30);
617   begin
618 
619    SessionID := SessID;
620    fmtSessionId (SessionID);
621 
622 	 status := sys.dbms_pipe.receive_message(SessionID);
623 	 if (status = 0) then
624 			msgType := sys.dbms_pipe.next_item_type;
625 	  	if (msgType != 0) then
626 		  	  if (msgType = 9) then
627 			  	  	sys.dbms_pipe.unpack_message(msg);
628 					  	if (msg = DEBUG_CARTX_BYE) then
629 					  		status := -1;
630 					  	else
631 					  		num_params := to_number(msg);
632 					  	end if;
633 					  else
634 							sys.dbms_pipe.unpack_message(num_params);
635 						end if;
636 				end if;
637 	  	end if;
638 
639 			if (status = 0) then
640 				FOR counter IN 1 .. num_params LOOP
641 						if (sys.dbms_pipe.next_item_type != 0) then
642 				  				sys.dbms_pipe.unpack_message(param_name(counter));
643 				  	--else
644 				  	-- raise PDE_OUT_OF_SYNC;
645 				  	end if;
646 				END LOOP;
647 
648 				FOR counter IN 1..num_params LOOP
649 						if (sys.dbms_pipe.Next_Item_Type != 0) then
650 						  	sys.dbms_pipe.unpack_message(param_val(counter));
651 						--else
652 						--raise PDE_OUT_OF_SYNC;
653 						end if;
654 				END LOOP;
655 				-- insertmessage('Trying to initialize cgi environment.');
656 				owa.init_cgi_env(num_params, param_name, param_val);
657 
658 	  		msg := DEBUG_CARTX_SUCCESS;
659 			else
660 				msg := DEBUG_CARTX_BYE;
661 
662 			end if;  --end of if status = 0
663 			-- insertmessage('Sending ack' || SessionID || 'Res');
664 	  	sendAck(SessionID || 'Res', msg, status);
665 
666   end;
667 
668   PROCEDURE dbg_send_results(SessID varchar2, status IN OUT integer,
669                              filename varchar2, mimetype number,
670                              err_info IN varchar2) IS
671   resPipeName   varchar2(30);
672   Lines					htp.htbuf_arr;
673   numLines			integer;
674   sDownloadFile varchar2(256);
675   nCompress     number;
676   res		varchar2(256);
677   ack           integer;
678   locStatus     integer;
679   SessionID     varchar2(30);
680   begin
681 
682     SessionID := SessID;
683     fmtSessionId (SessionID);
684 
685 	locstatus := 0;
686   	resPipeName := SessionID || 'Res';
687   	locstatus := sys.dbms_pipe.create_pipe(resPipeName, 8192, FALSE);
688 
689 	  if (locstatus = 0) then
690 
691 		  --if there is an error, poppulate the HTML array with error info
692 		  --and the rest will be taken care of.
693 
694 		  if (status != 0) then
695 			-- insertmessage(err_info);
696 					htp.htmlopen;
697 			  	htp.headopen;
698 					htp.title('Error: ' || to_char(status));
699 					htp.headclose;
700 					htp.bodyopen;
701 					if (err_info IS NULL) then
702 						htp.print('Error occurred');
703 					else
704 					  htp.print(err_info);
705 					end if;
706 					htp.print(htf.paragraph);
707 			  	htp.bodyclose;
708 				  htp.htmlclose;
709 		  end if;
710 
711 			if (filename IS NULL) then
712 					sDownloadFile := 'CARTX_NULL';
713 					nCompress := -1;
714 			else
715 					sDownloadFile := filename;
716 					nCompress := mimetype;
717 			end if;
718 
719 				-- insertmessage('Sending file :' || sDownloadFile
720         --               || ':And its compress code is ' || to_char(nCompress));
721 				sys.dbms_pipe.Pack_Message(sDownloadFile);
722 				sys.dbms_pipe.Pack_Message(nCompress);
723 
724 				locstatus := sys.dbms_pipe.Send_Message(resPipeName, 3600);
725 				ack := waitForAck(SessionID, res);
726 
727       if (ack < DEBUG_CARTX_SUCCESS) then
728       	 handleAborts(ack, SessID);
729 		     cleanup_pipes(SessionID);
730 		     raise PDE_QUIT_DEBUG_CARTX;
731 		  end if;
732 
733 				--Now send the htp page.
734 		  	loop
735 			  	numLines := 10;
736 		  		owa.get_page(Lines, numLines);
737 
738 					if (numLines = 0) then
739 						numLines := DEBUG_CARTX_RES_OVER;
740 					end if;
741 
742 			  		if (numLines > 0) then
743 			  			/*  Only if we have message to send, send! */
744 			  			sys.dbms_pipe.Pack_Message(numLines);
745 
746 							for counter in 1 .. numLines loop
747 					  		sys.dbms_pipe.Pack_Message(Lines(counter));
748 					  		-- insertmessage('sending msg ' || Lines(counter));
749 							end loop;
750 
751 			  			locstatus := sys.dbms_pipe.Send_Message(resPipeName, 3600);
752 			  			ack := waitForAck(SessionID, res);
753 
754 				      if (ack < DEBUG_CARTX_SUCCESS) then
755 				      	 handleAborts(ack, SessID);
756 				      	 cleanup_pipes(SessionID);
757 				      	 raise PDE_QUIT_DEBUG_CARTX;
758 						  end if;
759 			  		end if;
760 
761 			  	if (numLines < 10) then
762 			  		numLines := DEBUG_CARTX_RES_OVER;
763 			  		sys.dbms_pipe.Pack_Message(numLines);
764 			  		locstatus := sys.dbms_pipe.Send_Message(resPipeName, 3600);
765 			  		ack := waitForAck(SessionID, res);
766 
767 		        if (ack < DEBUG_CARTX_SUCCESS) then
771 				    end if;
768 		      	   handleAborts(ack, SessID);
769 		      	   cleanup_pipes(SessionID);
770 		      	   raise PDE_QUIT_DEBUG_CARTX;
772 			  		exit;
773 			  	end if;
774 		  	end loop;
775 	  end if;
776 
777 	  cleanup_pipes(SessionID);
778 	  status := locstatus;
779 
780   end;
781 
782   PROCEDURE dbg_Exec_proc(SessID varchar2, procname out varchar2) is
783   status  integer;
784   msg 		varchar2(100);
785   msgType	integer;
786   SessionID    varchar2(30);
787   begin
788 
789       SessionID := SessID;
790       fmtSessionId (SessionID);
791   		status := sys.dbms_pipe.Receive_Message(SessionID);
792 
793 	  	if (status = 0) then
794 				msgType := sys.dbms_pipe.Next_Item_Type;
795 				if ( msgType = 9) then
796 							sys.dbms_pipe.unpack_message(procname);
797 				else
798 					DBMS_OUTPUT.PUT_LINE('Weird Message Type ' || to_char(msgType));
799 				end if;
800 				msg := DEBUG_CARTX_SUCCESS;
801 
802 	  	  sendAck(SessionID || 'Res', msg, status);
803 		  end if;
804 
805   end;
806 
807 
808 
809 	procedure receiveAStringMessage(SessionID varchar2, status Out integer,
810                                   msg  out varchar2) is
811 	msgType integer;
812 	begin
813 		status := sys.dbms_pipe.Receive_Message(SessionID);
814 	  	if (status = 0) then
815 
816 				msgType := sys.dbms_pipe.Next_Item_Type;
817 				if ( msgType = 9) then
818 							sys.dbms_pipe.unpack_message(msg);
819 				--else
820 						--Wrong Message Type.
821 				end if;
822 		  --else
823 		  	--Could not able to receive a message
824 		  end if;
825 	end;
826 
827 	procedure dbg_stop_debugging(SessID varchar2, status OUT integer,
828                                stopflag integer) IS
829 	ignore  integer;
830 	SessionID  varchar2(30);
831 	begin
832 
833 		  SessionID := SessID;
834       fmtSessionId (SessionID);
835 
836 			--Send a 'bye' message to all waiting processes
837 		  sendAck(SessionId, stopflag, ignore);
838 		  sendAck(SessionId || 'Res', stopflag, ignore);
839 
840 	end;
841 
842 procedure dbg_stop_debugging(SessId varchar2, status OUT integer,
843                              stopflag integer, err_info varchar2) is
844 ignore  integer;
845 SessionID   varchar2(30);
846 	begin
847 
848 		  SessionID := SessID;
849       fmtSessionId (SessionID);
850 
851 			--Send a 'bye' message to all waiting processes
852 		  sendAck(SessionId, stopflag, err_info, ignore);
853 		  sendAck(SessionId || 'Res', stopflag, err_info, ignore);
854 
855 	end;
856 
857 	procedure dbg_get_exec_block(SessID  varchar2,
858                                status IN OUT integer,
859                                execBlock  OUT varchar2,
860                                err_info IN OUT varchar2) is
861 	source  varchar2(32736);
862 	newline	varchar2(5);
863 	SessionId varchar2(30);
864 	begin
865 
866 		 SessionID := SessID;
867      fmtSessionId (SessionID);
868 
869 			--This procedure gets called from  the built-in.
870 			execBlock := '';
871 			newline := '
872 ';
873 			--Wait on the pipename for the source.
874 				loop
875 					source := '';
876 					receiveAStringMessage(SessionID, status, source);
877 					if (status = 0) then
878 						if (substr(source, length(source), 1) = newline) then
879 							source := substr(source, 1, length(source) - 1);
880 						end if;
881 						if (source = '-- PU Name') then
882 						-- execBlock := execBlock || 'Debug.Suspend;' || newline;
883 						execBlock := execBlock || source || newline;
884 						elsif (source = 'End;') then
885 							execblock := execBlock || source;
886 							sendAck(SessionID || 'Res', DEBUG_CARTX_SUCCESS, status);
887 							exit;
888 						else
889 							execblock := execBlock || source || newline;
890 						end if;
891 						sendAck(SessionID || 'Res', DEBUG_CARTX_SUCCESS, status);
892 
893 					else
894 						sendAck(SessionID || 'Res', DEBUG_CARTX_ERR, status);
895 						exit;  --status has the error code.
896 					end if;
897 
898 				end loop;
899 	end;
900 
901   FUNCTION dbg_get_schema_name(schemaname OUT varchar2) RETURN integer is
902   begin
903   	 schemaname := owa_util.get_cgi_env( 'PLSQL_GATEWAY' );
904   	 if (schemaname = 'WebDb') then
905   		   schemaname := NULL;
906   		   schemaname := owa_util.get_cgi_env( 'DOCUMENT_SCHEMA' );
907   		   if (schemaname = '' or schemaname IS NULL) then
908   		 	    return 2;
909   		   end if;
910   	else
911   		return 1;
912   	end if;
913   	return 0;
914   end;
915 
916 END DEBUG_CARTX;
917 
918