-
Notifications
You must be signed in to change notification settings - Fork 19
/
pldbgapi.c
1445 lines (1197 loc) · 47 KB
/
pldbgapi.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
/*
* pldbgapi.c
*
* This module defines (and implements) an API for debugging PL
* functions and procedures (in particular, functions and procedures
* written in PL/pgSQL or edb-spl).
*
* To debug a function or procedure, you need two backend processes
* plus a debugger client (the client could be a command-line client
* such as psql but is more likely a graphical client such as pgAdmin).
*
* The first backend is called the target - it's the process that's
* running the code that you want to debug.
*
* The second backend is a 'proxy' process that shuttles data between
* the debugger client and the target. The functions implemented in
* this module are called 'proxy functions'.
*
* The proxy process provides an easy and secure way for the debugger
* client to connect to the target - the client opens a normal
* libpq-style connection that (presumably) knows how to work it's
* way through a firewall and through the authentication maze (once
* the connection process completes, the debugger client is connected
* to the proxy).
*
* The debugger client can call any of the functions in this API.
* Each function is executed by the proxy process. The proxy
* shuttles debugging requests (like 'step into' or 'show call
* stack') to the debugger server (running inside of the target
* process) and sends the results back to the debugger client.
*
* There are a few basic rules for using this API:
*
* You must call one of the connection functions before you can do
* anything else (at this point, the only connection function is
* 'pldbg_attach_to_port()', but we'll add more as soon as we
* implement global breakpoints). Each connection function returns
* a session ID that identifies that debugging session (a debugger
* client can maintain multiple simultaneous sessions by keeping
* track of each session identifier). You pass that session ID
* to all of the other proxy functions.
*
* Once you have opened a session, you must wait for the target
* to reach a breakpoint (it may already be stopped at a breakpoint)
* by calling pldbg_wait_for_breakpoint( sessionID ) - that function
* will hang until the target reaches a breakpoint (or the target
* session ends).
*
* When the target pauses, you can interact with the debugger server
* (running inside of the target process) by calling any of the other
* proxy functions. For example, to tell the target to "step into" a
* function/procedure call, you would call pldbg_step_into() (and that
* function would hang until the target pauses). To tell the target
* to continue until the next breakpoint, you would call
* pldbg_continue() (and, again, that function would hang until the
* target pauses).
*
* Each time the target pauses, it returns a tuple of type 'breakpoint'.
* That tuple contains the OID of the function that the target has paused
* in, and the line number at which the target has paused. The fact that the
* target returns a tuple of type breakpoint does not imply that the target
* has paused at a breakpoint - it may have paused because of a step-over or
* step-into operation.
*
* When the target is paused at a breakpoint (or has paused after
* a step-over or step-into), you can interrogate the target by calling
* pldbg_get_stack(), pldbg_get_source(), pldbg_get_breakpoints(), or
* pldbg_get_variables().
*
* The debugger server groks the PL call stack and maintains a
* 'focus' frame. By default, the debugger server focuses on the most
* deeply nested frame (because that's the code that's actually
* running). You can shift the debugger's focus to a different frame
* by calling pldbg_select_frame().
*
* The focus is important because many functions (such as
* pldbg_get_variables()) work against the stack frame that has the focus.
*
* Any of the proxy functions may throw an error - in particular, a proxy
* function will throw an error if the target process ends. You're most
* likely to encounter an error when you call pldbg_continue() and the
* target process runs to completion (without hitting another breakpoint)
*
* Copyright (c) 2004-2024 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 "funcapi.h"
#include "utils/memutils.h"
#include "utils/builtins.h"
#include "storage/ipc.h" /* For on_shmem_exit() */
#include "storage/proc.h" /* For MyProc */
#include "libpq/libpq-be.h" /* For Port */
#include "miscadmin.h" /* For MyProcPort */
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "access/htup.h" /* For heap_form_tuple() */
#include "access/hash.h" /* For dynahash stuff */
#include <errno.h>
#include <unistd.h> /* For close() */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "globalbp.h"
#include "dbgcomm.h"
/* Include header for GETSTRUCT */
#if (PG_VERSION_NUM >= 90300)
#include "access/htup_details.h"
#endif
#if PG_VERSION_NUM >= 110000
#ifndef TRUE
#define TRUE true
#endif
#ifndef FALSE
#define FALSE false
#endif
#endif
/*
* Let the PG module loader know that we are compiled against
* the right version of the PG header files
*/
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/*******************************************************************************
* Proxy functions
*******************************************************************************/
PG_FUNCTION_INFO_V1( pldbg_attach_to_port ); /* Attach to debugger server at the given port */
PG_FUNCTION_INFO_V1( pldbg_wait_for_breakpoint ); /* Wait for the target to reach a breakpoint */
PG_FUNCTION_INFO_V1( pldbg_step_into ); /* Steop into a function/procedure call */
PG_FUNCTION_INFO_V1( pldbg_step_over ); /* Step over a function/procedure call */
PG_FUNCTION_INFO_V1( pldbg_continue ); /* Continue execution until next breakpoint */
PG_FUNCTION_INFO_V1( pldbg_get_source ); /* Get the source code for a function/procedure */
PG_FUNCTION_INFO_V1( pldbg_get_breakpoints ); /* SHOW BREAKPOINTS equivalent (deprecated) */
PG_FUNCTION_INFO_V1( pldbg_get_variables ); /* Get a list of variable names/types/values */
PG_FUNCTION_INFO_V1( pldbg_get_stack ); /* Get the call stack from the target */
PG_FUNCTION_INFO_V1( pldbg_set_breakpoint ); /* CREATE BREAKPOINT equivalent (deprecated) */
PG_FUNCTION_INFO_V1( pldbg_drop_breakpoint ); /* DROP BREAKPOINT equivalent (deprecated) */
PG_FUNCTION_INFO_V1( pldbg_select_frame ); /* Change the focus to a different stack frame */
PG_FUNCTION_INFO_V1( pldbg_deposit_value ); /* Change the value of an in-scope variable */
PG_FUNCTION_INFO_V1( pldbg_abort_target ); /* Abort execution of the target - throws error */
PG_FUNCTION_INFO_V1( pldbg_get_proxy_info ); /* Get server version, proxy API version, ... */
PG_FUNCTION_INFO_V1( pldbg_create_listener ); /* Create a listener for global breakpoints */
PG_FUNCTION_INFO_V1( pldbg_wait_for_target ); /* Wait for a global breakpoint to fire */
PG_FUNCTION_INFO_V1( pldbg_set_global_breakpoint ); /* Create a global breakpoint */
/*******************************************************************************
* Structure debugSession
*
* A debugger client may attach to many target sessions at the same time. We
* keep track of each connection in a debugSession structure. When the client
* makes a connection, we allocate a new debugSession structure and return
* a handle to that structure to the caller. He gives us back the handle
* whenever he calls another proxy function. A handle is just a smallish
* integer value that we use to track each session - we use a hash to map
* handles into debugSession pointers.
*/
typedef struct
{
int serverSocket; /* Socket connected to the debugger server */
int serverPort; /* Port number where debugger server is listening */
int listener; /* Socket where we wait for global breakpoints */
char *breakpointString;
} debugSession;
/*******************************************************************************
* Stucture sessionHashEntry
*
* As mentioned above (see debugSession), a debugger proxy can manage many
* debug sessions at once. To keep track of each session, we create a
* debugSession object and return a handle to that object to the caller. The
* handle is an opaque value - it's just an integer value. To convert a
* handle into an actual debugSession pointer, we create a hash that maps
* handles into debugSession pointers.
*
* Each member of the hash is shaped like a sessionHashEntry object.
*/
typedef int32 sessionHandle;
typedef struct
{
sessionHandle m_handle;
debugSession *m_session;
} sessionHashEntry;
static debugSession * mostRecentSession;
static HTAB * sessionHash;
/*******************************************************************************
* The following symbols represent the magic strings that we send to the
* debugger server running in the target process
*/
#define PLDBG_GET_VARIABLES "i\n"
#define PLDBG_GET_BREAKPOINTS "l\n"
#define PLDBG_GET_STACK "$\n"
#define PLDBG_STEP_INTO "s\n"
#define PLDBG_STEP_OVER "o\n"
#define PLDBG_CONTINUE "c\n"
#define PLDBG_ABORT "x"
#define PLDBG_SELECT_FRAME "^" /* Followed by frame number */
#define PLDBG_SET_BREAKPOINT "b" /* Followed by pkgoid:funcoid:linenumber */
#define PLDBG_CLEAR_BREAKPOINT "f" /* Followed by pkgoid:funcoid:linenumber */
#define PLDBG_GET_SOURCE "#" /* Followed by pkgoid:funcoid */
#define PLDBG_DEPOSIT "d" /* Followed by var.line=value */
#define PLDBG_STRING_MAX_LEN 128
#define PROXY_API_VERSION 3 /* API version number */
/*******************************************************************************
* We currently define three PostgreSQL data types (all tuples) - the following
* symbols correspond to the names for those types.
*/
#define TYPE_NAME_BREAKPOINT "breakpoint" /* May change to pldbg.breakpoint later */
#define TYPE_NAME_FRAME "frame" /* May change to pldbg.frame later */
#define TYPE_NAME_VAR "var" /* May change to pldbg.var later */
#define GET_STR( textp ) DatumGetCString( DirectFunctionCall1( textout, PointerGetDatum( textp )))
#define PG_GETARG_SESSION( n ) (sessionHandle)PG_GETARG_UINT32( n )
Datum pldbg_select_frame( PG_FUNCTION_ARGS );
Datum pldbg_attach_to_port( PG_FUNCTION_ARGS );
Datum pldbg_get_source( PG_FUNCTION_ARGS );
Datum pldbg_get_breakpoints( PG_FUNCTION_ARGS );
Datum pldbg_get_variables( PG_FUNCTION_ARGS );
Datum pldbg_get_stack( PG_FUNCTION_ARGS );
Datum pldbg_wait_for_breakpoint( PG_FUNCTION_ARGS );
Datum pldbg_set_breakpoint( PG_FUNCTION_ARGS );
Datum pldbg_drop_breakpoint( PG_FUNCTION_ARGS );
Datum pldbg_step_into( PG_FUNCTION_ARGS );
Datum pldbg_step_over( PG_FUNCTION_ARGS );
Datum pldbg_continue( PG_FUNCTION_ARGS );
Datum pldbg_deposit_value( PG_FUNCTION_ARGS );
Datum pldbg_get_proxy_info( PG_FUNCTION_ARGS );
Datum pldbg_get_pkg_cons( PG_FUNCTION_ARGS );
Datum pldbg_abort_target( PG_FUNCTION_ARGS );
Datum pldbg_create_listener( PG_FUNCTION_ARGS );
Datum pldbg_wait_for_target( PG_FUNCTION_ARGS );
Datum pldbg_set_global_breakpoint( PG_FUNCTION_ARGS );
/************************************************************
* Local function forward declarations
************************************************************/
static char * tokenize( char * src, const char * delimiters, char ** ctx );
static void * readn( int serverHandle, void * dst, size_t len );
static void * writen( int serverHandle, void * dst, size_t len );
static void sendBytes( debugSession * session, void * src, size_t len );
static void sendUInt32( debugSession * session, uint32 val );
static void sendString( debugSession * session, char * src );
static bool getBool( debugSession * session );
static uint32 getUInt32( debugSession * session );
static char * getNString( debugSession * session );
static void initializeModule( void );
static void cleanupAtExit( int code, Datum arg );
static void initSessionHash();
static debugSession * defaultSession( sessionHandle handle );
static sessionHandle addSession( debugSession * session );
static debugSession * findSession( sessionHandle handle );
static TupleDesc getResultTupleDesc( FunctionCallInfo fcinfo );
/*******************************************************************************
* Exported functions
*******************************************************************************/
/*******************************************************************************
* pldbg_attach_to_port( portNumber INTEGER ) RETURNS INTEGER
*
* This function attaches to a debugging target listening on the given port. A
* debugger client should invoke this function in response to a PLDBGBREAK
* NOTICE (the notice contains the port number that you should connect to).
*
* This function returns a session handle that identifies this particular debug
* session. When you call any of the other pldbg functions, you must supply
* the session handle returned by pldbg_attach_to_port().
*
* A given debugger client can maintain multiple simultaneous sessions
* by calling pldbg_attach_to_port() many times (with different port
* numbers) and keeping track of the returned session handles.
*/
Datum pldbg_attach_to_port( PG_FUNCTION_ARGS )
{
int32 targetBackend = PG_GETARG_INT32( 0 );
debugSession *session;
initializeModule();
session = MemoryContextAllocZero( TopMemoryContext, sizeof( *session ));
session->listener = -1;
session->serverSocket = dbgcomm_connect_to_target(targetBackend);
if (session->serverSocket < 0)
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("could not connect to debug target")));
/*
* After the handshake, the target process will send us information about
* the local breakpoint that it hit. Read it. We will hand it to the client
* if it calls wait_for_breakpoint().
*/
session->breakpointString = MemoryContextStrdup(TopMemoryContext,
getNString(session));
/*
* For convenience, remember the most recent session - if you call
* another pldbg_xxx() function with sessionHandle = 0, we'll use
* the most recent session.
*/
mostRecentSession = session;
PG_RETURN_INT32(addSession(session));
}
Datum pldbg_create_listener( PG_FUNCTION_ARGS )
{
debugSession * session = MemoryContextAllocZero( TopMemoryContext, sizeof( *session ));
initializeModule();
session->listener = dbgcomm_listen_for_target(&session->serverPort);
session->serverSocket = -1;
mostRecentSession = session;
PG_RETURN_INT32( addSession( session ));
}
/*******************************************************************************
* pldbg_wait_for_target( ) RETURNS INTEGER
*
* This function advertises the proxy process as an active debugger, waiting
* for global breakpoints.
*
* This function returns a session handle that identifies this particular debug
* session. When you call any of the other pldbg functions, you must supply
* this session handle.
*
* A given debugger client can maintain multiple simultaneous sessions
* by calling pldbg_attach_to_port() many times (with different port
* numbers) and keeping track of the returned session handles.
*/
Datum pldbg_wait_for_target( PG_FUNCTION_ARGS )
{
debugSession *session = defaultSession(PG_GETARG_SESSION( 0 ));
int serverSocket;
int serverPID;
/*
* Now mark all of our global breakpoints as 'available' (that is, not
* busy)
*/
BreakpointFreeSession( MyProc->pid );
serverSocket = dbgcomm_accept_target(session->listener, &serverPID);
if (serverSocket < 0)
ereport(ERROR,
(errmsg("could not accept a connection from debugging target")));
session->serverSocket = serverSocket;
/*
* After the handshake, the target process will send us information about
* the local breakpoint that it hit. Read it. We will hand it to the client
* if it calls wait_for_breakpoint().
*/
session->breakpointString = MemoryContextStrdup(TopMemoryContext,
getNString(session));
PG_RETURN_UINT32( serverPID );
}
/*******************************************************************************
* pldbg_set_global_breakpoint(sessionID INT, function OID, lineNumber INT)
* RETURNS boolean
*
* This function registers a breakpoint in the global breakpoint table.
*/
Datum pldbg_set_global_breakpoint( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
Breakpoint breakpoint;
if( !superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be a superuser to create a breakpoint")));
if( session->listener == -1 )
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("given session is not a listener")));
breakpoint.key.databaseId = MyProc->databaseId;
breakpoint.key.functionId = PG_GETARG_OID( 1 );
if( PG_ARGISNULL( 2 ))
breakpoint.key.lineNumber = -1;
else
breakpoint.key.lineNumber = PG_GETARG_INT32( 2 );
if( PG_ARGISNULL( 3 ))
breakpoint.key.targetPid = -1;
else
breakpoint.key.targetPid = PG_GETARG_INT32( 3 );
breakpoint.data.isTmp = TRUE;
breakpoint.data.proxyPort = session->serverPort;
breakpoint.data.proxyPid = MyProc->pid;
if( !BreakpointInsert( BP_GLOBAL, &breakpoint.key, &breakpoint.data ))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_IN_USE),
errmsg("another debugger is already waiting for that breakpoint")));
PG_RETURN_BOOL( true );
}
/*******************************************************************************
* pldbg_wait_for_breakpoint( sessionID INTEGER ) RETURNS breakpoint
*
* This function waits for the debug target to reach a breakpoint. You should
* call this function immediately after pldbg_attach_to_port() returns a
* session ID. pldbg_wait_for_breakpoint() is nearly identical to
* pldbg_step_into(), pldbg_step_over(), and pldbg_continue(), (they all wait
* for the target) but this function does not send a command to the target
* first.
*
* This function returns a tuple of type 'breakpoint' - such a tuple contains
* the function OID and line number where the target is currently stopped.
*/
static Datum buildBreakpointDatum( char * breakpointString )
{
char * values[3];
char * ctx = NULL;
HeapTuple result;
TupleDesc tupleDesc = RelationNameGetTupleDesc( TYPE_NAME_BREAKPOINT );
values[0] = tokenize( breakpointString, ":", &ctx ); /* function OID */
values[1] = tokenize( NULL, ":", &ctx ); /* linenumber */
values[2] = tokenize( NULL, ":", &ctx ); /* targetName */
result = BuildTupleFromCStrings( TupleDescGetAttInMetadata( tupleDesc ), values );
return( HeapTupleGetDatum( result ));
}
Datum pldbg_wait_for_breakpoint( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
char * breakpointString;
if (!session->breakpointString)
PG_RETURN_NULL();
breakpointString = pstrdup(session->breakpointString);
pfree(session->breakpointString);
session->breakpointString = NULL;
PG_RETURN_DATUM( buildBreakpointDatum( breakpointString ));
}
/*******************************************************************************
* pldbg_step_into( sessionID INTEGER ) RETURNS breakpoint
*
* This function sends a "step/into" command to the debugger target and then
* waits for target to reach the next executable statement.
*
* This function returns a tuple of type 'breakpoint' that contains the
* function OID and line number where the target is currently stopped.
*/
Datum pldbg_step_into( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
sendString( session, PLDBG_STEP_INTO );
PG_RETURN_DATUM( buildBreakpointDatum( getNString( session )));
}
/*******************************************************************************
* pldbg_step_over( sessionID INTEGER ) RETURNS breakpoint
*
* This function sends a "step/over" command to the debugger target and then
* waits for target to reach the next executable statement within the current
* function. If the target encounters a breakpoint (presumably in a child
* invocation) before reaching the next executable line, it will stop at the
* breakpoint.
*
* This function returns a tuple of type 'breakpoint' that contains the
* function OID and line number where the target is currently stopped.
*/
Datum pldbg_step_over( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
sendString( session, PLDBG_STEP_OVER );
PG_RETURN_DATUM( buildBreakpointDatum( getNString( session )));
}
/*******************************************************************************
* pldbg_continue( sessionID INTEGER ) RETURNS breakpoint
*
* This function sends a "continue" command to the debugger target and then
* waits for target to reach a breakpoint.
*
* This function returns a tuple of type 'breakpoint' that contains the
* function OID and line number where the target is currently stopped.
*/
Datum pldbg_continue( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
sendString( session, PLDBG_CONTINUE );
PG_RETURN_DATUM( buildBreakpointDatum( getNString( session )));
}
/*******************************************************************************
* pldbg_abort_target( sessionID INTEGER ) RETURNS breakpoint
*
* This function sends an "abort" command to the debugger target and then
* waits for a reply
*/
Datum pldbg_abort_target( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
sendString( session, PLDBG_ABORT );
PG_RETURN_BOOL( getBool( session ));
}
/*******************************************************************************
* pldbg_select_frame( sessionID INTEGER, frameNumber INTEGER )
* RETURNS breakpoint
*
* This function changes the debugger focus to the indicated frame (in the call
* stack). Whenever the target stops (at a breakpoint or as the result of a
* step/into or step/over), the debugger changes focus to most deeply nested
* function in the call stack (because that's the function that's executing).
*
* You can change the debugger focus to other stack frames - once you do that,
* you can examine the source code for that frame, the variable values in that
* frame, and the breakpoints in that target.
*
* The debugger focus remains on the selected frame until you change it or
* the target stops at another breakpoint.
*
* This function returns a tuple of type 'breakpoint' that contains the
* function OID, and line number where the target is currently stopped in
* the selected frame.
*/
Datum pldbg_select_frame( PG_FUNCTION_ARGS )
{
if( PG_ARGISNULL( 0 ))
PG_RETURN_NULL();
else
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
int32 frameNumber = PG_GETARG_INT32( 1 );
char frameString[PLDBG_STRING_MAX_LEN];
char * resultString;
Datum result;
snprintf(
frameString, PLDBG_STRING_MAX_LEN, "%s %d", PLDBG_SELECT_FRAME,
frameNumber
);
sendString( session, frameString );
resultString = getNString( session );
result = buildBreakpointDatum( resultString );
PG_RETURN_DATUM( result );
}
}
/*******************************************************************************
* pldbg_get_source( sessionID INTEGER, functionOID OID )
* RETURNS CSTRING
*
* This function returns the source code for the given function. A debugger
* client should always retrieve source code using this function instead of
* reading pg_proc. If you read pg_proc instead, the source code that you
* read may not match the source that the target is actually executing
* (because the source code may have been modified in a different transaction).
*
* pldbg_get_source() always retrieves the source code from the target and
* ensures that the source code that you get is the source code that the
* target is executing.
*
*/
Datum pldbg_get_source( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
Oid funcOID = PG_GETARG_OID( 1 );
char sourceString[PLDBG_STRING_MAX_LEN];
char * source;
snprintf(
sourceString, PLDBG_STRING_MAX_LEN, "%s %u",
PLDBG_GET_SOURCE, funcOID
);
sendString( session, sourceString );
source = getNString( session );
PG_RETURN_TEXT_P(cstring_to_text(source));
}
/*******************************************************************************
* pldbg_get_breakpoints( sessionID INTEGER ) RETURNS SETOF breakpoint
*
* This function returns a SETOF breakpoint tuples. Each tuple in the result
* set identifies a breakpoint.
*
* NOTE: the result set returned by this function should be identical to
* the result set returned by a SHOW BREAKPOINTS command. This function
* may become obsolete when SHOW BREAKPOINTS is complete.
*/
Datum pldbg_get_breakpoints( PG_FUNCTION_ARGS )
{
FuncCallContext * srf;
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
char * breakpointString;
if( SRF_IS_FIRSTCALL())
{
MemoryContext oldContext;
srf = SRF_FIRSTCALL_INIT();
oldContext = MemoryContextSwitchTo( srf->multi_call_memory_ctx );
srf->attinmeta = TupleDescGetAttInMetadata( RelationNameGetTupleDesc( TYPE_NAME_BREAKPOINT ));
MemoryContextSwitchTo( oldContext );
sendString( session, PLDBG_GET_BREAKPOINTS );
}
else
{
srf = SRF_PERCALL_SETUP();
}
if(( breakpointString = getNString( session )) != NULL )
{
SRF_RETURN_NEXT( srf, buildBreakpointDatum( breakpointString ));
}
else
{
SRF_RETURN_DONE( srf );
}
}
/*******************************************************************************
* pldbg_get_variables( sessionID INTEGER ) RETURNS SETOF var
*
* This function returns a SETOF var tuples. Each tuple in the result
* set contains information about one local variable (or parameter) in the
* stack frame that has the focus. Each tuple contains the name of the
* variable, the line number at which the variable was declared, a flag
* that tells you whether the name is unique within the scope of the function
* (if the name is not unique, a debugger client may use the line number to
* distinguish between variables with the same name), a flag that tells you
* whether the variables is a CONST, a flag that tells you whether the variable
* is NOT NULL, the data type of the variable (the OID of the corresponding
* pg_type) and the value of the variable.
*
* To view variables defined in a different stack frame, call
* pldbg_select_frame() to change the debugger's focus to that frame.
*/
Datum pldbg_get_variables( PG_FUNCTION_ARGS )
{
FuncCallContext * srf;
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
char * variableString;
if( SRF_IS_FIRSTCALL())
{
MemoryContext oldContext;
srf = SRF_FIRSTCALL_INIT();
oldContext = MemoryContextSwitchTo( srf->multi_call_memory_ctx );
srf->attinmeta = TupleDescGetAttInMetadata( RelationNameGetTupleDesc( TYPE_NAME_VAR ));
MemoryContextSwitchTo( oldContext );
sendString( session, PLDBG_GET_VARIABLES );
}
else
{
srf = SRF_PERCALL_SETUP();
}
if(( variableString = getNString( session )) != NULL )
{
char * values[8];
char * ctx = NULL;
HeapTuple result;
/*
* variableString points to a string like:
* varName:class:lineNumber:unique:isConst:notNull:dataTypeOID
*/
values[0] = pstrdup( tokenize( variableString, ":", &ctx )); /* variable name */
values[1] = pstrdup( tokenize( NULL, ":", &ctx )); /* var class */
values[2] = pstrdup( tokenize( NULL, ":", &ctx )); /* line number */
values[3] = pstrdup( tokenize( NULL, ":", &ctx )); /* unique */
values[4] = pstrdup( tokenize( NULL, ":", &ctx )); /* isConst */
values[5] = pstrdup( tokenize( NULL, ":", &ctx )); /* notNull */
values[6] = pstrdup( tokenize( NULL, ":", &ctx )); /* data type OID */
values[7] = pstrdup( tokenize( NULL, NULL, &ctx )); /* value (rest of string) */
result = BuildTupleFromCStrings( srf->attinmeta, values );
SRF_RETURN_NEXT( srf, HeapTupleGetDatum( result ));
}
else
{
SRF_RETURN_DONE( srf );
}
}
/*******************************************************************************
* pldbg_get_stack( sessionID INTEGER ) RETURNS SETOF frame
*
* This function returns a SETOF frame tuples. Each tuple in the result
* set contains information about one stack frame: the tuple contains the
* function OID, and line number within that function. Each tuple also
* contains a string that you can use to display the name and value of each
* argument to that particular invocation.
*/
Datum pldbg_get_stack( PG_FUNCTION_ARGS )
{
FuncCallContext * srf;
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
char * frameString;
if( SRF_IS_FIRSTCALL())
{
MemoryContext oldContext;
srf = SRF_FIRSTCALL_INIT();
oldContext = MemoryContextSwitchTo( srf->multi_call_memory_ctx );
srf->attinmeta = TupleDescGetAttInMetadata( RelationNameGetTupleDesc( TYPE_NAME_FRAME ));
MemoryContextSwitchTo( oldContext );
sendString( session, PLDBG_GET_STACK );
}
else
{
srf = SRF_PERCALL_SETUP();
}
if(( frameString = getNString( session )) != NULL )
{
char * values[5];
char callCount[PLDBG_STRING_MAX_LEN];
char * ctx = NULL;
HeapTuple result;
/*
* frameString points to a string like:
* targetName:funcOID:lineNumber:arguments
*/
snprintf(
callCount, PLDBG_STRING_MAX_LEN, UINT64_FORMAT,
(uint64)srf->call_cntr
);
values[0] = callCount;
values[1] = tokenize( frameString, ":", &ctx ); /* targetName */
values[2] = tokenize( NULL, ":", &ctx ); /* funcOID */
values[3] = tokenize( NULL, ":", &ctx ); /* lineNumber */
values[4] = tokenize( NULL, NULL, &ctx ); /* arguments - rest of string */
result = BuildTupleFromCStrings( srf->attinmeta, values );
SRF_RETURN_NEXT( srf, HeapTupleGetDatum( result ));
}
else
{
SRF_RETURN_DONE( srf );
}
}
/********************************************************************************
* pldbg_get_proxy_info( ) RETURNS proxyInfo
*
* This function retrieves a small collection of parameters from the server, all
* parameters are related to the version of the server and the version of this
* proxy API.
*
* You can call this function (from the debugger client process) to find out
* which version of the proxy API you are talking to - if this function does
* not exist, you can assume that you are talking to a version 1 proxy server.
*/
Datum pldbg_get_proxy_info( PG_FUNCTION_ARGS )
{
Datum values[4] = {0};
bool nulls[4] = {0};
TupleDesc tupleDesc = getResultTupleDesc( fcinfo );
HeapTuple result;
values[0] = DirectFunctionCall1( textin, PointerGetDatum( PG_VERSION_STR ));
values[1] = Int32GetDatum( PG_VERSION_NUM );
values[2] = Int32GetDatum( PROXY_API_VERSION );
values[3] = Int32GetDatum( MyProcPid );
result = heap_form_tuple( tupleDesc, values, nulls );
PG_RETURN_DATUM( HeapTupleGetDatum( result ));
}
/*******************************************************************************
* pldbg_set_breakpoint(sessionID INT, function OID, lineNumber INT)
* RETURNS boolean
*
* Sets a *local* breakpoint in the target process.
*/
Datum pldbg_set_breakpoint( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
Oid funcOID = PG_GETARG_OID( 1 );
int lineNumber = PG_GETARG_INT32( 2 );
char breakpointString[PLDBG_STRING_MAX_LEN];
snprintf(
breakpointString, PLDBG_STRING_MAX_LEN, "%s %u:%d",
PLDBG_SET_BREAKPOINT, funcOID, lineNumber
);
sendString( session, breakpointString );
PG_RETURN_BOOL( getBool( session ));
}
/*******************************************************************************
* pldbg_drop_breakpoint(sessionID INT, function OID, lineNumber INT)
* RETURNS boolean
*/
Datum pldbg_drop_breakpoint( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
Oid funcOID = PG_GETARG_OID( 1 );
int lineNumber = PG_GETARG_INT32( 2 );
char breakpointString[PLDBG_STRING_MAX_LEN];
snprintf(
breakpointString, PLDBG_STRING_MAX_LEN, "%s %u:%d",
PLDBG_CLEAR_BREAKPOINT, funcOID, lineNumber
);
sendString( session, breakpointString );
PG_RETURN_BOOL( getBool( session ));
}
/*******************************************************************************
* pldbg_deposit_value( sessionID INT, varName TEXT, lineNumber INT, value TEXT)
* RETURNS boolean
*
* This function 'deposits' a new value into the given variable (identified by
* name and optional line number). 'value' is evaluated as an expression that
* must result in a value whose type matches the given variable (or whose type
* is coerce'able to the type of the given variable).
*/
Datum pldbg_deposit_value( PG_FUNCTION_ARGS )
{
debugSession * session = defaultSession( PG_GETARG_SESSION( 0 ));
char * varName = GET_STR( PG_GETARG_TEXT_P( 1 ));
int lineNumber = PG_GETARG_INT32( 2 );
char * value = GET_STR( PG_GETARG_TEXT_P( 3 ));
StringInfoData buf;
initStringInfo( &buf );
appendStringInfo( &buf, "%s %s.%d=%s", PLDBG_DEPOSIT, varName, lineNumber, value );
sendString( session, buf.data );
pfree( buf.data );
PG_RETURN_BOOL( getBool( session ));
}
/*******************************************************************************
* Local supporting (static) functions
*******************************************************************************/
/*******************************************************************************
* initializeModule()
*
* Initializes the debugger proxy module. For now, we just register a callback
* (cleanupAtExit()) that this backend will invoke on exit - we use that
* callback to gracefully close any outstanding connections.
*
* NOTE: this would also be a good place to load the tuple descriptions for
* each of the complex datatypes that we use (breakpoint, var, frame).
*/
static void initializeModule( void )
{
static bool initialized = FALSE;
if( !initialized )
{
initialized = TRUE;
on_shmem_exit( cleanupAtExit, 0 );
}
}
/*******************************************************************************
* defaultSession()
*
* This function is designed to make it a little easier to build a simple
* debugger client. Instead of managing session identifiers, you can simply
* pass '0' to each function that requires a session ID. When a proxy function
* encounters a session ID of 0, it assumes that you want to work with the most
* recently used session. If you have only one session, you can simply pass
* '0' to every function. This is particularly handy if you're using the proxy
* API from a command line application like psql.
*
* NOTE: If you give this function an invalid sessionHandle it will throw an
* error. A sessionHandle is valid if returned by addSession().
*/
static debugSession * defaultSession( sessionHandle handle )
{
debugSession * session;
if( handle == 0 )
{
if( mostRecentSession == NULL )
ereport( ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg( "invalid session handle" )));
else
return( mostRecentSession );
}
else
{
session = findSession( handle );
if( session == NULL )
ereport( ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg( "invalid session handle" )));
else
{
mostRecentSession = session;
return( session );
}
}
return( NULL ); /* keep the compiler happy */
}