summaryrefslogtreecommitdiff
path: root/parser/msg_parser.c
blob: 045da4a86a6aa0bad1c1c89e7626d0acc248199e (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
/**
 * parser/msg_parser.c
 *
 * @author Vyacheslav Cherkashin
 * @author Vitaliy Cherepanov <v.cherepanov@samsung.com>
 *
 * @sectionLICENSE
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * @section COPYRIGHT
 *
 * Copyright (C) Samsung Electronics, 2013
 *
 * @section DESCRIPTION
 *
 * Message parsing implementation.
 */


#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <us_manager/probes/probes.h>
#include "msg_parser.h"
#include "msg_buf.h"
#include "parser_defs.h"


static int str_to_u32(const char *str, u32 *val)
{
	u32 result;
	if (!str || !*str)
		return -EINVAL;

	for (result = 0 ; *str; ++str) {
		if (*str < '0' || *str > '9')
			return -EINVAL;

		result = result * 10 + (*str - '0');
	}

	*val = result;

	return 0;
}





/* ============================================================================
 * ==                               APP_INFO                                 ==
 * ============================================================================
 */

/**
 * @brief Creates and fills app_info_data struct.
 *
 * @param mb Pointer to the message buffer.
 * @return Pointer to the filled app_info_data struct on success;\n
 * NULL on error.
 */
struct app_info_data *create_app_info(struct msg_buf *mb)
{
	int ret;
	struct app_info_data *ai;
	u32 app_type;
	char *ta_id, *exec_path;

	print_parse_debug("app_info:\n");

	print_parse_debug("type:");
	ret = get_u32(mb, &app_type);
	if (ret) {
		print_err("failed to read target application type\n");
		return NULL;
	}

	print_parse_debug("id:");
	ret = get_string(mb, &ta_id);
	if (ret) {
		print_err("failed to read target application ID\n");
		return NULL;
	}

	print_parse_debug("exec path:");
	ret = get_string(mb, &exec_path);
	if (ret) {
		print_err("failed to read executable path\n");
		goto free_ta_id;
	}

	ai = kmalloc(sizeof(*ai), GFP_KERNEL);
	if (ai == NULL) {
		print_err("out of memory\n");
		goto free_exec_path;
	}

	switch (app_type) {
	case AT_TIZEN_NATIVE_APP:
	case AT_TIZEN_WEB_APP:
	case AT_COMMON_EXEC:
		ai->tgid = 0;
		break;
	case AT_PID: {
		u32 tgid = 0;

		if (*ta_id != '\0') {
			ret = str_to_u32(ta_id, &tgid);
			if (ret) {
				print_err("converting string to PID, "
					  "str='%s'\n", ta_id);
				goto free_ai;
			}
		}

		ai->tgid = tgid;
		break;
	}
	default:
		print_err("wrong application type(%u)\n", app_type);
		ret = -EINVAL;
		goto free_ai;
	}

	ai->app_type = (enum APP_TYPE)app_type;
	ai->app_id = ta_id;
	ai->exec_path = exec_path;

	return ai;

free_ai:
	kfree(ai);

free_exec_path:
	put_string(exec_path);

free_ta_id:
	put_string(ta_id);

	return NULL;
}

/**
 * @brief app_info_data cleanup.
 *
 * @param ai Pointer to the target app_info_data.
 * @return Void.
 */
void destroy_app_info(struct app_info_data *ai)
{
	put_string(ai->exec_path);
	put_string(ai->app_id);
	kfree(ai);
}





/* ============================================================================
 * ==                                CONFIG                                  ==
 * ============================================================================
 */

/**
 * @brief Creates and fills conf_data struct.
 *
 * @param mb Pointer to the message buffer.
 * @return Pointer to the filled conf_data struct on success;\n
 * 0 on error.
 */
struct conf_data *create_conf_data(struct msg_buf *mb)
{
	struct conf_data *conf;
	u64 use_features0, use_features1;
	u32 stp, dmp;

	print_parse_debug("conf_data:\n");

	print_parse_debug("features:");
	if (get_u64(mb, &use_features0)) {
		print_err("failed to read use_features\n");
		return NULL;
	}

	if (get_u64(mb, &use_features1)) {
		print_err("failed to read use_features\n");
		return NULL;
	}

	print_parse_debug("sys trace period:");
	if (get_u32(mb, &stp)) {
		print_err("failed to read sys trace period\n");
		return NULL;
	}

	print_parse_debug("data msg period:");
	if (get_u32(mb, &dmp)) {
		print_err("failed to read data message period\n");
		return NULL;
	}

	conf = kmalloc(sizeof(*conf), GFP_KERNEL);
	if (conf == NULL) {
		print_err("out of memory\n");
		return NULL;
	}

	conf->use_features0 = use_features0;
	conf->use_features1 = use_features1;
	conf->sys_trace_period = stp;
	conf->data_msg_period = dmp;

	return conf;
}

/**
 * @brief conf_data cleanup.
 *
 * @param conf Pointer to the target conf_data.
 * @return Void.
 */
void destroy_conf_data(struct conf_data *conf)
{
	kfree(conf);
}

static struct conf_data config;

/**
 * @brief Saves config to static config variable.
 *
 * @param conf Variable to save.
 * @return Void.
 */
void save_config(const struct conf_data *conf)
{
	memcpy(&config, conf, sizeof(config));
}

/**
 * @brief Restores config from static config variable.
 *
 * @param conf Variable to restore.
 * @return Void.
 */
void restore_config(struct conf_data *conf)
{
	memcpy(conf, &config, sizeof(*conf));
}



/* ============================================================================
 * ==                             PROBES PARSING                             ==
 * ============================================================================
 */

/**
 * @brief Gets retprobe data and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */
int get_retprobe(struct msg_buf *mb, struct probe_info *pi)
{
	char *args;
	char ret_type;

	print_parse_debug("funct args:");
	if (get_string(mb, &args)) {
		print_err("failed to read data function arguments\n");
		return -EINVAL;
	}

	print_parse_debug("funct ret type:");
	if (get_u8(mb, (u8 *)&ret_type)) {
		print_err("failed to read data function arguments\n");
		goto free_args;
	}

	pi->probe_type = SWAP_RETPROBE;
	pi->size = 0;
	pi->rp_i.args = args;
	pi->rp_i.ret_type = ret_type;

	return 0;

free_args:
	put_string(args);
	return -EINVAL;
}

/**
 * @brief Gets webprobe data and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */
int get_webprobe(struct msg_buf *mb, struct probe_info *pi)
{
	pi->probe_type = SWAP_WEBPROBE;
	pi->size = 0;

	return 0;
}

/**
 * @brief Retprobe data cleanup.
 *
 * @param pi Pointer to the probe_info comprising retprobe.
 * @return Void.
 */
void put_retprobe(struct probe_info *pi)
{
	put_string(pi->rp_i.args);
}

/**
 * @brief Gets preload data and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */
int get_preload_probe(struct msg_buf *mb, struct probe_info *pi)
{
	u64 handler;
	u8 flags;

	print_parse_debug("funct handler:");
	if (get_u64(mb, &handler)) {
		print_err("failed to read function handler\n");
		return -EINVAL;
	}

	print_parse_debug("collect events flag:");
	if (get_u8(mb, &flags)) {
		print_err("failed to read collect events type\n");
		return -EINVAL;
	}

	pi->probe_type = SWAP_PRELOAD_PROBE;
	pi->size = 0;
	pi->pl_i.handler = handler;
	pi->pl_i.flags = flags;

	return 0;
}

/**
 * @brief Preload probe data cleanup.
 *
 * @param pi Pointer to the probe_info comprising retprobe.
 * @return Void.
 */
void put_preload_probe(struct probe_info *pi)
{
}

/**
 * @brief Gets preload get_caller and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */

int get_get_caller_probe(struct msg_buf *mb, struct probe_info *pi)
{
	pi->probe_type = SWAP_GET_CALLER;
	pi->size = 0;

	return 0;
}

/**
 * @brief Preload get_caller probe data cleanup.
 *
 * @param pi Pointer to the probe_info comprising retprobe.
 * @return Void.
 */
void put_get_caller_probe(struct probe_info *pi)
{
}

/**
 * @brief Gets preload get_call_type and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */
int get_get_call_type_probe(struct msg_buf *mb, struct probe_info *pi)
{
	pi->probe_type = SWAP_GET_CALL_TYPE;
	pi->size = 0;

	return 0;
}

/**
 * @brief Preload get_call type probe data cleanup.
 *
 * @param pi Pointer to the probe_info comprising retprobe.
 * @return Void.
 */
void put_get_call_type_probe(struct probe_info *pi)
{
}

/**
 * @brief Gets preload write_msg and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */
int get_write_msg_probe(struct msg_buf *mb, struct probe_info *pi)
{
	pi->probe_type = SWAP_WRITE_MSG;
	pi->size = 0;

	return 0;
}

/**
 * @brief Preload write_msg type probe data cleanup.
 *
 * @param pi Pointer to the probe_info comprising retprobe.
 * @return Void.
 */
void put_write_msg_probe(struct probe_info *pi)
{
}




/**
 * @brief Gets FBI probe data and puts it to the probe_info struct.
 *
 * @param mb Pointer to the message buffer.
 * @param pi Pointer to the probe_info struct.
 * @return 0 on success, error code on error.
 */
int get_fbi_data(struct msg_buf *mb, struct fbi_var_data *vd)
{
	u64 var_id;
	u64 reg_offset;
	u8 reg_n;
	u32 data_size;
	u8 steps_count, i;
	struct fbi_step *steps = NULL;

	print_parse_debug("var ID:");
	if (get_u64(mb, &var_id)) {
		print_err("failed to read var ID\n");
		return -EINVAL;
	}

	print_parse_debug("register offset:");
	if (get_u64(mb, &reg_offset)) {
		print_err("failed to read register offset\n");
		return -EINVAL;
	}

	print_parse_debug("register number:");
	if (get_u8(mb, &reg_n)) {
		print_err("failed to read number of the register\n");
		return -EINVAL;
	}

	print_parse_debug("data size:");
	if (get_u32(mb, &data_size)) {
		print_err("failed to read data size\n");
		return -EINVAL;
	}

	print_parse_debug("steps count:");
	if (get_u8(mb, &steps_count)) {
		print_err("failed to read steps count\n");
		return -EINVAL;
	}

	if (steps_count > 0) {
		steps = kmalloc(steps_count * sizeof(*vd->steps),
				GFP_KERNEL);
		if (steps == NULL) {
			print_err("MALLOC FAIL\n");
			return -ENOMEM;
		}

		for (i = 0; i != steps_count; i++) {
			print_parse_debug("steps #%d ptr_order:", i);
			if (get_u8(mb, &(steps[i].ptr_order))) {
				print_err("failed to read pointer order(step #%d)\n",
					  i);
				goto free_steps;
			}
			print_parse_debug("steps #%d data_offset:", i);
			if (get_u64(mb, &(steps[i].data_offset))){
				print_err("failed to read offset (steps #%d)\n",
					  i);
				goto free_steps;
			}
		}
	}

	vd->reg_n = reg_n;
	vd->reg_offset = reg_offset;
	vd->data_size = data_size;
	vd->var_id = var_id;
	vd->steps_count = steps_count;
	vd->steps = steps;

	return 0;

free_steps:
	kfree(steps);
	return -EINVAL;
}

int get_fbi_probe(struct msg_buf *mb, struct probe_info *pi)
{
	uint8_t var_count, i;
	struct fbi_var_data *vars;

	print_parse_debug("var count:");
	if (get_u8(mb, &var_count)) {
		print_err("failed to read var ID\n");
		return -EINVAL;
	}

	vars = kmalloc(var_count * sizeof(*vars), GFP_KERNEL);
	if (vars == NULL) {
		print_err("alloc vars error\n");
		goto err;
	}

	for (i = 0; i != var_count; i++) {
		if (get_fbi_data(mb, &vars[i]) != 0)
			goto free_vars;
	}

	pi->probe_type = SWAP_FBIPROBE;
	pi->fbi_i.var_count = var_count;
	pi->fbi_i.vars = vars;
	pi->size =0 ;
	return 0;

free_vars:
	kfree(vars);

err:
	return -EINVAL;

}

/**
 * @brief FBI probe data cleanup.
 *
 * @param pi Pointer to the probe_info comprising FBI probe.
 * @return Void.
 */
void put_fbi_probe(struct probe_info *pi)
{
	return;
}


/* ============================================================================
 * ==                               FUNC_INST                                ==
 * ============================================================================
 */

/**
 * @brief Creates and fills func_inst_data struct.
 *
 * @param mb Pointer to the message buffer.
 * @return Pointer to the filled func_inst_data struct on success;\n
 * 0 on error.
 */
struct func_inst_data *create_func_inst_data(struct msg_buf *mb)
{
	struct func_inst_data *fi;
	u64 addr;
	u8 type;

	print_parse_debug("func addr:");
	if (get_u64(mb, &addr)) {
		print_err("failed to read data function address\n");
		return NULL;
	}

	print_parse_debug("probe type:");
	if (get_u8(mb, &type)) {
		print_err("failed to read data probe type\n");
		return NULL;
	}

	fi = kmalloc(sizeof(*fi), GFP_KERNEL);
	if (fi == NULL) {
		print_err("out of memory\n");
		return NULL;
	}

	fi->addr = addr;

	switch (type) {
	case SWAP_RETPROBE:
		if (get_retprobe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	case SWAP_WEBPROBE:
		if (get_webprobe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	case SWAP_PRELOAD_PROBE:
		if (get_preload_probe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	case SWAP_GET_CALLER:
		if (get_get_caller_probe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	case SWAP_GET_CALL_TYPE:
		if (get_get_call_type_probe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	case SWAP_FBIPROBE:
		if (get_fbi_probe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	case SWAP_WRITE_MSG:
		if (get_write_msg_probe(mb, &(fi->probe_i)) != 0)
			goto free_func_inst;
		break;
	default:
		printk(KERN_WARNING "SWAP PARSER: Wrong probe type %d!\n",
		       type);
		goto free_func_inst;
	}

	return fi;

free_func_inst:

	kfree(fi);
	return NULL;
}

/**
 * @brief func_inst_data cleanup.
 *
 * @param fi Pointer to the target func_inst_data.
 * @return Void.
 */
void destroy_func_inst_data(struct func_inst_data *fi)
{
	switch (fi->probe_i.probe_type) {
	case SWAP_RETPROBE:
		put_retprobe(&(fi->probe_i));
		break;
	case SWAP_WEBPROBE:
		break;
	case SWAP_PRELOAD_PROBE:
		put_preload_probe(&(fi->probe_i));
		break;
	case SWAP_GET_CALLER:
		put_get_caller_probe(&(fi->probe_i));
		break;
	case SWAP_GET_CALL_TYPE:
		put_get_call_type_probe(&(fi->probe_i));
		break;
	case SWAP_FBIPROBE:
		put_fbi_probe(&(fi->probe_i));
		break;
	case SWAP_WRITE_MSG:
		put_write_msg_probe(&(fi->probe_i));
		break;
	default:
		printk(KERN_WARNING "SWAP PARSER: Wrong probe type %d!\n",
		   fi->probe_i.probe_type);
	}

	kfree(fi);
}





/* ============================================================================
 * ==                               LIB_INST                                 ==
 * ============================================================================
 */

/**
 * @brief Creates and fills lib_inst_data struct.
 *
 * @param mb Pointer to the message buffer.
 * @return Pointer to the filled lib_inst_data struct on success;\n
 * 0 on error.
 */
struct lib_inst_data *create_lib_inst_data(struct msg_buf *mb)
{
	struct lib_inst_data *li;
	struct func_inst_data *fi;
	char *path;
	u32 cnt, j, i = 0;

	print_parse_debug("bin path:");
	if (get_string(mb, &path)) {
		print_err("failed to read path of binary\n");
		return NULL;
	}

	print_parse_debug("func count:");
	if (get_u32(mb, &cnt)) {
		print_err("failed to read count of functions\n");
		goto free_path;
	}

	if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt) {
		print_err("to match count of functions(%u)\n", cnt);
		goto free_path;
	}

	li = kmalloc(sizeof(*li), GFP_KERNEL);
	if (li == NULL) {
		print_err("out of memory\n");
		goto free_path;
	}

	if (cnt) {
		li->func = vmalloc(sizeof(*li->func) * cnt);
		if (li->func == NULL) {
			print_err("out of memory\n");
			goto free_li;
		}

		for (i = 0; i < cnt; ++i) {
			print_parse_debug("func #%d:\n", i + 1);
			fi = create_func_inst_data(mb);
			if (fi == NULL)
				goto free_func;

			li->func[i] = fi;
		}
	} else {
		li->func = NULL;
	}

	li->path = path;
	li->cnt_func = cnt;

	return li;

free_func:
	for (j = 0; j < i; ++j)
		destroy_func_inst_data(li->func[j]);
	vfree(li->func);

free_li:
	kfree(li);

free_path:
	put_string(path);

	return NULL;
}

/**
 * @brief lib_inst_data cleanup.
 *
 * @param li Pointer to the target lib_inst_data.
 * @return Void.
 */
void destroy_lib_inst_data(struct lib_inst_data *li)
{
	int i;

	put_string(li->path);

	for (i = 0; i < li->cnt_func; ++i)
		destroy_func_inst_data(li->func[i]);

	vfree(li->func);
	kfree(li);
}





/* ============================================================================
 * ==                               APP_INST                                 ==
 * ============================================================================
 */

/**
 * @brief Creates and fills app_inst_data struct.
 *
 * @param mb Pointer to the message buffer.
 * @return Pointer to the filled app_inst_data struct on success;\n
 * 0 on error.
 */
struct app_inst_data *create_app_inst_data(struct msg_buf *mb)
{
	struct app_inst_data *app_inst;
	struct app_info_data *app_info;
	struct func_inst_data *func;
	struct lib_inst_data *lib;
	u32 cnt_func, i_func = 0, cnt_lib, i_lib = 0, i;

	app_info = create_app_info(mb);
	if (app_info == NULL)
		return NULL;

	print_parse_debug("func count:");
	if (get_u32(mb, &cnt_func)) {
		print_err("failed to read count of functions\n");
		goto free_app_info;
	}

	if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt_func) {
		print_err("to match count of functions(%u)\n", cnt_func);
		goto free_app_info;
	}

	app_inst = kmalloc(sizeof(*app_inst), GFP_KERNEL);
	if (app_inst == NULL) {
		print_err("out of memory\n");
		goto free_app_info;
	}

	if (cnt_func) {
		app_inst->func = vmalloc(sizeof(*app_inst->func) * cnt_func);
		if (app_inst->func == NULL) {
			print_err("out of memory\n");
			goto free_app_inst;
		}

		for (i_func = 0; i_func < cnt_func; ++i_func) {
			print_parse_debug("func #%d:\n", i_func + 1);
			func = create_func_inst_data(mb);
			if (func == NULL)
				goto free_func;

			app_inst->func[i_func] = func;
		}
	} else {
		app_inst->func = NULL;
	}

	print_parse_debug("lib count:");
	if (get_u32(mb, &cnt_lib)) {
		print_err("failed to read count of libraries\n");
		goto free_func;
	}

	if (remained_mb(mb) / MIN_SIZE_LIB_INST < cnt_lib) {
		print_err("to match count of libraries(%u)\n", cnt_lib);
		goto free_func;
	}

	if (cnt_lib) {
		app_inst->lib = vmalloc(sizeof(*app_inst->lib) * cnt_lib);
		if (app_inst->lib == NULL) {
			print_err("out of memory\n");
			goto free_func;
		}

		for (i_lib = 0; i_lib < cnt_lib; ++i_lib) {
			print_parse_debug("lib #%d:\n", i_lib + 1);
			lib = create_lib_inst_data(mb);
			if (lib == NULL)
				goto free_lib;

			app_inst->lib[i_lib] = lib;
		}
	} else {
		app_inst->lib = NULL;
	}

	app_inst->app_info = app_info;
	app_inst->cnt_func = cnt_func;
	app_inst->cnt_lib = cnt_lib;

	return app_inst;

free_lib:
	for (i = 0; i < i_lib; ++i)
		destroy_lib_inst_data(app_inst->lib[i]);
	vfree(app_inst->lib);

free_func:
	for (i = 0; i < i_func; ++i)
		destroy_func_inst_data(app_inst->func[i]);
	vfree(app_inst->func);

free_app_inst:
	kfree(app_inst);

free_app_info:
	destroy_app_info(app_info);

	return NULL;
}

/**
 * @brief app_inst_data cleanup.
 *
 * @param ai Pointer to the target app_inst_data.
 * @return Void.
 */
void destroy_app_inst_data(struct app_inst_data *ai)
{
	int i;

	for (i = 0; i < ai->cnt_lib; ++i)
		destroy_lib_inst_data(ai->lib[i]);
	vfree(ai->lib);

	for (i = 0; i < ai->cnt_func; ++i)
		destroy_func_inst_data(ai->func[i]);
	vfree(ai->func);

	destroy_app_info(ai->app_info);
	kfree(ai);
}





/* ============================================================================
 * ==                                US_INST                                 ==
 * ============================================================================
 */

/**
 * @brief Creates and fills us_inst_data struct.
 *
 * @param mb Pointer to the message buffer.
 * @return Pointer to the filled us_inst_data struct on success;\n
 * 0 on error.
 */
struct us_inst_data *create_us_inst_data(struct msg_buf *mb)
{
	struct us_inst_data *ui;
	struct app_inst_data *ai;
	u32 cnt, j, i = 0;

	print_parse_debug("us_inst_data:\n");

	print_parse_debug("app count:");
	if (get_u32(mb, &cnt)) {
		print_err("failed to read count of applications\n");
		return NULL;
	}

	if (remained_mb(mb) / MIN_SIZE_APP_INST < cnt) {
		print_err("to match count of applications(%u)\n", cnt);
		return NULL;
	}

	ui = kmalloc(sizeof(struct us_inst_data), GFP_KERNEL);
	if (ui == NULL) {
		print_err("out of memory\n");
		return NULL;
	}

	ui->app_inst = kmalloc(sizeof(struct app_inst_data *) * cnt,
			       GFP_KERNEL);
	if (ui->app_inst == NULL) {
		print_err("out of memory\n");
		goto free_ui;
	}

	for (i = 0; i < cnt; ++i) {
		print_parse_debug("app #%d:\n", i + 1);
		ai = create_app_inst_data(mb);
		if (ai == NULL)
			goto free_app_inst;

		ui->app_inst[i] = ai;
	}

	ui->cnt = cnt;

	return ui;

free_app_inst:
	for (j = 0; j < i; ++j)
		destroy_app_inst_data(ui->app_inst[j]);
	kfree(ui->app_inst);

free_ui:
	kfree(ui);

	return NULL;
}

/**
 * @brief us_inst_data cleanup.
 *
 * @param ui Pointer to the target us_inst_data.
 * @return Void.
 */
void destroy_us_inst_data(struct us_inst_data *ui)
{
	int i;

	for (i = 0; i < ui->cnt; ++i)
		destroy_app_inst_data(ui->app_inst[i]);

	kfree(ui->app_inst);
	kfree(ui);
}