1 /*
2    Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include <ndb_global.h>
26 #include <BaseString.hpp>
27 
28 #include "DebuggerNames.hpp"
29 
30 #include <BlockNumbers.h>
31 #include <BlockNames.hpp>
32 #include <GlobalSignalNumbers.h>
33 #include <signaldata/SignalDataPrint.hpp>
34 
35 static const char *            localSignalNames[MAX_GSN+1];
36 static SignalDataPrintFunction localPrintFunctions[MAX_GSN+1];
37 static const char *            localBlockNames[NO_OF_BLOCKS];
38 
39 static
40 int
initSignalNames(const char * dst[],const GsnName src[],unsigned short len)41 initSignalNames(const char * dst[], const GsnName src[], unsigned short len){
42   unsigned i;
43   for(i = 0; i<=MAX_GSN; i++)
44     dst[i] = 0;
45 
46   for(i = 0; i<len; i++){
47     unsigned short gsn = src[i].gsn;
48     const char * name  = src[i].name;
49 
50     if(dst[gsn] != 0 && name != 0){
51       if(strcmp(dst[gsn], name) != 0){
52 	fprintf(stderr,
53 		"Multiple definition of signal name for gsn: %d (%s, %s)\n",
54 		gsn, dst[gsn], name);
55 	exit(0);
56       }
57     }
58     dst[gsn] = name;
59   }
60   return 0;
61 }
62 
63 static
64 int
initSignalPrinters(SignalDataPrintFunction dst[],const NameFunctionPair src[])65 initSignalPrinters(SignalDataPrintFunction dst[],
66 		   const NameFunctionPair src[]){
67   unsigned i;
68   for(i = 0; i<=MAX_GSN; i++)
69     dst[i] = 0;
70 
71   unsigned short gsn;
72   for(i = 0; (gsn = src[i].gsn) > 0; i++){
73     SignalDataPrintFunction fun = src[i].function;
74 
75     if(dst[gsn] != 0 && fun != 0){
76       if(dst[gsn] != fun){
77 	fprintf(stderr,
78 		"Multiple definition of signal print function for gsn: %d\n",
79 		gsn);
80 	exit(0);
81       }
82     }
83     dst[gsn] = fun;
84   }
85   return 0;
86 }
87 
88 static
89 int
initBlockNames(const char * dst[],const BlockName src[],unsigned len)90 initBlockNames(const char * dst[],
91 	       const BlockName src[],
92 	       unsigned len){
93   unsigned i;
94   for(i = 0; i<NO_OF_BLOCKS; i++)
95     dst[i] = 0;
96 
97   for(i = 0; i<len; i++){
98     const int index = src[i].number - MIN_BLOCK_NO;
99     if((index < 0 && index >= NO_OF_BLOCKS) || dst[index] != 0){
100       fprintf(stderr,
101 	      "Invalid block name definition: %d %s\n",
102 	      src[i].number, src[i].name);
103       exit(0);
104     }
105     dst[index] = src[i].name;
106   }
107   return 0;
108 }
109 
110 /**
111  * Run static initializer
112  */
113 static const int
114 xxx_DUMMY_SIGNAL_NAMES_xxx = initSignalNames(localSignalNames,
115 					     SignalNames,
116 					     NO_OF_SIGNAL_NAMES);
117 static const int
118 xxx_DUMMY_PRINT_FUNCTIONS_xxx  = initSignalPrinters(localPrintFunctions,
119 						    SignalDataPrintFunctions);
120 
121 static const int
122 xxx_DUMMY_BLOCK_NAMES_xxx = initBlockNames(localBlockNames,
123 					   BlockNames,
124 					   NO_OF_BLOCK_NAMES);
125 
126 const char *
getSignalName(unsigned short gsn,const char * defVal)127 getSignalName(unsigned short gsn, const char * defVal){
128   if(gsn > 0 && gsn <= MAX_GSN)
129     return (localSignalNames[gsn] ? localSignalNames[gsn] : defVal);
130   return defVal;
131 }
132 
133 unsigned short
getGsn(const char * signalName)134 getGsn(const char * signalName){
135   return 0;
136 }
137 
138 const char *
getBlockName(unsigned short blockNo,const char * ret)139 getBlockName(unsigned short blockNo, const char * ret){
140   if(blockNo >= MIN_BLOCK_NO && blockNo <= MAX_BLOCK_NO)
141     return localBlockNames[blockNo-MIN_BLOCK_NO];
142   if (ret == 0) {
143     static char buf[20];
144     BaseString::snprintf(buf, sizeof(buf), "BLOCK#%d", (int)blockNo);
145     return buf;
146   }
147   return ret;
148 }
149 
150 unsigned short
getBlockNo(const char * blockName)151 getBlockNo(const char * blockName){
152   for(int i = 0; i<NO_OF_BLOCKS; i++)
153     if(localBlockNames[i] != 0 && strcmp(localBlockNames[i], blockName) == 0)
154       return i + MIN_BLOCK_NO;
155   return 0;
156 }
157 
158 SignalDataPrintFunction
findPrintFunction(unsigned short gsn)159 findPrintFunction(unsigned short gsn){
160   if(gsn > 0 && gsn <= MAX_GSN)
161     return localPrintFunctions[gsn];
162   return 0;
163 }
164