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
|
/*
This code implements one part of functonality of
free available library PL/Vision. Please look www.quest.com
Original author: Steven Feuerstein, 1996 - 2002
PostgreSQL implementation author: Pavel Stehule, 2006-2023
This module is under BSD Licence
History:
1.0. first public version 22. September 2006
*/
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "utils/pg_locale.h"
#include "mb/pg_wchar.h"
#include "lib/stringinfo.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/memutils.h"
#include "utils/lsyscache.h"
#include "access/tupmacs.h"
#include "orafce.h"
#include "builtins.h"
#include "utils/elog.h"
PG_FUNCTION_INFO_V1(dbms_utility_format_call_stack0);
PG_FUNCTION_INFO_V1(dbms_utility_format_call_stack1);
PG_FUNCTION_INFO_V1(dbms_utility_get_time);
static char *
dbms_utility_format_call_stack(char mode)
{
MemoryContext oldcontext = CurrentMemoryContext;
ErrorData *edata;
ErrorContextCallback *econtext;
StringInfo sinfo;
#if PG_VERSION_NUM >= 130000
errstart(ERROR, TEXTDOMAIN);
#else
errstart(ERROR, __FILE__, __LINE__, PG_FUNCNAME_MACRO, TEXTDOMAIN);
#endif
MemoryContextSwitchTo(oldcontext);
for (econtext = error_context_stack;
econtext != NULL;
econtext = econtext->previous)
(*econtext->callback) (econtext->arg);
edata = CopyErrorData();
FlushErrorState();
/* Now I wont to parse edata->context to more traditional format */
/* I am not sure about order */
sinfo = makeStringInfo();
switch (mode)
{
case 'o':
appendStringInfoString(sinfo, "----- PL/pgSQL Call Stack -----\n");
appendStringInfoString(sinfo, " object line object\n");
appendStringInfoString(sinfo, " handle number name\n");
break;
}
if (edata->context)
{
char *start = edata->context;
while (*start)
{
char *oname = "anonymous object";
char *line = "";
char *eol = strchr(start, '\n');
Oid fnoid = InvalidOid;
/* first, solve multilines */
if (eol)
*eol = '\0';
/* first know format */
if (strncmp(start, "PL/pgSQL function ", 18) == 0)
{
char *p1,
*p2;
if ((p1 = strstr(start, "function \"")))
{
p1 += strlen("function \"");
if ((p2 = strchr(p1, '"')))
{
*p2++ = '\0';
oname = p1;
start = p2;
}
}
else if ((p1 = strstr(start, "function ")))
{
p1 += strlen("function ");
if ((p2 = strchr(p1, ')')))
{
char c = *++p2;
*p2 = '\0';
oname = pstrdup(p1);
fnoid = DatumGetObjectId(DirectFunctionCall1(regprocedurein,
CStringGetDatum(oname)));
*p2 = c;
start = p2;
}
}
if ((p1 = strstr(start, "line ")))
{
size_t p2i;
char c;
p1 += strlen("line ");
p2i = strspn(p1, "0123456789");
/* safe separator */
c = p1[p2i];
p1[p2i] = '\0';
line = pstrdup(p1);
p1[p2i] = c;
}
}
switch (mode)
{
case 'o':
appendStringInfo(sinfo, "%8x %5s function %s", (int) fnoid, line, oname);
break;
case 'p':
appendStringInfo(sinfo, "%8d %5s function %s", (int) fnoid, line, oname);
break;
case 's':
appendStringInfo(sinfo, "%d,%s,%s", (int) fnoid, line, oname);
break;
}
if (eol)
{
start = eol + 1;
appendStringInfoChar(sinfo, '\n');
}
else
break;
}
}
return sinfo->data;
}
Datum
dbms_utility_format_call_stack0(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text(dbms_utility_format_call_stack('o')));
}
Datum
dbms_utility_format_call_stack1(PG_FUNCTION_ARGS)
{
text *arg = PG_GETARG_TEXT_P(0);
char mode;
if ((1 != VARSIZE(arg) - VARHDRSZ))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid parameter"),
errdetail("Allowed only chars [ops].")));
mode = *VARDATA(arg);
switch (mode)
{
case 'o':
case 'p':
case 's':
break;
default:
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid parameter"),
errdetail("Allowed only chars [ops].")));
}
PG_RETURN_TEXT_P(cstring_to_text(dbms_utility_format_call_stack(mode)));
}
/*
* Returns the number of hundredths of seconds that have elapsed
* since a point in time in the past.
*/
Datum
dbms_utility_get_time(PG_FUNCTION_ARGS)
{
struct timeval tv;
gettimeofday(&tv, NULL);
PG_RETURN_INT32((int32) ((int64) tv.tv_sec * 100 + tv.tv_usec / 10000));
}
|