Add -j/--jul to lttng UI and ABI
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <limits.h>
23
24 #include <common/error.h>
25 #include <common/utils.h>
26
27 #include "conf.h"
28 #include "utils.h"
29 #include "command.h"
30
31 static const char *str_kernel = "Kernel";
32 static const char *str_ust = "UST";
33 static const char *str_jul = "JUL";
34
35 /*
36 * get_session_name
37 *
38 * Return allocated string with the session name found in the config
39 * directory.
40 */
41 char *get_session_name(void)
42 {
43 char *path, *session_name = NULL;
44
45 /* Get path to config file */
46 path = utils_get_home_dir();
47 if (path == NULL) {
48 goto error;
49 }
50
51 /* Get session name from config */
52 session_name = config_read_session_name(path);
53 if (session_name == NULL) {
54 goto error;
55 }
56
57 DBG2("Config file path found: %s", path);
58 DBG("Session name found: %s", session_name);
59 return session_name;
60
61 error:
62 return NULL;
63 }
64
65 /*
66 * list_commands
67 *
68 * List commands line by line. This is mostly for bash auto completion and to
69 * avoid difficult parsing.
70 */
71 void list_commands(struct cmd_struct *commands, FILE *ofp)
72 {
73 int i = 0;
74 struct cmd_struct *cmd = NULL;
75
76 cmd = &commands[i];
77 while (cmd->name != NULL) {
78 fprintf(ofp, "%s\n", cmd->name);
79 i++;
80 cmd = &commands[i];
81 }
82 }
83
84 /*
85 * list_cmd_options
86 *
87 * Prints a simple list of the options available to a command. This is intended
88 * to be easily parsed for bash completion.
89 */
90 void list_cmd_options(FILE *ofp, struct poptOption *options)
91 {
92 int i;
93 struct poptOption *option = NULL;
94
95 for (i = 0; options[i].longName != NULL; i++) {
96 option = &options[i];
97
98 fprintf(ofp, "--%s\n", option->longName);
99
100 if (isprint(option->shortName)) {
101 fprintf(ofp, "-%c\n", option->shortName);
102 }
103 }
104 }
105
106 /*
107 * fls: returns the position of the most significant bit.
108 * Returns 0 if no bit is set, else returns the position of the most
109 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
110 */
111 #if defined(__i386) || defined(__x86_64)
112 static inline
113 unsigned int fls_u32(uint32_t x)
114 {
115 int r;
116
117 asm("bsrl %1,%0\n\t"
118 "jnz 1f\n\t"
119 "movl $-1,%0\n\t"
120 "1:\n\t"
121 : "=r" (r) : "rm" (x));
122 return r + 1;
123 }
124 #define HAS_FLS_U32
125 #endif
126
127 #if defined(__x86_64)
128 static inline
129 unsigned int fls_u64(uint64_t x)
130 {
131 long r;
132
133 asm("bsrq %1,%0\n\t"
134 "jnz 1f\n\t"
135 "movq $-1,%0\n\t"
136 "1:\n\t"
137 : "=r" (r) : "rm" (x));
138 return r + 1;
139 }
140 #define HAS_FLS_U64
141 #endif
142
143 #ifndef HAS_FLS_U64
144 static __attribute__((unused))
145 unsigned int fls_u64(uint64_t x)
146 {
147 unsigned int r = 64;
148
149 if (!x)
150 return 0;
151
152 if (!(x & 0xFFFFFFFF00000000ULL)) {
153 x <<= 32;
154 r -= 32;
155 }
156 if (!(x & 0xFFFF000000000000ULL)) {
157 x <<= 16;
158 r -= 16;
159 }
160 if (!(x & 0xFF00000000000000ULL)) {
161 x <<= 8;
162 r -= 8;
163 }
164 if (!(x & 0xF000000000000000ULL)) {
165 x <<= 4;
166 r -= 4;
167 }
168 if (!(x & 0xC000000000000000ULL)) {
169 x <<= 2;
170 r -= 2;
171 }
172 if (!(x & 0x8000000000000000ULL)) {
173 x <<= 1;
174 r -= 1;
175 }
176 return r;
177 }
178 #endif
179
180 #ifndef HAS_FLS_U32
181 static __attribute__((unused))
182 unsigned int fls_u32(uint32_t x)
183 {
184 unsigned int r = 32;
185
186 if (!x)
187 return 0;
188 if (!(x & 0xFFFF0000U)) {
189 x <<= 16;
190 r -= 16;
191 }
192 if (!(x & 0xFF000000U)) {
193 x <<= 8;
194 r -= 8;
195 }
196 if (!(x & 0xF0000000U)) {
197 x <<= 4;
198 r -= 4;
199 }
200 if (!(x & 0xC0000000U)) {
201 x <<= 2;
202 r -= 2;
203 }
204 if (!(x & 0x80000000U)) {
205 x <<= 1;
206 r -= 1;
207 }
208 return r;
209 }
210 #endif
211
212 static
213 unsigned int fls_ulong(unsigned long x)
214 {
215 #if (CAA_BITS_PER_LONG == 32)
216 return fls_u32(x);
217 #else
218 return fls_u64(x);
219 #endif
220 }
221
222 /*
223 * Return the minimum order for which x <= (1UL << order).
224 * Return -1 if x is 0.
225 */
226 int get_count_order_u32(uint32_t x)
227 {
228 if (!x)
229 return -1;
230
231 return fls_u32(x - 1);
232 }
233
234 /*
235 * Return the minimum order for which x <= (1UL << order).
236 * Return -1 if x is 0.
237 */
238 int get_count_order_u64(uint64_t x)
239 {
240 if (!x)
241 return -1;
242
243 return fls_u64(x - 1);
244 }
245
246 /*
247 * Return the minimum order for which x <= (1UL << order).
248 * Return -1 if x is 0.
249 */
250 int get_count_order_ulong(unsigned long x)
251 {
252 if (!x)
253 return -1;
254
255 return fls_ulong(x - 1);
256 }
257
258 const char *get_domain_str(enum lttng_domain_type domain)
259 {
260 const char *str_dom;
261
262 switch (domain) {
263 case LTTNG_DOMAIN_KERNEL:
264 str_dom = str_kernel;
265 break;
266 case LTTNG_DOMAIN_UST:
267 str_dom = str_ust;
268 break;
269 case LTTNG_DOMAIN_JUL:
270 str_dom = str_jul;
271 break;
272 default:
273 /* Should not have an unknown domain or else define it. */
274 assert(0);
275 }
276
277 return str_dom;
278 }
This page took 0.035028 seconds and 5 git commands to generate.