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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
|
/*
* 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 */
}
/*******************************************************************************
* initSessionHash()
*
* Initialize a hash table that we use to map session handles (simple integer
* values) into debugSession pointers.
*
* You should call this function before you use the hash - you can call it
* as many times as you like, it will only initialize the hash table on the
* first invocation.
*/
static void initSessionHash()
{
if( sessionHash )
return;
else
{
HASHCTL ctl = {0};
ctl.keysize = sizeof( sessionHandle );
ctl.entrysize = sizeof( sessionHashEntry );
ctl.hash = tag_hash;
sessionHash = hash_create( "Debugger sessions", 5, &ctl, HASH_ELEM | HASH_FUNCTION );
}
}
/*******************************************************************************
* addSession()
*
* Adds a session (debugSession *) to the hash that we use to map session
* handles into debugSession pointers. This function returns a handle that
* you should give back to the debugger client process. When the debugger
* client calls us again, he gives us the handle and we map that back into
* a debugSession pointer. That way, we don't have to expose a pointer to
* the debugger client (which can make for nasty denial of service hacks, not
* to mention 32-bit vs. 64-bit hassles).
*/
static sessionHandle addSession( debugSession * session )
{
static sessionHandle nextHandle;
sessionHashEntry * entry;
bool found;
sessionHandle handle;
initSessionHash();
handle = ++nextHandle;
entry = (sessionHashEntry *)hash_search( sessionHash, &handle, HASH_ENTER, &found );
entry->m_handle = handle;
entry->m_session = session;
return( handle );
}
/*******************************************************************************
* findSession()
*
* Given a sessionHandle (integer), this function returns the corresponding
* debugSession pointer. If the sessionHandle is invalid (that is, it's a
* number not returned by addSession()), this function returns NULL.
*/
static debugSession * findSession( sessionHandle handle )
{
sessionHashEntry * entry;
initSessionHash();
if(( entry = hash_search( sessionHash, &handle, HASH_FIND, NULL )) != NULL )
{
return( entry->m_session );
}
else
{
return( NULL );
}
}
/*******************************************************************************
* tokenize()
*
* This is a re-entrant safe version of the standard C strtok() function.
* tokenize() will split a string (src) into multiple substrings separated by
* any of the characters in the delimiter string (delimiters). Each time you
* call tokenize(), it returns the next subtstring (or NULL when all substrings
* have been exhausted). The first time you call this function, ctx should be
* NULL and src should point to the start of the string you are splitting.
* For every subsequent call, src should be NULL and tokenize() will manage
* ctx itself.
*
* NOTE: the search string (src) is brutally altered by this function - make
* a copy of the search string before you call tokenize() if you need the
* original string.
*/
static char * tokenize( char * src, const char * delimiters, char ** ctx )
{
char * start;
char * end;
if( src == NULL )
src = *ctx;
/*
* Special case - if delimiters is NULL, we just return the
* remainder of the string.
*/
if( delimiters == NULL )
return( src );
if( src == NULL )
elog(ERROR, "debugger protocol error: token expected");
/*
* Skip past any leading delimiters
*/
start = src = ( src + strspn( src, delimiters ));
if( *src == '\0' )
return( "" );
if(( end = strpbrk( start, delimiters )) == NULL )
{
*ctx = strchr( start, '\0' );
}
else
{
*end = '\0';
*ctx = end + 1;
}
return( start );
}
/*******************************************************************************
* readn()
*
* This function reads exactly 'len' bytes from the given socket or it
* throws an error (ERRCODE_CONNECTION_FAILURE). 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 serverHandle, void * dst, size_t len )
{
size_t bytesRemaining = len;
char * buffer = (char *)dst;
if( serverHandle == -1 )
ereport( ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg( "given session is not connected" )));
while( bytesRemaining > 0 )
{
fd_set rmask;
ssize_t bytesRead;
/*
* Note: we want to wait for some number of bytes to arrive from the
* target process, but we also want to notice if the client process
* disappears. To do that, we'll call select() before we call recv()
* and we'll tell select() to return as soon as something interesting
* happens on *either* of the sockets. If the target sends us data
* first, we're ok (that's what we are expecting to happen). If we
* detect any activity on the client-side socket (which is the libpq
* socket), we can assume that something's gone horribly wrong (most
* likely, the user killed the client by clicking the close button).
*/
FD_ZERO( &rmask );
FD_SET( serverHandle, &rmask );
FD_SET( MyProcPort->sock, &rmask );
switch( select(( serverHandle > MyProcPort->sock ? serverHandle : MyProcPort->sock ) + 1, &rmask, NULL, NULL, NULL ))
{
case -1:
{
ereport( ERROR, ( errcode(ERRCODE_CONNECTION_FAILURE), errmsg( "select() failed waiting for target" )));
break;
}
case 0:
{
/* Timer expired */
return( NULL );
break;
}
default:
{
/*
* We got traffic on one of the two sockets. If we see traffic
* from the client (libpq) connection, just return to the
* caller so that libpq can process whatever's waiting.
* Presumably, the only time we'll see any libpq traffic here
* is when the client process has killed itself...
*/
if( FD_ISSET( MyProcPort->sock, &rmask ))
ereport( ERROR, ( errcode(ERRCODE_CONNECTION_FAILURE), errmsg( "debugger connection(client side) terminated" )));
break;
}
}
bytesRead = recv( serverHandle, buffer, bytesRemaining, 0 );
if( bytesRead <= 0 && errno != EINTR )
{
ereport( ERROR, (errcode(ERRCODE_CONNECTION_FAILURE), errmsg( "debugger connection terminated" )));
return( NULL );
}
bytesRemaining -= bytesRead;
buffer += bytesRead;
}
return( dst );
}
/*******************************************************************************
* writen()
*
* This function writes exactly 'len' bytes to the given socket or it
* throws an error (ERRCODE_CONNECTION_FAILURE). writen() will hang until
* the proper number of bytes have been written (or an error occurs).
*/
static void * writen( int serverHandle, void * src, size_t len )
{
size_t bytesRemaining = len;
char * buffer = (char *)src;
while( bytesRemaining > 0 )
{
ssize_t bytesWritten;
if(( bytesWritten = send( serverHandle, buffer, bytesRemaining, 0 )) <= 0 )
{
ereport( ERROR, ( errcode( ERRCODE_CONNECTION_FAILURE ), errmsg( "debugger connection terminated" )));
return( NULL );
}
bytesRemaining -= bytesWritten;
buffer += bytesWritten;
}
return( src );
}
/*******************************************************************************
* sendBytes()
*
* This function sends 'len' bytes to the server (identfied by a debugSession
* pointer). 'src' should point to the bytes that you want to send to the
* server.
*/
static void sendBytes( debugSession * session, void * src, size_t len )
{
writen( session->serverSocket, src, len );
}
/*******************************************************************************
* sendUInt32()
*
* This function sends a uint32 value (val) to the debugger server.
*/
static void sendUInt32( debugSession * session, uint32 val )
{
uint32 netVal = htonl( val );
sendBytes( session, &netVal, sizeof( netVal ));
}
/*******************************************************************************
* sendString()
*
* This function sends a string value (src) to the debugger server. 'src'
* should point to a null-terminated string. We send the length of the string
* (as a 32-bit unsigned integer), then the bytes that make up the string - we
* don't send the null-terminator.
*/
static void sendString( debugSession * session, char * src )
{
size_t len = strlen( src );
sendUInt32( session, len );
sendBytes( session, src, len );
}
/*******************************************************************************
* getBool()
*
* getBool() retreives a boolean value (TRUE or FALSE) from the server. We
* call this function after we ask the server to do something that returns a
* boolean result (like deleting a breakpoint or depositing a new value).
*/
static bool getBool( debugSession * session )
{
char * str;
bool result;
str = getNString( session );
if (str == NULL)
elog(ERROR, "debugger protocol error; bool expected");
if( str[0] == 't' )
result = TRUE;
else
result = FALSE;
pfree( str );
return( result );
}
/*******************************************************************************
* getUInt32()
*
* Reads a 32-bit unsigned value from the server (and returns it in the host's
* byte ordering)
*/
static uint32 getUInt32( debugSession * session )
{
uint32 result;
readn( session->serverSocket, &result, sizeof( result ));
return( ntohl( result ));
}
/******************************************************************************
* getNstring()
*
* This function is the opposite of sendString() - it reads a string from the
* debugger server. The server sends the length of the string and then the
* bytes that make up the string (minus the null-terminator). We palloc()
* enough space to hold the entire string (including the null-terminator) and
* return a pointer to that space (after, of course, reading the string from
* the server and tacking on the null-terminator).
*/
static char * getNString( debugSession * session )
{
uint32 len = getUInt32( session );
if( len == 0 )
return( NULL );
else
{
char * result = palloc( len + 1 );
readn( session->serverSocket, result, len );
result[len] = '\0';
return( result );
}
}
/*******************************************************************************
* closeSession()
*
* This function closes (in an orderly manner) the connection with the debugger
* server.
*/
static void closeSession( debugSession * session )
{
if( session->serverSocket )
closesocket( session->serverSocket );
if( session->listener )
BreakpointCleanupProc( MyProcPid );
if( session->breakpointString )
pfree( session->breakpointString );
pfree( session );
}
/******************************************************************************
* cleanupAtExit()
*
* This is a callback function that the backend invokes when exiting. At exit,
* we close any connections that we may still have (connections to debugger
* servers, that is).
*/
static void cleanupAtExit( int code, Datum arg )
{
/*
* FIXME: we should clean up all of the sessions stored in the
* sessionHash.
*/
if( mostRecentSession )
closeSession( mostRecentSession );
mostRecentSession = NULL;
}
/*******************************************************************************
* getResultTupleDesc()
*
* If this function returns (without throwing an error), it returns a pointer
* to a description of the tuple that should be returned by the caller.
*
* NOTE: the caller must have been called in a context that can accept a
* set, not a context that expects a tuple. That means that you
* must invoke our caller with:
* select * from foo();
* instead of:
* select foo();
*/
static TupleDesc getResultTupleDesc( FunctionCallInfo fcinfo )
{
ReturnSetInfo * rsinfo = (ReturnSetInfo *)fcinfo->resultinfo;
if( rsinfo == NULL )
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
}
return( rsinfo->expectedDesc );
}
|