forked from EnterpriseDB/pldebugger
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin_debugger.c
1981 lines (1642 loc) · 48.3 KB
/
plugin_debugger.c
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
* plugin_debugger.c - Language-independent parts of debugger
*
* Copyright (c) 2004-2018 EnterpriseDB Corporation. All Rights Reserved.
*
* Licensed under the Artistic License v2.0, see
* https://opensource.org/licenses/artistic-license-2.0
* for full details
*
**********************************************************************/
#include "postgres.h"
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#ifdef WIN32
#include<winsock2.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include "access/xact.h"
#include "lib/stringinfo.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#if (PG_VERSION_NUM >= 130000)
#include "common/hashfn.h"
#endif
#include "parser/parser.h"
#include "parser/parse_func.h"
#include "globalbp.h"
#include "storage/proc.h" /* For MyProc */
#include "storage/procarray.h" /* For BackendPidGetProc */
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
#include "miscadmin.h"
#include "pldebugger.h"
#include "dbgcomm.h"
/* Include header for GETSTRUCT */
#if (PG_VERSION_NUM >= 90300)
#include "access/htup_details.h"
#endif
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
#define TARGET_PROTO_VERSION "1.1"
/**********************************************************************
* Type and structure definitions
**********************************************************************/
/*
* eConnectType
*
* This enum defines the different ways that we can connect to the
* debugger proxy.
*
* CONNECT_AS_SERVER means that we create a socket, bind an address to
* to that socket, send a NOTICE to our client application, and wait for
* a debugger proxy to attach to us. That's what happens when your
* client application sets a local breakpoint and can handle the
* NOTICE that we send.
*
* CONNECT_AS_CLIENT means that a proxy has already created a socket
* and is waiting for a target (that's us) to connect to it. We do
* this kind of connection stuff when a debugger client sets a global
* breakpoint and we happen to blunder into that breakpoint.
*
* CONNECT_UNKNOWN indicates a problem, we shouldn't ever see this.
*/
typedef enum
{
CONNECT_AS_SERVER, /* Open a server socket and wait for a proxy to connect to us */
CONNECT_AS_CLIENT, /* Connect to a waiting proxy (global breakpoints do this) */
CONNECT_UNKNOWN /* Must already be connected */
} eConnectType;
/* Global breakpoint data. */
typedef struct
{
#if (PG_VERSION_NUM >= 90600)
int tranche_id;
LWLock lock;
#else
LWLockId lockid;
#endif
} GlobalBreakpointData;
/**********************************************************************
* Local (static) variables
**********************************************************************/
per_session_ctx_t per_session_ctx;
errorHandlerCtx client_lost;
static debugger_language_t *debugger_languages[] = {
&plpgsql_debugger_lang,
#ifdef INCLUDE_PACKAGE_SUPPORT
&spl_debugger_lang,
#endif
NULL
};
#if (PG_VERSION_NUM >= 150000)
static shmem_request_hook_type prev_shmem_request_hook = NULL;
#endif
/**********************************************************************
* Function declarations
**********************************************************************/
void _PG_init( void ); /* initialize this module when we are dynamically loaded */
/**********************************************************************
* Local (hidden) function prototypes
**********************************************************************/
#if (PG_VERSION_NUM >= 150000)
static void pldebugger_shmem_request( void );
#endif
static void * writen( int peer, void * src, size_t len );
static bool connectAsServer( void );
static bool connectAsClient( Breakpoint * breakpoint );
static bool handle_socket_error(void);
static bool parseBreakpoint( Oid * funcOID, int * lineNumber, char * breakpointString );
static bool addLocalBreakpoint( Oid funcOID, int lineNo );
static void reserveBreakpoints( void );
static debugger_language_t *language_of_frame(ErrorContextCallback *frame);
static char * findSource( Oid oid, HeapTuple * tup );
static void do_deposit(ErrorContextCallback *frame, debugger_language_t *lang,
char *command);
static void send_breakpoints(Oid funcOid);
static void send_stack(void);
static void select_frame(int frameNo, ErrorContextCallback **frame_p, debugger_language_t **lang_p);
/**********************************************************************
* Function definitions
**********************************************************************/
void _PG_init( void )
{
int i;
/* Initialize all the per-language hooks. */
for (i = 0; debugger_languages[i] != NULL; i++)
debugger_languages[i]->initialize();
#if (PG_VERSION_NUM >= 150000)
prev_shmem_request_hook = shmem_request_hook;
shmem_request_hook = pldebugger_shmem_request;
#else
reserveBreakpoints();
dbgcomm_reserve();
#endif
}
#if (PG_VERSION_NUM >= 150000)
static void pldebugger_shmem_request( void )
{
if (prev_shmem_request_hook)
prev_shmem_request_hook();
reserveBreakpoints();
dbgcomm_reserve();
}
#endif
/*
* CREATE OR REPLACE FUNCTION pldbg_oid_debug( functionOID OID ) RETURNS INTEGER AS 'pldbg_oid_debug' LANGUAGE C;
*/
PGDLLEXPORT Datum pldbg_oid_debug(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pldbg_oid_debug);
Datum pldbg_oid_debug(PG_FUNCTION_ARGS)
{
Oid funcOid;
HeapTuple tuple;
Oid userid;
if(( funcOid = PG_GETARG_OID( 0 )) == InvalidOid )
ereport( ERROR, ( errcode( ERRCODE_UNDEFINED_FUNCTION ), errmsg( "no target specified" )));
/* get the owner of the function */
tuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(funcOid),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for function %u",
funcOid);
userid = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
ReleaseSysCache(tuple);
if( !superuser() && (GetUserId() != userid))
ereport( ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), errmsg( "must be owner or superuser to create a breakpoint" )));
addLocalBreakpoint( funcOid, -1 );
PG_RETURN_INT32( 0 );
}
/*
* ---------------------------------------------------------------------
* readn()
*
* This function reads exactly 'len' bytes from the given socket or it
* throws an error. readn() will hang until the proper number of bytes
* have been read (or an error occurs).
*
* Note: dst must point to a buffer large enough to hold at least 'len'
* bytes. readn() returns dst (for convenience).
*/
static void * readn( int peer, void * dst, size_t len )
{
size_t bytesRemaining = len;
char * buffer = (char *)dst;
while( bytesRemaining > 0 )
{
ssize_t bytesRead = recv( peer, buffer, bytesRemaining, 0 );
if( bytesRead <= 0 && errno != EINTR )
handle_socket_error();
/* Ignore if we didn't receive anything. */
if ( bytesRead > 0 )
{
bytesRemaining -= bytesRead;
buffer += bytesRead;
}
}
return( dst );
}
/*
* ---------------------------------------------------------------------
* readUInt32()
*
* Reads a 32-bit unsigned value from the server (and returns it in the host's
* byte ordering)
*/
static uint32 readUInt32( int channel )
{
uint32 netVal;
readn( channel, &netVal, sizeof( netVal ));
return( ntohl( netVal ));
}
/*
* ---------------------------------------------------------------------
* dbg_read_str()
*
* This function reads a counted string from the given stream
* Returns a palloc'd, null-terminated string.
*
* NOTE: the server-side of the debugger uses this function to read a
* string from the client side
*/
char *dbg_read_str( void )
{
uint32 len;
char *dst;
int sock = per_session_ctx.client_r;
len = readUInt32( sock );
dst = palloc(len + 1);
readn( sock, dst, len );
dst[len] = '\0';
return dst;
}
/*
* ---------------------------------------------------------------------
* writen()
*
* This function writes exactly 'len' bytes to the given socket or it
* throws an error. writen() will hang until the proper number of bytes
* have been written (or an error occurs).
*/
static void * writen( int peer, void * src, size_t len )
{
size_t bytesRemaining = len;
char * buffer = (char *)src;
while( bytesRemaining > 0 )
{
ssize_t bytesWritten;
if(( bytesWritten = send( peer, buffer, bytesRemaining, 0 )) <= 0 )
handle_socket_error();
bytesRemaining -= bytesWritten;
buffer += bytesWritten;
}
return( src );
}
/*
* ---------------------------------------------------------------------
* sendUInt32()
*
* This function sends a uint32 value (val) to the debugger server.
*/
static void sendUInt32( int channel, uint32 val )
{
uint32 netVal = htonl( val );
writen( channel, &netVal, sizeof( netVal ));
}
/*
* ---------------------------------------------------------------------
* dbg_send()
*
* This function writes a formatted, counted string to the
* given stream. The argument list for this function is identical to
* the argument list for the fprintf() function - you provide a socket,
* a format string, and then some number of arguments whose meanings
* are defined by the format string.
*
* NOTE: the server-side of the debugger uses this function to send
* data to the client side. If the connection drops, dbg_send()
* will longjmp() back to the debugger top-level so that the
* server-side can respond properly.
*/
void dbg_send( const char *fmt, ... )
{
StringInfoData result;
char *data;
size_t remaining;
int sock = per_session_ctx.client_w;
if( !sock )
return;
initStringInfo(&result);
for (;;)
{
va_list args;
#if (PG_VERSION_NUM >= 90400)
int needed;
va_start(args, fmt);
needed = appendStringInfoVA(&result, fmt, args);
va_end(args);
if (needed == 0)
break;
enlargeStringInfo(&result, needed);
#else
bool success;
va_start(args, fmt);
success = appendStringInfoVA(&result, fmt, args);
va_end(args);
if (success)
break;
enlargeStringInfo(&result, result.maxlen);
#endif
}
data = result.data;
remaining = strlen(data);
sendUInt32(sock, remaining);
while( remaining > 0 )
{
int written = send( sock, data, remaining, 0 );
if(written < 0)
{
handle_socket_error();
continue;
}
remaining -= written;
data += written;
}
pfree(result.data);
}
/*
* ---------------------------------------------------------------------
* dbg_send_src()
*
* dbg_send_src() sends the source code for a function to the client.
*
* The client caches the source code that we send it and uses xmin/cmin
* to ensure the validity of the cache.
*/
static void dbg_send_src( char * command )
{
HeapTuple tup;
char *procSrc;
Oid targetOid = InvalidOid; /* Initialize to keep compiler happy */
targetOid = atoi( command + 2 );
/* Find the source code for this function */
procSrc = findSource( targetOid, &tup );
/* Found it - now send the source to the client */
dbg_send( "%s", procSrc );
/* Release the process tuple and send a footer to the client so he knows we're finished */
ReleaseSysCache( tup );
}
/*
* ---------------------------------------------------------------------
* findSource()
*
* This function locates and returns a pointer to a null-terminated string
* that contains the source code for the given function (identified by its
* OID).
*
* In addition to returning a pointer to the requested source code, this
* function sets *tup to point to a HeapTuple (that you must release when
* you are finished with it).
*/
static char * findSource( Oid oid, HeapTuple * tup )
{
bool isNull;
*tup = SearchSysCache( PROCOID, ObjectIdGetDatum( oid ), 0, 0, 0 );
if(!HeapTupleIsValid( *tup ))
elog( ERROR, "pldebugger: cache lookup for proc %u failed", oid );
return( DatumGetCString( DirectFunctionCall1( textout, SysCacheGetAttr( PROCOID, *tup, Anum_pg_proc_prosrc, &isNull ))));
}
/*
* ---------------------------------------------------------------------
* attach_to_proxy()
*
* This function creates a connection to the debugger client (via the
* proxy process). attach_to_proxy() will hang the PostgreSQL backend
* until the debugger client completes the connection.
*
* We start by asking the TCP/IP stack to allocate an unused port, then we
* extract the port number from the resulting socket, send the port number to
* the client application (by raising a NOTICE), and finally, we wait for the
* client to connect.
*
* We assume that the client application knows the IP address of the PostgreSQL
* backend process - if that turns out to be a poor assumption, we can include
* the IP address in the notification string that we send to the client application.
*/
bool attach_to_proxy( Breakpoint * breakpoint )
{
bool result;
errorHandlerCtx save;
if( per_session_ctx.client_w )
{
/* We're already connected to a live proxy, just go home */
return( TRUE );
}
if( breakpoint == NULL )
{
/*
* No breakpoint - that implies that we're 'stepping into'.
* We had better already have a connection to a proxy here
* (how could we be 'stepping into' if we aren't connected
* to a proxy?)
*/
return( FALSE );
}
/*
* When a networking error is detected, we longjmp() to the client_lost
* error handler - that normally points to a location inside of dbg_newstmt()
* but we want to handle any network errors that arise while we are
* setting up a link to the proxy. So, we save the original client_lost
* error handler context and push our own context on to the stack.
*/
save = client_lost;
if( sigsetjmp( client_lost.m_savepoint, 1 ) != 0 )
{
client_lost = save;
return( FALSE );
}
if( breakpoint->data.proxyPort == -1 )
{
/*
* proxyPort == -1 implies that this is a local breakpoint,
* create a server socket and wait for the proxy to contact
* us.
*/
result = connectAsServer();
}
else
{
/*
* proxyPort != -1 implies that this is a global breakpoint,
* a debugger proxy is already waiting for us at the given
* port (on this host), connect to that proxy.
*/
result = connectAsClient( breakpoint );
}
/*
* Now restore the original error handler context so that
* dbg_newstmt() can handle any future network errors.
*/
client_lost = save;
return( result );
}
/*
* ---------------------------------------------------------------------
* connectAsServer()
*
* This function creates a socket, asks the TCP/IP stack to bind it to
* an unused port, and then waits for a debugger proxy to connect to
* that port. We send a NOTICE to our client process (on the other
* end of the fe/be connection) to let the client know that it should
* fire up a debugger and attach to that port (the NOTICE includes
* the port number)
*/
static bool connectAsServer( void )
{
int client_sock;
client_sock = dbgcomm_listen_for_proxy();
if (client_sock < 0)
{
per_session_ctx.client_w = per_session_ctx.client_r = 0;
return( FALSE );
}
else
{
per_session_ctx.client_w = client_sock;
per_session_ctx.client_r = client_sock;
return( TRUE );
}
}
/*
* ---------------------------------------------------------------------
* connectAsClient()
*
* This function connects to a waiting proxy process over the given
* port. We got the port number from a global breakpoint (the proxy
* stores it's port number in the breakpoint so we'll know how to
* find that proxy).
*/
static bool connectAsClient( Breakpoint * breakpoint )
{
int proxySocket;
proxySocket = dbgcomm_connect_to_proxy(breakpoint->data.proxyPort);
if (proxySocket < 0 )
{
/* dbgcomm_connect_to_proxy already logged the reason */
return false;
}
else
{
per_session_ctx.client_w = proxySocket;
per_session_ctx.client_r = proxySocket;
BreakpointBusySession( breakpoint->data.proxyPid );
return true;
}
}
/*
* ---------------------------------------------------------------------
* parseBreakpoint()
*
* Given a string that formatted like "funcOID:linenumber",
* this function parses out the components and returns them to the
* caller. If the string is well-formatted, this function returns
* TRUE, otherwise, we return FALSE.
*/
static bool parseBreakpoint( Oid * funcOID, int * lineNumber, char * breakpointString )
{
int a, b;
int n;
n = sscanf(breakpointString, "%d:%d", &a, &b);
if (n == 2)
{
*funcOID = a;
*lineNumber = b;
}
else
return false;
return( TRUE );
}
/*
* ---------------------------------------------------------------------
* addLocalBreakpoint()
*
* This function adds a local breakpoint for the given function and
* line number
*/
static bool addLocalBreakpoint( Oid funcOID, int lineNo )
{
Breakpoint breakpoint;
breakpoint.key.databaseId = MyProc->databaseId;
breakpoint.key.functionId = funcOID;
breakpoint.key.lineNumber = lineNo;
breakpoint.key.targetPid = MyProc->pid;
breakpoint.data.isTmp = FALSE;
breakpoint.data.proxyPort = -1;
breakpoint.data.proxyPid = -1;
return( BreakpointInsert( BP_LOCAL, &breakpoint.key, &breakpoint.data ));
}
/*
* ---------------------------------------------------------------------
* setBreakpoint()
*
* The debugger client can set a local breakpoint at a given
* function/procedure and line number by calling this function
* (through the debugger proxy process).
*/
void setBreakpoint( char * command )
{
/*
* Format is 'b funcOID:lineNumber'
*/
int lineNo;
Oid funcOID;
if( parseBreakpoint( &funcOID, &lineNo, command + 2 ))
{
if( addLocalBreakpoint( funcOID, lineNo ))
dbg_send( "%s", "t" );
else
dbg_send( "%s", "f" );
}
else
{
dbg_send( "%s", "f" );
}
}
/*
* ---------------------------------------------------------------------
* clearBreakpoint()
*
* This function deletes the breakpoint at the package,
* function/procedure, and line number indicated by the
* given command.
*
* For now, we maintain our own private list of breakpoints -
* later, we'll use the same list managed by the CREATE/
* DROP BREAKPOINT commands.
*/
void clearBreakpoint( char * command )
{
/*
* Format is 'f funcOID:lineNumber'
*/
int lineNo;
Oid funcOID;
if( parseBreakpoint( &funcOID, &lineNo, command + 2 ))
{
Breakpoint breakpoint;
breakpoint.key.databaseId = MyProc->databaseId;
breakpoint.key.functionId = funcOID;
breakpoint.key.lineNumber = lineNo;
breakpoint.key.targetPid = MyProc->pid;
if( BreakpointDelete( BP_LOCAL, &breakpoint.key ))
dbg_send( "t" );
else
dbg_send( "f" );
}
else
{
dbg_send( "f" );
}
}
bool breakAtThisLine( Breakpoint ** dst, eBreakpointScope * scope, Oid funcOid, int lineNumber )
{
BreakpointKey key;
key.databaseId = MyProc->databaseId;
key.functionId = funcOid;
key.lineNumber = lineNumber;
if( per_session_ctx.step_into_next_func )
{
*dst = NULL;
*scope = BP_LOCAL;
return( TRUE );
}
/*
* We conduct 3 searches here.
*
* First, we look for a global breakpoint at this line, targeting our
* specific backend process.
*
* Next, we look for a global breakpoint (at this line) that does
* not target a specific backend process.
*
* Finally, we look for a local breakpoint at this line (implicitly
* targeting our specific backend process).
*
* NOTE: We must do the local-breakpoint search last because, when the
* proxy attaches to our process, it marks all of its global
* breakpoints as busy (so other potential targets will ignore
* those breakpoints) and we copy all of those global breakpoints
* into our local breakpoint hash. If the debugger client exits
* and the user starts another debugger session, we want to see the
* new breakpoints instead of our obsolete local breakpoints (we
* don't have a good way to detect that the proxy has disconnected
* until it's inconvenient - we have to read-from or write-to the
* proxy before we can tell that it's died).
*/
key.targetPid = MyProc->pid; /* Search for a global breakpoint targeted at our process ID */
if((( *dst = BreakpointLookup( BP_GLOBAL, &key )) != NULL ) && ((*dst)->data.busy == FALSE ))
{
*scope = BP_GLOBAL;
return( TRUE );
}
key.targetPid = -1; /* Search for a global breakpoint targeted at any process ID */
if((( *dst = BreakpointLookup( BP_GLOBAL, &key )) != NULL ) && ((*dst)->data.busy == FALSE ))
{
*scope = BP_GLOBAL;
return( TRUE );
}
key.targetPid = MyProc->pid; /* Search for a local breakpoint (targeted at our process ID) */
if(( *dst = BreakpointLookup( BP_LOCAL, &key )) != NULL )
{
*scope = BP_LOCAL;
return( TRUE );
}
return( FALSE );
}
bool breakpointsForFunction( Oid funcOid )
{
if( BreakpointOnId( BP_LOCAL, funcOid ) || BreakpointOnId( BP_GLOBAL, funcOid ))
return( TRUE );
else
return( FALSE );
}
/* ---------------------------------------------------------------------
* handle_socket_error()
*
* when invoked after a socket operation it would check socket operation's
* last error status and invoke siglongjmp incase the error is fatal.
*/
static bool handle_socket_error(void)
{
int err;
bool fatal_err = TRUE;
#ifdef WIN32
err = WSAGetLastError();
switch(err)
{
case WSAEINTR:
case WSAEBADF:
case WSAEACCES:
case WSAEFAULT:
case WSAEINVAL:
case WSAEMFILE:
/*
* Windows Sockets definitions of regular Berkeley error constants
*/
case WSAEWOULDBLOCK:
case WSAEINPROGRESS:
case WSAEALREADY:
case WSAENOTSOCK:
case WSAEDESTADDRREQ:
case WSAEMSGSIZE:
case WSAEPROTOTYPE:
case WSAENOPROTOOPT:
case WSAEPROTONOSUPPORT:
case WSAESOCKTNOSUPPORT:
case WSAEOPNOTSUPP:
case WSAEPFNOSUPPORT:
case WSAEAFNOSUPPORT:
case WSAEADDRINUSE:
case WSAEADDRNOTAVAIL:
case WSAENOBUFS:
case WSAEISCONN:
case WSAENOTCONN:
case WSAETOOMANYREFS:
case WSAETIMEDOUT:
case WSAELOOP:
case WSAENAMETOOLONG:
case WSAEHOSTUNREACH:
case WSAENOTEMPTY:
case WSAEPROCLIM:
case WSAEUSERS:
case WSAEDQUOT:
case WSAESTALE:
case WSAEREMOTE:
/*
* Extended Windows Sockets error constant definitions
*/
case WSASYSNOTREADY:
case WSAVERNOTSUPPORTED:
case WSANOTINITIALISED:
case WSAEDISCON:
case WSAENOMORE:
case WSAECANCELLED:
case WSAEINVALIDPROCTABLE:
case WSAEINVALIDPROVIDER:
case WSAEPROVIDERFAILEDINIT:
case WSASYSCALLFAILURE:
case WSASERVICE_NOT_FOUND:
case WSATYPE_NOT_FOUND:
case WSA_E_NO_MORE:
case WSA_E_CANCELLED:
case WSAEREFUSED:
break;
/*
* Server should shut down its socket on these errors.
*/
case WSAENETDOWN:
case WSAENETUNREACH:
case WSAENETRESET:
case WSAECONNABORTED:
case WSAESHUTDOWN:
case WSAEHOSTDOWN:
case WSAECONNREFUSED:
case WSAECONNRESET:
fatal_err = TRUE;
break;
default:
;
}
if(fatal_err)
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0, NULL );
elog(COMMERROR,"%s", (char *)lpMsgBuf);
LocalFree(lpMsgBuf);
siglongjmp(client_lost.m_savepoint, 1);
}
#else
err = errno;
switch(err)
{
case EINTR:
case ECONNREFUSED:
case EPIPE:
case ENOTCONN:
fatal_err = TRUE;
break;
case ENOTSOCK:
case EAGAIN:
case EFAULT:
case ENOMEM:
case EINVAL:
default:
break;
}
if(fatal_err)
{
if(( err ) && ( err != EPIPE ))
elog(COMMERROR, "%s", strerror(err));
siglongjmp(client_lost.m_savepoint, 1);
}
errno = err;
#endif
return fatal_err;
}
/*
* Returns true if we continue stepping in this frame. False otherwise.
*/
bool
plugin_debugger_main_loop(void)
{
ErrorContextCallback *frame;
debugger_language_t *lang; /* language of the selected frame */
bool need_more = TRUE;
char *command;
bool retval = TRUE;
/* Initially, set focus on the topmost frame in the stack */
for( frame = error_context_stack; frame; frame = frame->previous )
{
/*
* ignore unrecognized stack frames.
*/
lang = language_of_frame(frame);
if (lang)
break;
}
if (frame == NULL)
{
/*
* Oops, couldn't find a frame that we recognize in the stack. This
* shouldn't happen since we're stopped at a breakpoint.
*/
elog(WARNING, "could not find PL/pgSQL frame at the top of the stack");
return false;
}
/* Report the current location */
lang->send_cur_line(frame);
/*
* Loop through the following chunk of code until we get a command
* from the user that would let us execute this PL/pgSQL statement.
*/
while( need_more )
{
/* Wait for a command from the debugger client */