7f78195978ccc923d1d46a8e22607c1b6fbcfb65
[platform/core/system/swap-manager.git] / daemon / da_protocol.h
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9  * Nikita Kalyazin    <n.kalyazin@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Contributors:
24  * - Samsung RnD Institute Russia
25  *
26  */
27
28
29 #ifndef _DA_PROTOCOL_
30 #define _DA_PROTOCOL_
31
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <stddef.h>
37 #include <linux/input.h>
38 #include <linux/limits.h>
39 #include <assert.h>
40
41 #define PROTOCOL_VERSION "4.2"
42
43 enum {
44         MAX_RAW_MESSAGE_SIZE = 8*1024,
45 };
46
47 #include "md5.h"
48 #include "da_msg_ids.h"
49
50 enum HostMessageT {
51
52 /* msg ids */
53         #define X(id, val) id = val,
54         MSG_ID_LIST
55         #undef X
56
57         NMSG_WRT_LAUNCHER_PORT                  =0x8001,
58         NMSG_ACK_FLAG                           =0x1000,
59
60 /* msg ACK */
61         #define X(id, val) id ## _ACK = val + NMSG_ACK_FLAG,
62         MSG_ID_LIST
63         #undef X
64 };
65
66
67 enum DataMessageT {
68         #define X(id, val) id = val,
69         DATA_MSG_ID_LIST
70         #undef X
71 };
72
73 #define MSG_MAX_NUM NMSG_SWAP_INST_REMOVE
74
75 enum ErrorCode {
76         ERR_NO                                          = 0,    /* success */
77         ERR_LOCKFILE_CREATE_FAILED                      = -101, /* lock file create failed */
78         ERR_ALREADY_RUNNING                             = -102, /* already running */
79         ERR_INITIALIZE_SYSTEM_INFO_FAILED               = -103, /* initialize system info failed */
80         ERR_HOST_SERVER_SOCKET_CREATE_FAILED            = -104, /* host server socket create failed */
81         ERR_TARGET_SERVER_SOCKET_CREATE_FAILED          = -105, /* target server socket create failed */
82         ERR_UI_TARGET_SERVER_SOCKET_CREATE_FAILED       = -107, /* ui target server socket create failed */
83
84         ERR_SIGNAL_MASK_SETTING_FAILED                  = -106, /* TODO del (old parametr) */
85
86         ERR_WRONG_MESSAGE_FORMAT                        = -201, /* wrong message format */
87         ERR_WRONG_MESSAGE_TYPE                          = -202, /* wrong message type */
88         ERR_WRONG_MESSAGE_DATA                          = -203, /* wrong message data */
89         ERR_CANNOT_START_PROFILING                      = -204, /* cannot start profiling */
90         ERR_TO_LONG_MESSAGE                             = -205, /* message is too long to process */
91         ERR_TARGET_NOT_FOUND                            = -206, /* some target in message not found like file or some else */
92         ERR_UI_OBJ_NOT_FOUND                            = -207, /* requested ui object is not found */
93         ERR_UI_OBJ_SCREENSHOT_FAILED                    = -208, /* taking ui screenshot failed because App is in background */
94         ERR_NOT_SUPPORTED                               = -800, /* request not supported by security reason */
95         ERR_SERV_SOCK_CREATE                            = -900, /* server socket creation failed (written in /tmp/da.port file) */
96         ERR_SERV_SOCK_BIND                              = -901, /* server socket bind failed (written in /tmp/da.port file) */
97         ERR_SERV_SOCK_LISTEN                            = -902, /* server socket listen failed (written in /tmp/da.port file) */
98         ERR_UNKNOWN                                     = -999  /* unknown error */
99 };
100
101 #define FL_SYSTEM_ENERGY_OLD (1<<26)
102
103 enum feature_code_0 {
104         FL_RESERVED1                            = 0x0000000000003ULL, // reserved 0011
105
106         FL_FUNCTION_PROFILING                   = 0x0000000000004ULL, // 0x4 * 0x10^00 On/Off the UserSpaceInst
107         FL_MEMORY_ALLOC_PROBING                 = 0x0000000000008ULL, // 0x8 * 0x10^00 memory allocation API (glibc)
108         FL_FILE_API_PROBING                     = 0x0000000000010ULL, // 0x1 * 0x10^01 file API (glibc, OSP)
109         FL_THREAD_API_PROBING                   = 0x0000000000020ULL, // 0x2 * 0x10^01 thread API (glibc, OSP)
110         FL_OSP_UI_API_PROBING                   = 0x0000000000040ULL, // 0x4 * 0x10^01 UI API (OSP)
111         FL_SCREENSHOT                           = 0x0000000000080ULL, // 0x8 * 0x10^01 Screenshot
112         FL_USER_EVENT                           = 0x0000000000100ULL, // 0x1 * 0x10^02 events of Touch, Gesture, Orientation, Key
113         FL_RECORDING                            = 0x0000000000200ULL, // 0x2 * 0x10^02 recording the user event
114         FL_SYSTCALL_FILE                        = 0x0000000000400ULL, // 0x4 * 0x10^02 File operation syscalls tracing
115         FL_SYSTCALL_IPC                         = 0x0000000000800ULL, // 0x8 * 0x10^02 IPC syscall tracing
116         FL_SYSTCALL_PROCESS                     = 0x0000000001000ULL, // 0x1 * 0x10^03 Process syscalls tracing
117         FL_SYSTCALL_SIGNAL                      = 0x0000000002000ULL, // 0x2 * 0x10^03 Signal syscalls tracing
118         FL_SYSTCALL_NETWORK                     = 0x0000000004000ULL, // 0x4 * 0x10^03 Network syscalls tracing
119         FL_SYSTCALL_DESC                        = 0x0000000008000ULL, // 0x8 * 0x10^03 Descriptor syscalls tracing
120         FL_CONTEXT_SWITCH                       = 0x0000000010000ULL, // 0x1 * 0x10^04 Context switch tracing
121         FL_NETWORK_API_PROBING                  = 0x0000000020000ULL, // 0x2 * 0x10^04 network API (glibc, OSP, libsoap, openssl)
122         FL_OPENGL_API_PROBING                   = 0x0000000040000ULL, // 0x4 * 0x10^04 openGL API
123         FL_FUNCTION_SAMPLING                    = 0x0000000080000ULL, // 0x8 * 0x10^04 Function sampling
124
125         FL_RESERVED2                            = 0x000000FF00000ULL, // 0x1 * 0x10^00 reserved
126
127         FL_MEMORY_ALLOC_ALWAYS_PROBING          = 0x0000010000000ULL, // 0x1 * 0x10^07 all (include external) memory allocation API (glibc) always
128         FL_FILE_API_ALWAYS_PROBING              = 0x0000020000000ULL, // 0x2 * 0x10^07 all (include external) file API (glibc, OSP) always
129         FL_THREAD_API_ALWAYS_PROBING            = 0x0000040000000ULL, // 0x4 * 0x10^07 all (include external) thread API (glibc, OSP) always
130         FL_OSP_UI_API_ALWAYS_PROBING            = 0x0000080000000ULL, // 0x8 * 0x10^07 all (include external) UI API (OSP) always
131         FL_NETWORK_API_ALWAYS_PROBING           = 0x0000100000000ULL, // 0x1 * 0x10^08 all (include external) network API (glibc, OSP, libsoap, openssl) always
132         FL_OPENGL_API_ALWAYS_PROBING            = 0x0000200000000ULL, // 0x2 * 0x10^08 all (include external) openGL API always
133
134         FL_RESERVED3                            = 0x0000c00000000ULL, // 0x1 * 0x10^00 reserved
135
136         FL_SYSTEM_CPU                           = 0x0001000000000ULL, // 0x1 * 0x10^09 CPU core load, frequency
137         FL_SYSTEM_MEMORY                        = 0x0002000000000ULL, // 0x2 * 0x10^09 System memory used
138         FL_SYSTEM_PROCESS                       = 0x0004000000000ULL, // 0x4 * 0x10^09 Info for profilling processes (VSS, PSS, RSS, etc)
139         FL_SYSTEM_THREAD_LOAD                   = 0x0008000000000ULL, // 0x8 * 0x10^09 Thread loading for profiling processes
140         FL_SYSTEM_PROCESSES_LOAD                = 0x0010000000000ULL, // 0x1 * 0x10^10 Non instrumented process load
141         FL_SYSTEM_DISK                          = 0x0020000000000ULL, // 0x2 * 0x10^10 /proc/diskstats - reads, sectors read, writes, sectors written
142         FL_SYSTEM_NETWORK                       = 0x0040000000000ULL, // 0x4 * 0x10^10 network send/recv size
143         FL_SYSTEM_DEVICE                        = 0x0080000000000ULL, // 0x8 * 0x10^10
144         FL_SYSTEM_ENERGY                        = 0x0100000000000ULL, // 0x1 * 0x10^11
145
146         FL_APP_STARTUP                          = 0x0200000000000ULL, // 0x2 * 0x10^11 see MSG_APP_SETUP_STAGE in data channel
147         FL_WEB_PROFILING                        = 0x0400000000000ULL, // 0x4 * 0x10^11 Web profiling
148         FL_WEB_STARTUP_PROFILING                = 0x0800000000000ULL, // 0x8 * 0x10^11 Web startup profiling
149
150         FL_SYSTEM_FILE_ACTIVITY                 = 0x1000000000000ULL, // 0x1 * 0x10^12 function entry/exit for probe type 04 (File syscall)
151
152         FL_UI_VIEWER_PROFILING                  = 0x2000000000000ULL, // 0x2 * 0x10^12 load ui viewer library
153         FL_LSAN_ENABLED                         = 0x4000000000000ULL, // 0x4 * 0x10^12 LSan enabled
154
155         FL_RESERVED4                            = 0x8000000000000ULL, // reserved 1100
156
157         FL_ALL_FEATURES_0                       = 0x7FFFFFFFFFFFFULL &
158                                                   (~FL_RESERVED1) &
159                                                   (~FL_RESERVED2) &
160                                                   (~FL_RESERVED3) &
161                                                   (~FL_RESERVED4)
162
163 };
164
165 enum feature_code_1 {
166         FL_MEMORY_ALLOC_PROBING_PRELOAD         = 0x0000000000001ULL, // 0x1 memory allocation API (glibc) based on preload
167         FL_FILE_API_PROBING_PRELOAD             = 0x0000000000002ULL, // 0x2 file API (glibc, OSP) based on preload
168         FL_THREAD_API_PROBING_PRELOAD           = 0x0000000000004ULL, // 0x4 thread API (glibc, OSP) based on preload
169         FL_NETWORK_API_PROBING_PRELOAD          = 0x0000000000008ULL, // 0x8 network API (glibc, OSP, libsoap, openssl) based on preload
170         FL_OPENGL_API_PROBING_PRELOAD           = 0x0000000000010ULL, // 0x10 openGL API based on preload
171
172         FL_MEMORY_ALLOC_ALWAYS_PROBING_PRELOAD  = 0x0000000000020ULL, // 0x20 all (include external) memory allocation API (glibc) always based on preload
173         FL_FILE_API_ALWAYS_PROBING_PRELOAD      = 0x0000000000040ULL, // 0x40 all (include external) file API (glibc, OSP) always based on preload
174         FL_THREAD_API_ALWAYS_PROBING_PRELOAD    = 0x0000000000080ULL, // 0x80 all (include external) thread API (glibc, OSP) always based on preload
175         FL_NETWORK_API_ALWAYS_PROBING_PRELOAD   = 0x0000000000100ULL, // 0x100 all (include external) network API (glibc, OSP, libsoap, openssl) always based on preload
176         FL_OPENGL_API_ALWAYS_PROBING_PRELOAD    = 0x0000000000200ULL, // 0x200 all (include external) openGL API always based on preload
177
178         FL_ALL_FEATURES_1                       = 0x00000000003FFULL, /* all */
179 };
180
181 enum probe_type {
182         SWAP_RETPROBE   = 0, //Common retprobe
183         SWAP_FBI_PROBE  = 1, //Function body instrumentation probe
184         SWAP_LD_PROBE   = 2, //Preloaded API probe
185         SWAP_WEBPROBE   = 3, //Webprobe
186 };
187
188 #define IS_OPT_SET_IN(OPT, reg) (reg & (OPT))
189 #define IS_OPT_SET(OPT) IS_OPT_SET_IN((OPT), prof_session.conf.use_features0)
190
191 enum supported_device {
192         DEVICE_FLASH,
193         DEVICE_CPU,
194         DEVICE_LCD,
195         DEVICE_WIFI,
196         DEVICE_BT
197 };
198 static const char *supported_devices_strings[] = {
199         "FLASH",
200         "CPU",
201         "LCD",
202         "WIFI",
203         "BT"
204 };
205 #define array_size(x) (sizeof(x)/sizeof((x)[0]))
206 enum { supported_devices_count = array_size(supported_devices_strings) };
207
208 #define TARGER_MSG_MAX_LEN                      4096
209 #define HOST_CTL_MSG_MAX_LEN                    (10 * 1024 * 1024)
210
211 struct _msg_target_t {
212         unsigned int type;
213         unsigned int length;
214         char data[0];
215 };
216
217 struct msg_target_t {
218         unsigned int type;
219         unsigned int length;
220         char data[TARGER_MSG_MAX_LEN];
221 };
222
223 enum { MSG_HEADER_LEN = offsetof(struct msg_target_t, data) };
224
225
226 #define MAX_FILENAME 128
227
228 #define MSG_DATA_HDR_LEN 20
229 struct msg_data_t {
230         uint32_t id;
231         uint32_t seq_num;
232         uint32_t sec;
233         uint32_t nsec;
234         uint32_t len;
235         char payload[0];
236 };
237
238 #define MSG_CMD_HDR_LEN 8
239 //conf
240 struct msg_buf_t {
241         char *payload;
242         char *cur_pos;
243         char *end;
244         uint32_t len;
245 };
246
247 struct msg_t {
248         uint32_t id;
249         uint32_t len;
250         char payload[0];
251 };
252
253
254 struct conf_t {
255         uint64_t use_features0;
256         uint64_t use_features1;
257         uint32_t system_trace_period;
258         uint32_t data_message_period;
259 };
260
261 typedef uint32_t log_interval_t;
262
263 //app, libs, probes
264 enum app_type_t {
265         APP_TYPE_UNKNOWN = 0,
266         APP_TYPE_TIZEN = 1,
267         APP_TYPE_RUNNING = 2,
268         APP_TYPE_COMMON = 3,
269         APP_TYPE_WEB = 4
270 };
271
272 struct app_info_t {
273         uint32_t app_type;
274         char *app_id;
275         char *exe_path;
276
277         struct {
278                 size_t size;
279                 char data[8];
280         } setup_data;
281 };
282
283
284
285 struct us_func_inst_plane_t {
286         //format
287         //name       | type   | len       | info
288         //------------------------------------------
289         //func_addr  | uint64 | 8         |
290         //probe_type | char   | 1         |
291         uint64_t func_addr;
292         char probe_type;
293         char probe_info[0];
294 } __attribute__ ((packed));
295
296 struct us_lib_inst_t {
297         char *bin_path;
298 };
299
300 struct user_space_inst_t {
301         uint32_t app_num;
302         struct app_list_t *app_inst_list;
303         uint32_t lib_num;
304         struct lib_list_t *lib_inst_list;
305         uint32_t ld_lib_num;
306         struct lib_list_t *ld_lib_inst_list;
307 };
308
309 //replays
310 struct replay_event_t {
311         uint32_t id;
312         struct input_event ev;
313 };
314
315 struct replay_event_seq_t {
316         uint32_t enabled;
317         struct timeval tv;
318         uint32_t event_num;
319         struct replay_event_t *events;
320 };
321
322 struct prof_session_t {
323         struct conf_t conf;
324         struct user_space_inst_t user_space_inst;
325         struct replay_event_seq_t replay_event_seq;
326         unsigned running_status:1; // to stop properly (1 - it is running, 0 - no)
327 };
328
329 int parseHostMessage(struct msg_t *log, char *msg);
330 int host_message_handler(struct msg_t *msg);
331
332 char *msg_ID_str(enum HostMessageT ID);
333
334 // testing
335 #include <sys/stat.h>
336 #include <fcntl.h>
337 #include <unistd.h>
338
339 //data protocol
340 struct thread_info_t {
341         uint32_t pid;
342         float load;
343 };
344
345 struct other_process_info_t {
346         uint32_t id;
347         float load;
348 };
349
350 struct inst_process_info_t {
351         uint32_t id;
352         float load;
353         uint64_t virtual_memory;
354         uint64_t resident_memory;
355         uint64_t shared_memory;
356         uint64_t pss_memory;
357         uint64_t total_alloc_size;
358         uint32_t count_of_threads;
359
360 };
361
362
363 /**
364  * NOTE:
365  *
366  * Adding pointer memebers to struct %system_info_t REQUIRES updating code,
367  * calculating length of bytevector, suitable for serializing it. See
368  * msg_data_payload_length()
369  *
370  */
371 struct system_info_t {
372         // system_cpu
373         float *cpu_frequency;
374         float *cpu_load;
375
376         // total thread count to calculate msg size
377         uint32_t count_of_threads;
378
379         // system_memory
380         uint64_t system_memory_used;
381         uint64_t system_total_gem_memory;
382
383         // system_processes
384         uint32_t count_of_inst_processes;
385         uint32_t count_of_other_processes;
386
387         //system fd
388         uint32_t fd_count;
389         uint32_t fd_peration_count;
390
391         // system_disk
392         uint32_t total_used_drive;
393         uint32_t disk_reads;
394         uint32_t disk_bytes_read;
395         uint32_t disk_writes;
396         uint32_t disk_bytes_write;
397
398         // system_network
399         uint32_t network_send_size;
400         uint32_t network_receive_size;
401
402         // system_device
403         uint32_t wifi_status;
404         uint32_t bt_status;
405         uint32_t gps_status;
406         uint32_t brightness_status;
407         uint32_t camera_status;
408         uint32_t sound_status;
409         uint32_t audio_status;
410         uint32_t vibration_status;
411         uint32_t voltage_status;
412         uint32_t rssi_status;
413         uint32_t video_status;
414         uint32_t call_status;
415         uint32_t dnet_status;
416
417         // system_energy
418         uint32_t energy;
419         uint32_t energy_per_device[supported_devices_count];
420         uint32_t app_energy_per_device[supported_devices_count];
421 };
422
423 struct recorded_event_t {
424         uint32_t id;
425         uint32_t type;
426         uint32_t code;
427         uint32_t value;
428 };
429
430 #ifndef da_static_assert
431 #define da_static_assert(cond) \
432         char __attribute__((unused)) __static_assert[(cond) ? 1 : -1];
433 #endif
434
435 #define pack_int64(to, n) do {                                          \
436                 da_static_assert(sizeof(n) == 8);                               \
437                 *(uint64_t *)to = (n);                                  \
438                 to += sizeof(uint64_t);                                 \
439         } while (0)
440
441 #define pack_int32(to, n) do {                                          \
442                 da_static_assert(sizeof(n) == 4);                               \
443                 *(uint32_t *)to = (n);                                  \
444                 to += sizeof(uint32_t);                                 \
445         } while (0)
446
447 #define pack_int8(to, n) do {                                           \
448                 da_static_assert(sizeof(n) == 1);                               \
449                 *(uint8_t *)to = (n);                                   \
450                 to += sizeof(uint8_t);                                  \
451         } while (0)
452
453 #define pack_time(to, n)                                                \
454         do {                                                            \
455                 pack_int32(to, n.tv_sec);                               \
456                 pack_int32(to, n.tv_usec);                              \
457         } while (0)
458
459 #define pack_float(to, n)                                       \
460         do {                                                    \
461                 *(float *)to = n;                               \
462                 to += sizeof(float);                            \
463         } while (0)
464
465 #define pack_str(to, n)                         \
466         do {                                    \
467                 memcpy(to, n, strlen(n) + 1);   \
468                 to = (char *)to + strlen(n) + 1;        \
469         } while (0)
470
471 #define pack_path_with_len(to, n)               \
472         do {                                    \
473                 uint32_t len;                   \
474                 len = strnlen(n, PATH_MAX) + 1; \
475                 pack_int32(to, len);            \
476                 memcpy(to, n, len);             \
477                 to += len;                      \
478         } while (0)
479
480 static inline void* pack_str_array(void *buffer, const char **strings,
481                                    size_t count)
482 {
483         size_t index;
484         for (index = 0; index != count; ++index)
485                 pack_str(buffer, strings[index]);
486         return buffer;
487 }
488
489 struct msg_data_t *pack_system_info(struct system_info_t *sys_info);
490 int write_to_buf(struct msg_data_t *msg);
491 void free_msg_data(struct msg_data_t *msg);
492 void free_msg_payload(struct msg_t *msg);
493 void free_sys_info(struct system_info_t *sys_info);
494 int start_replay(void);
495 void stop_replay(void);
496
497 enum ErrorCode stop_all(void);
498 enum ErrorCode stop_all_no_lock(void);
499 int stop_all_in_process(void);
500 void stop_all_done(void);
501
502 void reset_msg(struct msg_t *msg);
503 void reset_replay_event_seq(struct replay_event_seq_t *res);
504 void reset_system_info(struct system_info_t *sys);
505 int check_running_status(const struct prof_session_t *prof_session);
506
507 void prepare_config_msg(struct msg_target_t *msg);
508
509 extern struct prof_session_t prof_session;
510
511 int send_msg_to_sock(int sock, struct msg_target_t *msg);
512 int recv_msg_from_sock(int sock, struct msg_target_t *msg, size_t data_len);
513
514 //debugs
515 void print_replay_event(struct replay_event_t *ev, uint32_t num, char *tab);
516 void feature_code_str(uint64_t feature0, uint64_t feature1, char *to,
517                       uint32_t buflen);
518
519 int sendACKToHost(enum HostMessageT resp, enum ErrorCode err_code,
520                         char *payload, int payload_size);
521
522 int parse_int8(struct msg_buf_t *msg, uint8_t *val);
523 int parse_int32(struct msg_buf_t *msg, uint32_t *val);
524 int parse_int64(struct msg_buf_t *msg, uint64_t *val);
525 int parse_string(struct msg_buf_t *msg, char **str);
526 int parse_string_no_alloc(struct msg_buf_t *msg, char *str);
527 int parse_replay_event_seq(struct msg_buf_t *msg, struct replay_event_seq_t *res);
528
529 struct md5sum {
530         md5_byte_t data[16];
531 };
532
533 int md5sum_by_file(struct md5sum *md5sum, const char *filename);
534 char *md5sum_to_str(const struct md5sum *md5sum, char *to, size_t buff_len);
535
536 void init_prof_session(struct prof_session_t *prof_session);
537 #endif /* _DA_PROTOCOL_ */