LCOV - code coverage report
Current view: top level - monetdb5/modules/mal - inspect.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 151 308 49.0 %
Date: 2021-01-13 20:07:21 Functions: 12 19 63.2 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * author Martin Kersten
      11             :  * Inspection
      12             :  * This module introduces a series of commands that provide access
      13             :  * to information stored within the interpreter data structures.
      14             :  * It's primary use is debugging.
      15             :  * In all cases, the pseudo BAT operation is returned that
      16             :  * should be garbage collected after being used.
      17             :  *
      18             :  * The main performance drain would be to use a pseudo BAT directly to
      19             :  * successively access it components. This can be avoided by first assigning
      20             :  * the pseudo BAT to a variable.
      21             :  */
      22             : #include "monetdb_config.h"
      23             : #include "gdk.h"
      24             : #include <time.h>
      25             : #include "mal_resolve.h"
      26             : #include "mal_client.h"
      27             : #include "mal_exception.h"
      28             : #include "mal_debugger.h"
      29             : #include "mal_interpreter.h"
      30             : #include "mal_listing.h"
      31             : #include "mal_namespace.h"
      32             : 
      33             : static int
      34           7 : pseudo(bat *ret, BAT *b, str X1,str X2, str X3) {
      35             :         char buf[BUFSIZ];
      36           7 :         snprintf(buf,BUFSIZ,"%s_%s_%s", X1,X2,X3);
      37           7 :         if (BBPindex(buf) <= 0 && BBPrename(b->batCacheid, buf) != 0)
      38             :                 return -1;
      39           7 :         if (BATroles(b,X2) != GDK_SUCCEED)
      40             :                 return -1;
      41           7 :         *ret = b->batCacheid;
      42           7 :         BBPkeepref(*ret);
      43           7 :         return 0;
      44             : }
      45             : 
      46             : /*
      47             :  * Symbol table
      48             :  * Mal symbol table and environment analysis.
      49             :  *
      50             :  * Collect symbol table information in a series of BATs for analysis
      51             :  * and display. Note, the elements are aligned using a counter,
      52             :  * which makes it susceptable for intermediate updates
      53             :  */
      54             : 
      55             : static str
      56           1 : INSPECTgetAllFunctions(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      57             : {
      58             :         Module s;
      59             :         Symbol t;
      60             :         int i, j;
      61             :         Module* moduleList;
      62             :         int length;
      63           1 :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
      64           1 :         bat *ret = getArgReference_bat(stk,pci,0);
      65             : 
      66             :         (void) mb;
      67           1 :         if (b == 0)
      68           0 :                 throw(MAL, "inspect.getgetFunctionId", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      69             : 
      70           1 :         getModuleList(&moduleList, &length);
      71           1 :         if (moduleList == NULL)
      72           0 :                 goto bailout;
      73          72 :         for(j = -1; j < length; j++) {
      74          71 :                 s = j < 0 ? cntxt->usermodule : moduleList[j];
      75       18247 :                 for (i = 0; s && i < MAXSCOPE; i++) {
      76       18176 :                         if (s->space[i]) {
      77       13514 :                                 for (t = s->space[i]; t; t = t->peer) {
      78       12993 :                                         InstrPtr sig = getSignature(t);
      79       12993 :                                         if (BUNappend(b, getFunctionId(sig), false) != GDK_SUCCEED)
      80           0 :                                                 goto bailout;
      81             :                                 }
      82             :                         }
      83             :                 }
      84             :         }
      85           1 :         if (pseudo(ret,b,"view","symbol","function"))
      86           0 :                 goto bailout;
      87           1 :         freeModuleList(moduleList);
      88             : 
      89           1 :         return MAL_SUCCEED;
      90           0 :   bailout:
      91           0 :         BBPreclaim(b);
      92           0 :         freeModuleList(moduleList);
      93           0 :         throw(MAL, "inspect.getgetFunctionId", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      94             : }
      95             : 
      96             : static str
      97           1 : INSPECTgetAllModules(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      98             : {
      99             :         Module s;
     100             :         Symbol t;
     101             :         int i, j;
     102             :         Module* moduleList;
     103             :         int length;
     104           1 :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
     105           1 :         bat *ret = getArgReference_bat(stk,pci,0);
     106             : 
     107             :         (void) mb;
     108           1 :         if (b == 0)
     109           0 :                 throw(MAL, "inspect.getmodule", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     110             : 
     111           1 :         getModuleList(&moduleList, &length);
     112           1 :         if (moduleList == NULL)
     113           0 :                 goto bailout;
     114          72 :         for(j = -1; j < length; j++) {
     115          71 :                 s = j < 0 ? cntxt->usermodule : moduleList[j];
     116       18247 :                 for (i = 0; s && i < MAXSCOPE; i++) {
     117       18176 :                         if (s->space[i]) {
     118       13514 :                                 for (t = s->space[i]; t; t = t->peer) {
     119       12993 :                                         InstrPtr sig = getSignature(t);
     120             : 
     121       12993 :                                         if (BUNappend(b, getModuleId(sig), false) != GDK_SUCCEED)
     122           0 :                                                 goto bailout;
     123             :                                 }
     124             :                         }
     125             :                 }
     126             :         }
     127           1 :         if (pseudo(ret,b,"view","symbol","module"))
     128           0 :                 goto bailout;
     129           1 :         freeModuleList(moduleList);
     130             : 
     131           1 :         return MAL_SUCCEED;
     132           0 :   bailout:
     133           0 :         freeModuleList(moduleList);
     134           0 :         BBPreclaim(b);
     135           0 :         throw(MAL, "inspect.getmodule", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     136             : }
     137             : 
     138             : static str
     139           1 : INSPECTgetkind(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     140             : {
     141             :         Module s;
     142             :         Symbol t;
     143             :         int i, j;
     144             :         Module* moduleList;
     145             :         int length;
     146           1 :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
     147           1 :         bat *ret = getArgReference_bat(stk,pci,0);
     148             : 
     149             :         (void)mb;
     150           1 :         if (b == 0)
     151           0 :                 throw(MAL, "inspect.get", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     152             : 
     153           1 :         getModuleList(&moduleList, &length);
     154           1 :         if (moduleList == NULL)
     155           0 :                 goto bailout;
     156          72 :         for(j = -1; j < length; j++) {
     157          71 :                 s = j < 0 ? cntxt->usermodule : moduleList[j];
     158       18247 :                 for (i = 0; s && i < MAXSCOPE; i++) {
     159       18176 :                         if (s->space[i]) {
     160       13514 :                                 for (t = s->space[i]; t; t = t->peer) {
     161       12993 :                                         InstrPtr sig = getSignature(t);
     162       12993 :                                         str kind = operatorName(sig->token);
     163       12993 :                                         if (BUNappend(b, kind, false) != GDK_SUCCEED)
     164           0 :                                                 goto bailout;
     165             :                                 }
     166             :                         }
     167             :                 }
     168             :         }
     169           1 :         if (pseudo(ret,b,"view","symbol","kind"))
     170           0 :                 goto bailout;
     171           1 :         freeModuleList(moduleList);
     172             : 
     173           1 :         return MAL_SUCCEED;
     174           0 :   bailout:
     175           0 :         BBPreclaim(b);
     176           0 :         freeModuleList(moduleList);
     177           0 :         throw(MAL, "inspect.get", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     178             : }
     179             : 
     180             : static str
     181           1 : INSPECTgetAllSignatures(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     182             : {
     183             :         Module s;
     184             :         Symbol t;
     185             :         int i, j;
     186             :         Module* moduleList;
     187             :         int length;
     188           1 :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
     189             :         char sig[BLOCK],*a;
     190           1 :         bat *ret = getArgReference_bat(stk,pci,0);
     191             : 
     192             :         (void)mb;
     193           1 :         if (b == 0)
     194           0 :                 throw(MAL, "inspect.get", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     195             : 
     196           1 :         getModuleList(&moduleList, &length);
     197           1 :         if (moduleList == NULL)
     198           0 :                 goto bailout;
     199          72 :         for(j = -1; j < length; j++) {
     200          71 :                 s = j < 0 ? cntxt->usermodule : moduleList[j];
     201       18247 :                 for (i = 0; s && i < MAXSCOPE; i++)
     202       18176 :                         if (s->space[i]) {
     203       13514 :                                 for (t = s->space[i]; t; t = t->peer) {
     204       12993 :                                         fcnDefinition(t->def, getSignature(t), sig, 0,sig,BLOCK);
     205       12993 :                                         a= strstr(sig,"address");
     206       12993 :                                         if(a) *a = 0;
     207       12993 :                                         if (BUNappend(b, (a = strchr(sig, '(')) ? a : "", false) != GDK_SUCCEED)
     208           0 :                                                 goto bailout;
     209             :                                 }
     210             :                         }
     211             :         }
     212           1 :         if (pseudo(ret,b,"view"," symbol","address"))
     213           0 :                 goto bailout;
     214           1 :         freeModuleList(moduleList);
     215             : 
     216           1 :         return MAL_SUCCEED;
     217           0 :   bailout:
     218           0 :         BBPreclaim(b);
     219           0 :         freeModuleList(moduleList);
     220           0 :         throw(MAL, "inspect.get", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     221             : }
     222             : 
     223             : #if 0
     224             : static str
     225             : INSPECTgetAllAddresses(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     226             : {
     227             :         Module s;
     228             :         Symbol t;
     229             :         int i, j;
     230             :         Module* moduleList;
     231             :         int length;
     232             :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
     233             :         char sig[BLOCK],*a;
     234             :         bat *ret = getArgReference_bat(stk,pci,0);
     235             : 
     236             :         (void)mb;
     237             : 
     238             :         if (b == 0)
     239             :                 throw(MAL, "inspect.get", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     240             : 
     241             :         getModuleList(&moduleList, &length);
     242             :         if (moduleList == NULL)
     243             :                 goto bailout;
     244             :         for(j = -1; j < length; j++) {
     245             :                 s = j < 0 ? cntxt->usermodule : moduleList[j];
     246             :                 for (i = 0; s && i < MAXSCOPE; i++)
     247             :                         if (s->space[i]) {
     248             :                                 for (t = s->space[i]; t; t = t->peer) {
     249             :                                         fcnDefinition(t->def, getSignature(t), sig, 0,sig,BLOCK);
     250             :                                         a= strstr(sig,"address");
     251             :                                         if( a)
     252             :                                                 for( a=a+7; isspace((unsigned char) *a); a++)
     253             :                                                         ;
     254             :                                         if (BUNappend(b, (a? a: "nil"), false) != GDK_SUCCEED)
     255             :                                                 goto bailout;
     256             :                                 }
     257             :                         }
     258             :         }
     259             :         if (pseudo(ret,b,"view"," symbol","address"))
     260             :                 goto bailout;
     261             :         freeModuleList(moduleList);
     262             : 
     263             :         return MAL_SUCCEED;
     264             :   bailout:
     265             :         BBPreclaim(b);
     266             :         freeModuleList(moduleList);
     267             :         throw(MAL, "inspect.get", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     268             : }
     269             : #endif
     270             : 
     271             : static str
     272           0 : INSPECTgetDefinition(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     273             : {
     274           0 :         bat *ret = getArgReference_bat(stk,pci,0);
     275           0 :         str *mod = getArgReference_str(stk,pci,1);
     276           0 :         str *fcn = getArgReference_str(stk,pci,2);
     277             :         Symbol s;
     278             :         BAT *b;
     279             :         (void)mb;
     280             : 
     281           0 :         s = findSymbol(cntxt->usermodule, putName(*mod), putName(*fcn));
     282           0 :         if (s == 0)
     283           0 :                 throw(MAL, "inspect.getDefinition", RUNTIME_SIGNATURE_MISSING);
     284             : 
     285           0 :         b = COLnew(0, TYPE_str, 256, TRANSIENT);
     286           0 :         if (b == 0)
     287           0 :                 throw(MAL, "inspect.getDefinition", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     288             : 
     289           0 :         while (s) {
     290             :                 int i;
     291             :                 str ps;
     292             : 
     293           0 :                 for (i = 0; i < s->def->stop; i++) {
     294           0 :                         if((ps = instruction2str(s->def,0, getInstrPtr(s->def, i), 0)) == NULL)
     295           0 :                                 goto bailout;
     296           0 :                         if (BUNappend(b, ps + 1, false) != GDK_SUCCEED) {
     297           0 :                                 GDKfree(ps);
     298           0 :                                 goto bailout;
     299             :                         }
     300           0 :                         GDKfree(ps);
     301             :                 }
     302           0 :                 s = s->peer;
     303             :         }
     304           0 :         if (pseudo(ret,b,"view","fcn","stmt"))
     305           0 :                 goto bailout;
     306             : 
     307             :         return MAL_SUCCEED;
     308           0 :   bailout:
     309           0 :         BBPreclaim(b);
     310           0 :         throw(MAL, "inspect.getDefinition", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     311             : }
     312             : 
     313             : static str
     314           3 : INSPECTgetSignature(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     315             : {
     316           3 :         bat *ret = getArgReference_bat(stk,pci,0);
     317           3 :         str *mod = getArgReference_str(stk,pci,1);
     318           3 :         str *fcn = getArgReference_str(stk,pci,2);
     319             :         Symbol s;
     320             :         str ps, tail;
     321             :         BAT *b;
     322             :         (void) mb;
     323             : 
     324           3 :         s = findSymbol(cntxt->usermodule, getName(*mod), putName(*fcn));
     325           3 :         if (s == 0)
     326           0 :                 throw(MAL, "inspect.getSignature", RUNTIME_SIGNATURE_MISSING);
     327           3 :         b = COLnew(0, TYPE_str, 12, TRANSIENT);
     328           3 :         if (b == 0)
     329           0 :                 throw(MAL, "inspect.getSignature", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     330             : 
     331           7 :         while (s != NULL) {
     332           4 :                 if (idcmp(s->name, *fcn) == 0) {
     333           4 :                         InstrPtr p = getSignature(s);
     334             :                         char *c, *w;
     335             : 
     336           4 :                         ps = instruction2str(s->def, 0, p, 0);
     337           4 :                         if (ps == 0) {
     338           0 :                                 continue;
     339             :                         }
     340           4 :                         c = strchr(ps, '(');
     341           4 :                         if (c == 0) {
     342           0 :                                 GDKfree(ps);
     343           0 :                                 continue;
     344             :                         }
     345           4 :                         tail= strstr(c,"address");
     346           4 :                         if( tail)
     347           0 :                                 *tail = 0;
     348           4 :                         if (tail && (w=strchr(tail, ';')) )
     349           0 :                                 *w = 0;
     350           4 :                         if (BUNappend(b, c, false) != GDK_SUCCEED) {
     351           0 :                                 GDKfree(ps);
     352           0 :                                 goto bailout;
     353             :                         }
     354           4 :                         GDKfree(ps);
     355             :                 }
     356           4 :                 s = s->peer;
     357             :         }
     358             : 
     359           3 :         if (pseudo(ret,b,"view","input","result"))
     360           0 :                 goto bailout;
     361             :         return MAL_SUCCEED;
     362           0 :   bailout:
     363           0 :         BBPreclaim(b);
     364           0 :         throw(MAL, "inspect.getSignature", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     365             : }
     366             : 
     367             : static str
     368           0 : INSPECTgetComment(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     369             : {
     370           0 :         bat *ret = getArgReference_bat(stk,pci,0);
     371           0 :         str *mod = getArgReference_str(stk,pci,1);
     372           0 :         str *fcn = getArgReference_str(stk,pci,2);
     373             :         Symbol s;
     374             :         BAT *b;
     375             :         (void) mb;
     376             : 
     377           0 :         s = findSymbol(cntxt->usermodule, getName(*mod), putName(*fcn));
     378           0 :         if (s == 0)
     379           0 :                 throw(MAL, "inspect.getComment", RUNTIME_SIGNATURE_MISSING);
     380           0 :         b = COLnew(0, TYPE_str, 12, TRANSIENT);
     381           0 :         if (b == 0)
     382           0 :                 throw(MAL, "inspect.getComment", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     383             : 
     384           0 :         while (s != NULL) {
     385           0 :                 if (idcmp(s->name, *fcn) == 0 &&
     386           0 :                         BUNappend(b, s->def->help, false) != GDK_SUCCEED)
     387           0 :                         goto bailout;
     388           0 :                 s = s->peer;
     389             :         }
     390             : 
     391           0 :         if (pseudo(ret,b,"view","input","result"))
     392           0 :                 goto bailout;
     393             :         return MAL_SUCCEED;
     394           0 :   bailout:
     395           0 :         BBPreclaim(b);
     396           0 :         throw(MAL, "inspect.getComment", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     397             : }
     398             : 
     399             : static str
     400           1 : INSPECTgetSource(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     401             : {
     402           1 :         str *ret = getArgReference_str(stk,pci,0);
     403           1 :         str *mod = getArgReference_str(stk,pci,1);
     404           1 :         str *fcn = getArgReference_str(stk,pci,2);
     405             :         Symbol s;
     406             :         char *buf;
     407             :         size_t len,lim;
     408             :         (void) mb;
     409             : 
     410           1 :         s = findSymbol( cntxt->usermodule, getName(*mod), putName(*fcn));
     411           1 :         if (s == 0)
     412           0 :                 throw(MAL, "inspect.getSource", RUNTIME_SIGNATURE_MISSING);
     413             : 
     414           1 :         buf= (char*) GDKmalloc(BUFSIZ);
     415           1 :         if ( buf == NULL)
     416           0 :                 throw(MAL, "inspect.getSource", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     417           1 :         snprintf(buf,BUFSIZ,"%s.%s",*mod,*fcn);
     418           1 :         buf[0]=0;
     419             :         len= 0;
     420             :         lim= BUFSIZ;
     421             : 
     422           2 :         while (s) {
     423             :                 int i;
     424             :                 str ps;
     425             : 
     426           9 :                 for (i = 0; i < s->def->stop; i++) {
     427           8 :                         if((ps = instruction2str(s->def, 0, getInstrPtr(s->def, i), LIST_MAL_NAME )) == NULL) {
     428           0 :                                 GDKfree(buf);
     429           0 :                                 throw(MAL, "inspect.getSource", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     430             :                         }
     431           8 :                         if( strlen(ps) >= lim-len){
     432             :                                 /* expand the buffer */
     433             :                                 char *bn;
     434           0 :                                 bn= GDKrealloc(buf, lim+BUFSIZ);
     435           0 :                                 if ( bn == NULL) {
     436           0 :                                         GDKfree(ps);
     437           0 :                                         GDKfree(buf);
     438           0 :                                         throw(MAL, "inspect.getSource", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     439             :                                 }
     440             :                                 buf=bn;
     441             :                                 lim+= BUFSIZ;
     442             :                         }
     443           8 :                         strcat(buf+len,ps);
     444           8 :                         len+= strlen(ps);
     445           8 :                         buf[len++]='\n';
     446           8 :                         buf[len]=0;
     447           8 :                         GDKfree(ps);
     448             :                 }
     449           1 :                 s = s->peer;
     450             :         }
     451           1 :         *ret= buf;
     452           1 :         return MAL_SUCCEED;
     453             : }
     454             : 
     455             : static str
     456           0 : INSPECTatom_names(bat *ret)
     457             : {
     458             :         int i;
     459           0 :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
     460             : 
     461           0 :         if (b == 0)
     462           0 :                 throw(MAL, "inspect.getAtomNames", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     463             : 
     464           0 :         for (i = 0; i < GDKatomcnt; i++)
     465           0 :                 if (BUNappend(b, ATOMname(i), false) != GDK_SUCCEED)
     466           0 :                         goto bailout;
     467             : 
     468           0 :         if (pseudo(ret,b,"view","atom","name"))
     469           0 :                 goto bailout;
     470             : 
     471             :         return MAL_SUCCEED;
     472           0 :   bailout:
     473           0 :         BBPreclaim(b);
     474           0 :         throw(MAL, "inspect.getAtomNames", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     475             : }
     476             : 
     477             : static str
     478          33 : INSPECTgetEnvironment(bat *ret, bat *ret2)
     479             : {
     480             :         BAT *k, *v;
     481             : 
     482          33 :         if (GDKcopyenv(&k, &v, false) != GDK_SUCCEED)
     483           0 :                 throw(MAL, "inspect.getEnvironment", GDK_EXCEPTION);
     484             : 
     485          33 :         BBPkeepref(*ret = k->batCacheid);
     486          33 :         BBPkeepref(*ret2 = v->batCacheid);
     487          33 :         return MAL_SUCCEED;
     488             : }
     489             : 
     490             : static str
     491           6 : INSPECTgetEnvironmentKey(str *ret, str *key)
     492             : {
     493             :         const char *s;
     494           6 :         *ret = 0;
     495             : 
     496           6 :         s= GDKgetenv(*key);
     497           6 :         if (s == 0)
     498           0 :                 s= getenv(*key);
     499           6 :         if (s == 0)
     500           0 :                 throw(MAL, "inspect.getEnvironment", "environment variable '%s' not found", *key);
     501           6 :         *ret = GDKstrdup(s);
     502           6 :         if (*ret == NULL)
     503           0 :                 throw(MAL, "inspect.getEnvironment", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     504             :         return MAL_SUCCEED;
     505             : }
     506             : 
     507             : static str
     508           0 : INSPECTatom_sup_names(bat *ret)
     509             : {
     510             :         int i, k;
     511           0 :         BAT *b = COLnew(0, TYPE_str, 256, TRANSIENT);
     512             : 
     513           0 :         if (b == 0)
     514           0 :                 throw(MAL, "inspect.getAtomSuper", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     515             : 
     516           0 :         for (i = 0; i < GDKatomcnt; i++) {
     517           0 :                 for (k = ATOMstorage(i); k > TYPE_str; k = ATOMstorage(k))
     518             :                         ;
     519           0 :                 if (BUNappend(b, ATOMname(k), false) != GDK_SUCCEED)
     520           0 :                         goto bailout;
     521             :         }
     522             : 
     523           0 :         if (pseudo(ret,b,"view","atom","sup_name"))
     524           0 :                 goto bailout;
     525             : 
     526             :         return MAL_SUCCEED;
     527           0 :   bailout:
     528           0 :         BBPreclaim(b);
     529           0 :         throw(MAL, "inspect.getAtomSuper", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     530             : }
     531             : 
     532             : static str
     533           0 : INSPECTatom_sizes(bat *ret)
     534             : {
     535             :         int i;
     536             :         int s;
     537           0 :         BAT *b = COLnew(0, TYPE_int, 256, TRANSIENT);
     538             : 
     539           0 :         if (b == 0)
     540           0 :                 throw(MAL, "inspect.getAtomSizes", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     541             : 
     542           0 :         for (i = 0; i < GDKatomcnt; i++) {
     543           0 :                 s = ATOMsize(i);
     544           0 :                 if (BUNappend(b, &s, false) != GDK_SUCCEED)
     545           0 :                         goto bailout;
     546             :         }
     547             : 
     548           0 :         if (pseudo(ret,b,"view","atom","size"))
     549           0 :                 goto bailout;
     550             : 
     551             :         return MAL_SUCCEED;
     552           0 :   bailout:
     553           0 :         BBPreclaim(b);
     554           0 :         throw(MAL, "inspect.getAtomSizes", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     555             : }
     556             : 
     557             : /* calculate to trimmed storage space */
     558             : static lng
     559             : INSPECTcalcSize(MalBlkPtr mb){
     560             :         lng size,args=0,i;
     561             :         InstrPtr p;
     562             : 
     563          12 :         for(i=0;i<mb->stop; i++){
     564          10 :                 p= getInstrPtr(mb,i);
     565          10 :                 args += (p->argc-1)* sizeof(*p->argv);
     566             :         }
     567           2 :         size = (offsetof(InstrRecord, argv) +sizeof(InstrPtr)) * mb->stop;
     568           2 :         size += sizeof(VarRecord) * mb->vtop;
     569           2 :         size += args;
     570             :         return size;
     571             : }
     572             : 
     573             : static str
     574           2 : INSPECTgetSize(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p){
     575           2 :         lng *ret = getArgReference_lng(stk,p,0);
     576             : 
     577             : 
     578           2 :         *ret= INSPECTcalcSize(mb);
     579             :         (void) cntxt;
     580             :         (void) mb;
     581           2 :         return MAL_SUCCEED;
     582             : }
     583             : 
     584             : static str
     585           0 : INSPECTgetFunctionSize(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     586             : {
     587           0 :         lng *ret = getArgReference_lng(stk,pci,0);
     588           0 :         str *mod = getArgReference_str(stk,pci,1);
     589           0 :         str *fcn = getArgReference_str(stk,pci,2);
     590             :         Symbol s;
     591             :         (void) mb;
     592             : 
     593           0 :         s = findSymbol(cntxt->usermodule, getName(*mod), putName(*fcn));
     594           0 :         if (s == 0)
     595           0 :                 throw(MAL, "inspect.getSize", RUNTIME_SIGNATURE_MISSING);
     596           0 :         *ret= INSPECTcalcSize(s->def);
     597           0 :         return MAL_SUCCEED;
     598             : }
     599             : /*
     600             :  * Display routines
     601             :  */
     602             : 
     603             : #if 0
     604             : static str
     605             : INSPECTshowFunction(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p)
     606             : {
     607             :         (void) p;
     608             :         printFunction(cntxt->fdout, mb, stk, LIST_INPUT);
     609             :         return MAL_SUCCEED;
     610             : }
     611             : 
     612             : static str
     613             : INSPECTshowFunction3(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p)
     614             : {
     615             :         str modnme = getArgName(mb, p, 1);
     616             :         str fcnnme = getArgName(mb, p, 2);
     617             :         Symbol s = NULL;
     618             : 
     619             :         s = findSymbol(cntxt->usermodule,getName(modnme), putName(fcnnme));
     620             : 
     621             :         if (s == NULL){
     622             :                 char buf[BUFSIZ];
     623             :                 snprintf(buf,BUFSIZ,"%s.%s", modnme, fcnnme);
     624             :                 throw(MAL, "inspect.showSource",RUNTIME_SIGNATURE_MISSING "%s",buf);
     625             :         } else
     626             :                 printFunction(cntxt->fdout, s->def, stk, LIST_INPUT);
     627             :         return MAL_SUCCEED;
     628             : }
     629             : #endif
     630             : 
     631             : static str
     632           0 : INSPECTequalType(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     633             : {
     634             :         bit *ret;
     635             :         (void) stk;
     636             :         (void) cntxt;
     637           0 :         ret = getArgReference_bit(stk, pci, 0);
     638           0 :         *ret = getArgType(mb,pci,1)== getArgType(mb,pci,2);
     639           0 :         return MAL_SUCCEED;
     640             : }
     641             : 
     642             : static str
     643           8 : INSPECTtypeName(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     644             : {
     645             :         str *hn, *tn =0;
     646             : 
     647           8 :         hn = getArgReference_str(stk, pci, 0);
     648             : 
     649             :         (void) cntxt;
     650           8 :         if( pci->retc== 2){
     651           0 :                 tn = getArgReference_str(stk, pci, 1);
     652           0 :                 *hn = getTypeName(TYPE_oid);
     653           0 :                 *tn = getTypeName(getBatType(getArgType(mb, pci, 2)));
     654           8 :         } else if (isaBatType(getArgType(mb,pci,1) ) ){
     655             :                 bat *bid= getArgReference_bat(stk,pci,1);
     656             :                 BAT *b;
     657           2 :                 if ((b = BATdescriptor(*bid)) ) {
     658           2 :                         *hn = getTypeName(newBatType(b->ttype));
     659           2 :                         BBPunfix(b->batCacheid);
     660             :                 } else
     661           0 :                         *hn = getTypeName(getArgType(mb, pci, 1));
     662             :         } else
     663           6 :                 *hn = getTypeName(getArgType(mb, pci, 1));
     664           8 :         return MAL_SUCCEED;
     665             : }
     666             : 
     667             : #include "mel.h"
     668             : mel_func inspect_init_funcs[] = {
     669             :  pattern("inspect", "getDefinition", INSPECTgetDefinition, false, "Returns a string representation of a specific function.", args(1,3, batarg("",str),arg("mod",str),arg("fcn",str))),
     670             :  pattern("inspect", "getSignature", INSPECTgetSignature, false, "Returns the function signature(s).", args(1,3, batarg("",str),arg("mod",str),arg("fcn",str))),
     671             :  pattern("inspect", "getComment", INSPECTgetComment, false, "Returns the function help information.", args(1,3, batarg("",str),arg("mod",str),arg("fcn",str))),
     672             :  pattern("inspect", "getSource", INSPECTgetSource, false, "Return the original input for a function.", args(1,3, arg("",str),arg("mod",str),arg("fcn",str))),
     673             :  pattern("inspect", "getKind", INSPECTgetkind, false, "Obtain the instruction kind.", args(1,1, batarg("",str))),
     674             :  pattern("inspect", "getModule", INSPECTgetAllModules, false, "Obtain the function name.", args(1,1, batarg("",str))),
     675             :  pattern("inspect", "getFunction", INSPECTgetAllFunctions, false, "Obtain the function name.", args(1,1, batarg("",str))),
     676             :  pattern("inspect", "getSignatures", INSPECTgetAllSignatures, false, "Obtain the function signatures.", args(1,1, batarg("",str))),
     677             :  pattern("inspect", "getSize", INSPECTgetSize, false, "Return the storage size for the current function (in bytes).", args(1,1, arg("",lng))),
     678             :  pattern("inspect", "getSize", INSPECTgetFunctionSize, false, "Return the storage size for a function (in bytes).", args(1,3, arg("",lng),arg("mod",str),arg("fcn",str))),
     679             :  pattern("inspect", "getType", INSPECTtypeName, false, "Return the concrete type of a variable (expression).", args(1,2, arg("",str),argany("v",1))),
     680             :  pattern("inspect", "equalType", INSPECTequalType, false, "Return true if both operands are of the same type", args(1,3, arg("",bit),argany("l",0),argany("r",0))),
     681             :  command("inspect", "getAtomNames", INSPECTatom_names, false, "Collect a BAT with the atom names.", args(1,1, batarg("",str))),
     682             :  command("inspect", "getAtomSuper", INSPECTatom_sup_names, false, "Collect a BAT with the atom names.", args(1,1, batarg("",str))),
     683             :  command("inspect", "getAtomSizes", INSPECTatom_sizes, false, "Collect a BAT with the atom sizes.", args(1,1, batarg("",int))),
     684             :  command("inspect", "getEnvironment", INSPECTgetEnvironment, false, "Collect the environment variables.", args(2,2, batarg("k",str),batarg("v",str))),
     685             :  command("inspect", "getEnvironment", INSPECTgetEnvironmentKey, false, "Get the value of an environemnt variable", args(1,2, arg("",str),arg("k",str))),
     686             :  { .imp=NULL }
     687             : };
     688             : #include "mal_import.h"
     689             : #ifdef _MSC_VER
     690             : #undef read
     691             : #pragma section(".CRT$XCU",read)
     692             : #endif
     693         255 : LIB_STARTUP_FUNC(init_inspect_mal)
     694         255 : { mal_module("inspect", NULL, inspect_init_funcs); }

Generated by: LCOV version 1.14