-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeil-build-viewer.c
3568 lines (3227 loc) · 116 KB
/
keil-build-viewer.c
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
/**
* \file keil-build-viewer.c
* \brief main application
*/
/*
* Copyright (c) 2023 Dino Haw
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is keil-build-viewer.
*
* Author: Dino Haw <[email protected]>
* Version: v1.6
* Change Logs:
* Version Date Author Notes
* v1.0 2023-11-10 Dino the first version
* v1.1 2023-11-11 Dino 1. 适配 RAM 和 ROM 的解析
* v1.2 2023-11-11 Dino 1. 适配 keil4 的 map 文件
* 2. 增加检测到开启 LTO 后打印提示信息
* 3. 修复开启 LTO 后无打印 region 的问题
* v1.3 2023-11-12 Dino 1. 修复工程存在多个 lib 时仅解析一个的问题
* v1.4 2023-11-21 Dino 1. 增加将本工具放置于系统环境变量 Path 所含目录的功能
* v1.5 2023-11-30 Dino 1. 新增更多的 progress bar 样式
* 2. 新增解析自定义的 memory area
* 3. 修复 RAM 和 ROM 信息缺失时显示异常的问题
* v1.5a 2023-11-30 Dino 1. 修复 object 数据溢出的问题
* 2. 修改进度条内存大小的显示策略,不再四舍五入
* v1.5b 2023-12-02 Dino 1. 【修复】保存文件路径内存动态分配过小的问题
* v1.6 2024-12-11 Dino 1. 【修复】有多个 region 时导致显示回车多行的问题
* 2. 【修复】Execution Region 被错误识别的问题
* 3. 【修复】当 Execution Region 在其他 Loard Region 中使用会重复显示的问题
* 4. 【修改】将未使用的 memory 放在同一分类显示
* 5. 【修改】若 Execution Region Size 为 UINT32_MAX 时,则修改为对应 memory 的 Size
*/
/* Includes ------------------------------------------------------------------*/
#include "keil-build-viewer.h"
/* Private variables ---------------------------------------------------------*/
static FILE * _log_file;
static bool _is_save_log = true;
static bool _is_has_unused_region;
static bool _is_display_object = true;
static bool _is_display_path = true;
static char _line_text[1024];
static char * _current_dir;
static ENCODING_TYPE _encoding_type = ENCODING_TYPE_GBK;
static PROGRESS_STYLE _progress_style = PROGRESS_STYLE_0;
static struct prj_path_list * _keil_prj_path_list;
static struct memory_info * _memory_info_head;
static struct file_path_list * _file_path_list_head;
static const char * _keil_prj_extension[] =
{
".uvprojx",
".uvproj"
};
static struct command_list _command_list[] =
{
{
.cmd = "-NOLOG",
.desc = "NOT save log file",
},
{
.cmd = "-OBJ",
.desc = "Display the ram and flash occupancy of each object file (default)",
},
{
.cmd = "-NOOBJ",
.desc = "NOT display the ram and flash occupancy of each object file",
},
{
.cmd = "-PATH",
.desc = "Display each object file path (default)",
},
{
.cmd = "-NOPATH",
.desc = "NOT display each object file path",
},
{
.cmd = "-STYLE0",
.desc = "Progress bar style: following system (default)",
},
{
.cmd = "-STYLE1",
.desc = "Progress bar style: |###OOO____| (when non-Chinese and not specified progress bar style)",
},
{
.cmd = "-STYLE2",
.desc = "Progress bar style: |XXXOOO____|",
},
};
/**
* @brief 主程序
* @note
* @param argc: 参数数量
* @param argv[]: 参数列表
* @retval 0: 正常 | -x: 错误
*/
int main(int argc, char *argv[])
{
clock_t run_time = clock();
struct load_region *load_region_head = NULL;
struct object_info *object_info_head = NULL;
struct load_region *record_load_region_head = NULL;
struct object_info *record_object_info_head = NULL;
/* 获取编码格式 */
UINT acp = GetACP();
if (acp == 936) {
_encoding_type = ENCODING_TYPE_GBK;
}
else if (acp == 950) {
_encoding_type = ENCODING_TYPE_BIG5;
}
else {
_encoding_type = ENCODING_TYPE_OTHER;
}
/* 1. 获取程序运行的工作目录 */
int result = 0;
DWORD buff_len = GetCurrentDirectory(0, NULL);
if (buff_len == 0)
{
printf("\n[ERROR] %s %s\n", APP_NAME, APP_VERSION);
printf("[ERROR] Get current directory length failed (code: %d)\n", GetLastError());
result = -20;
goto __exit;
}
_current_dir = (char *)malloc(buff_len + 1);
if (_current_dir == NULL)
{
printf("\n[ERROR] %s %s\n", APP_NAME, APP_VERSION);
printf("[ERROR] Failed to allocate current directory memory\n");
result = -21;
goto __exit;
}
buff_len = GetCurrentDirectory(buff_len, _current_dir);
if (buff_len == 0)
{
printf("\n[ERROR] %s %s\n", APP_NAME, APP_VERSION);
printf("[ERROR] Get current directory failed. (code: %d)\n", GetLastError());
result = -22;
goto __exit;
}
/* 创建 log 文件 */
char *file_path = NULL;
size_t file_path_size = 0;
if (buff_len < MAX_PATH) {
file_path_size = MAX_PATH * 2;
} else {
file_path_size = buff_len * 2;
}
file_path = (char *)malloc(file_path_size);
if (file_path == NULL)
{
printf("\n[ERROR] %s %s\n", APP_NAME, APP_VERSION);
printf("[ERROR] Failed to allocate file path memory\n");
result = -23;
goto __exit;
}
/* 2. 搜索同级目录或指定目录下的所有 keil 工程并打印 */
_keil_prj_path_list = prj_path_list_init(MAX_PATH_QTY);
search_files_by_extension(_current_dir,
buff_len,
_keil_prj_extension,
sizeof(_keil_prj_extension) / sizeof(char *),
_keil_prj_path_list);
snprintf(file_path, file_path_size, "%s\\%s.log", _current_dir, APP_NAME);
/* 3. 参数处理 */
char input_param[MAX_PATH] = {0};
char keil_prj_name[MAX_PRJ_NAME_SIZE] = {0};
if (argc > 1)
{
int err_param = 0;
int res = parameter_process(argc,
argv,
keil_prj_name,
sizeof(keil_prj_name),
input_param,
sizeof(input_param),
&err_param);
if (_is_save_log) {
_log_file = fopen(file_path, "w+");
}
if (res == -1)
{
log_print(_log_file, "\n[ERROR] INVALID INPUT (code: %d): %s\n", GetLastError(), argv[1]);
result = -1;
goto __exit;
}
else if (res == -2)
{
log_print(_log_file, "\n[ERROR] INVALID INPUT: %s\n", argv[1]);
log_print(_log_file, "[ERROR] Please enter the absolute path or keil project name with extension\n");
result = -2;
goto __exit;
}
else if (res == -3)
{
log_print(_log_file, "\n[ERROR] INVALID INPUT: %s\n", argv[err_param]);
log_print(_log_file, "[ERROR] Only the following commands are supported\n");
for (size_t i = 0; i < sizeof(_command_list) / sizeof(struct command_list); i++) {
log_print(_log_file, "\t%s\t %s\n", _command_list[i].cmd, _command_list[i].desc);
}
result = -3;
goto __exit;
}
else if (res == -4)
{
log_print(_log_file, "\nYou can control the displayed information by entering the following commands\n \n");
for (size_t i = 0; i < sizeof(_command_list) / sizeof(struct command_list); i++) {
log_print(_log_file, "\t%s\t %s\n", _command_list[i].cmd, _command_list[i].desc);
}
result = 0;
goto __exit;
}
}
else {
_log_file = fopen(file_path, "w+");
}
log_print(_log_file, "\n=================================================== %s %s ==================================================\n ", APP_NAME, APP_VERSION);
if (_keil_prj_path_list->size > 0) {
log_save(_log_file, "\n[Search keil project] %d item(s)\n", _keil_prj_path_list->size);
}
for (size_t i = 0; i < _keil_prj_path_list->size; i++) {
log_save(_log_file, "\t%s\n", _keil_prj_path_list->items[i]);
}
log_save(_log_file, "\n[User input] %s\n", input_param);
log_save(_log_file, "[Current folder] %s\n", _current_dir);
log_save(_log_file, "[Encoding] %d\n", acp);
/* 4. 确定 keil 工程 */
char *keil_prj_path;
if (input_param[0] != '\0')
{
log_print(_log_file, "\n[Hint] You specify the keil project!\n");
keil_prj_path = input_param;
}
else if (_keil_prj_path_list->size > 0)
{
keil_prj_path = _keil_prj_path_list->items[_keil_prj_path_list->size - 1];
char *last_slash = strrchr(keil_prj_path, '\\');
if (last_slash)
{
last_slash += 1;
strncpy_s(keil_prj_name, sizeof(keil_prj_name), last_slash, strnlen(last_slash, sizeof(keil_prj_name)));
}
}
else
{
log_print(_log_file, "\n[ERROR] NO keil project found\n");
log_print(_log_file, "[ERROR] Please check: %s\n", input_param);
result = -4;
goto __exit;
}
log_save(_log_file, "[Keil project path] %s\n", keil_prj_path);
log_save(_log_file, "[Keil project name] %s\n", keil_prj_name);
bool is_keil4_prj = false;
if (keil_prj_name[strlen(keil_prj_name) - 1] == 'j') {
is_keil4_prj = true;
}
log_save(_log_file, "[Is keil v4] %d\n", is_keil4_prj);
char keil_prj_full_name[MAX_PRJ_NAME_SIZE] = {0};
memcpy_s(keil_prj_full_name, sizeof(keil_prj_full_name), keil_prj_name, strnlen_s(keil_prj_name, sizeof(keil_prj_full_name)));
char *dot = strrchr(keil_prj_name, '.');
if (dot) {
*dot = '\0';
}
/* 5. 获取启用的 project target */
/* 打开同名的 .uvoptx 或 .uvopt 文件 */
char target_name[MAX_PRJ_NAME_SIZE] = {0};
snprintf(file_path, file_path_size, "%s\\%s.uvopt", _current_dir, keil_prj_name);
if (is_keil4_prj == false) {
strncat_s(file_path, file_path_size, "x", 1);
}
/* 不存在 uvoptx 文件时,默认选择第一个 target name */
bool is_has_target = true;
if (uvoptx_file_process(file_path, target_name, sizeof(target_name)) == false)
{
is_has_target = false;
log_print(_log_file, "\n[WARNING] can't open '%s'\n", file_path);
log_print(_log_file, "[WARNING] The first project target is selected by default.\n");
}
/* 6. 获取 map 和 htm 文件所在的目录及 device 和 output_name 信息 */
/* 打开同名的 .uvprojx 或 .uvproj 文件 */
snprintf(file_path, file_path_size, "%s\\%s.uvproj", _current_dir, keil_prj_name);
if (is_keil4_prj == false) {
strncat_s(file_path, file_path_size, "x", 1);
}
char target_name_label[MAX_PRJ_NAME_SIZE * 2] = {0};
if (is_has_target) {
snprintf(target_name_label, sizeof(target_name_label), "%s%s", LABEL_TARGET_NAME, target_name);
} else {
strncpy_s(target_name_label, sizeof(target_name_label), LABEL_TARGET_NAME, strlen(LABEL_TARGET_NAME));
}
struct uvprojx_info uvprojx_file = {0};
int res = uvprojx_file_process(file_path,
target_name_label,
&uvprojx_file,
!is_has_target);
if (res == -1)
{
log_print(_log_file, "\n[ERROR] can't open .uvproj(x) file\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -5;
goto __exit;
}
else if (res == -2)
{
log_print(_log_file, "\n[ERROR] <Cpu> contains unsupported types\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -6;
goto __exit;
}
else if (res == -3)
{
log_print(_log_file, "\n[ERROR] generate map file is not checked (Options for Target -> Listing -> Linker Listing)\n");
result = -7;
goto __exit;
}
log_save(_log_file, "\n[Device] %s\n", uvprojx_file.chip);
log_save(_log_file, "[Target name] %s\n", uvprojx_file.target_name);
log_save(_log_file, "[Output name] %s\n", uvprojx_file.output_name);
log_save(_log_file, "[Output path] %s\n", uvprojx_file.output_path);
log_save(_log_file, "[Listing path] %s\n", uvprojx_file.listing_path);
log_save(_log_file, "[Is has pack] %d\n", uvprojx_file.is_has_pack);
log_save(_log_file, "[Is enbale LTO] %d\n", uvprojx_file.is_enable_lto);
log_save(_log_file, "[Is has user library] %d\n", uvprojx_file.is_has_user_lib);
log_save(_log_file, "[Is custom scatter file] %d\n", uvprojx_file.is_custom_scatter);
if (uvprojx_file.output_name[0] == '\0')
{
log_print(_log_file, "\n[ERROR] output name is empty\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -8;
goto __exit;
}
if (uvprojx_file.listing_path[0] == '\0')
{
log_print(_log_file, "\n[ERROR] listing path is empty\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -9;
goto __exit;
}
char *p_target_name = target_name;
if (is_has_target == false) {
p_target_name = uvprojx_file.target_name;
}
log_print(_log_file, "\n[%s] [%s] [%s]", keil_prj_full_name, p_target_name, uvprojx_file.chip);
if (uvprojx_file.is_enable_lto) {
log_print(_log_file, " [LTO enable]\n \n");
} else {
log_print(_log_file, " [LTO disable]\n \n");
}
log_save(_log_file, "[memory info]\n");
for (struct memory_info *memory = _memory_info_head;
memory != NULL;
memory = memory->next)
{
log_save(_log_file, "[name] %s [base addr] 0x%08X [size] 0x%08X [type] %d [off-chip] %d [is pack] %d [ID] %d \n",
memory->name, memory->base_addr, memory->size, memory->type, memory->is_offchip, memory->is_from_pack, memory->id);
}
/* 7. 从 build_log 文件中获取被改名的文件信息 */
if (uvprojx_file.output_path[0] != '\0')
{
res = combine_path(file_path, file_path_size, keil_prj_path, uvprojx_file.output_path);
snprintf(file_path, file_path_size, "%s%s.build_log.htm", file_path, uvprojx_file.output_name);
if (res == 0)
{
build_log_file_process(file_path);
}
if (res == -1)
{
log_print(_log_file, "\n[WARNING] %s not a absolute path\n", keil_prj_path);
log_print(_log_file, "[WARNING] path: %s\n \n", file_path);
}
else if (res == -2)
{
log_print(_log_file, "\n[WARNING] relative paths go up more levels than absolute paths\n");
log_print(_log_file, "[WARNING] path: %s\n \n", file_path);
}
}
else {
log_print(_log_file, "\n[WARNING] %s is empty, can't read '.build_log.htm' file\n \n", LABEL_OUTPUT_DIRECTORY);
}
/* 8. 处理剩余的重名文件 */
file_rename_process();
/* 9. 打开 map 文件,获取 Load Region 和 Execution Region */
res = combine_path(file_path, file_path_size, keil_prj_path, uvprojx_file.listing_path);
if (res == -1)
{
log_print(_log_file, "\n[ERROR] %s not a absolute path\n \n", keil_prj_path);
result = -10;
goto __exit;
}
else if (res == -2)
{
log_print(_log_file, "\n[ERROR] relative paths go up more levels than absolute paths\n \n");
result = -11;
goto __exit;
}
snprintf(file_path, file_path_size, "%s%s.map", file_path, uvprojx_file.output_name);
log_save(_log_file, "[map file path] %s\n", file_path);
res = map_file_process(file_path,
&load_region_head,
&object_info_head,
uvprojx_file.is_has_user_lib,
true); /* !uvprojx_file.is_custom_scatter */
if (res == -1)
{
log_print(_log_file, "\n[ERROR] Check if a map file exists (Options for Target -> Listing -> Linker Listing)\n");
log_print(_log_file, "[ERROR] map file path: %s\n", file_path);
result = -12;
goto __exit;
}
else if (res == -2)
{
log_print(_log_file, "\n[ERROR] map file does not contain \"%s\"\n", STR_MEMORY_MAP_OF_THE_IMAGE);
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -13;
goto __exit;
}
else if (res == -3)
{
log_print(_log_file, "\n[ERROR] map file does not find object's information\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -14;
goto __exit;
}
log_save(_log_file, "\n[region info]\n");
for (struct load_region *l_region = load_region_head;
l_region != NULL;
l_region = l_region->next)
{
log_save(_log_file, "[load region] %s\n", l_region->name);
for (struct exec_region *e_region = l_region->exec_region;
e_region != NULL;
e_region = e_region->next)
{
log_save(_log_file, "\t[execution region] %s, 0x%08X, 0x%08X, 0x%08X [memory type] %d [memory ID] %d\n",
e_region->name, e_region->base_addr, e_region->size,
e_region->used_size, e_region->memory_type, e_region->memory_id);
for (struct region_block *block = e_region->zi_block;
block != NULL;
block = block->next)
{
log_save(_log_file, "\t\t[ZI block] addr: 0x%08X, size: 0x%08X (%d)\n",
block->start_addr, block->size, block->size);
}
log_save(_log_file, "\n");
}
}
/* 10. 打印用户 object 和用户 library 文件的 flash 和 RAM 占用情况 */
/* 10.1 将路径绑定到 object info 对应的 path 成员 */
size_t max_name_len = 0;
size_t max_path_len = 0;
for (struct file_path_list *path_temp = _file_path_list_head;
path_temp != NULL;
path_temp = path_temp->next)
{
for (struct object_info *object_temp = object_info_head;
object_temp != NULL;
object_temp = object_temp->next)
{
if (path_temp->file_type == OBJECT_FILE_TYPE_LIBRARY)
{
if (strcasecmp(object_temp->name, path_temp->old_name) == 0) {
object_temp->path = path_temp->path;
}
}
else
{
if (strcasecmp(object_temp->name, path_temp->new_object_name) == 0) {
object_temp->path = path_temp->path;
}
}
}
/* 计算出各个文件名称和相对路径的最长长度 */
size_t path_len = strnlen_s(path_temp->path, MAX_PATH);
size_t name_len1 = strnlen_s(path_temp->old_name, MAX_PATH);
size_t name_len2 = strnlen_s(path_temp->new_object_name, MAX_PATH);
if (name_len1 > max_name_len) {
max_name_len = name_len1;
}
if (name_len2 > max_name_len) {
max_name_len = name_len2;
}
if (path_len > max_path_len) {
max_path_len = path_len;
}
}
log_save(_log_file, "\n[object name max length] %d\n", max_name_len);
log_save(_log_file, "[object path max length] %d\n", max_path_len);
/* 打印抓取的 object 名称和路径 */
log_save(_log_file, "\n[object in map file]\n");
for (struct object_info *object_temp = object_info_head;
object_temp != NULL;
object_temp = object_temp->next)
{
log_save(_log_file, "[object name] %s%*s [path] %s\n",
object_temp->name, max_name_len + 1 - strlen(object_temp->name), " ", object_temp->path);
}
/* 打印抓取的 keil 工程中的文件名和路径 */
log_save(_log_file, "\n[file path in keil project]\n");
for (struct file_path_list *path_list = _file_path_list_head;
path_list != NULL;
path_list = path_list->next)
{
log_save(_log_file, "[old name] %s%*s [type] %d [path] %s\n",
path_list->old_name, max_name_len + 1 - strlen(path_list->old_name), " ",
path_list->file_type, path_list->path);
if (strcmp(path_list->object_name, path_list->new_object_name)) {
log_save(_log_file, "[new name] %s\n", path_list->new_object_name);
}
}
/* 10.2 打开记录文件,打开失败则新建 */
snprintf(file_path, file_path_size, "%s\\%s-record.txt", _current_dir, APP_NAME);
bool is_has_record = true;
FILE *p_file = fopen(file_path, "r");
if (p_file == NULL)
{
p_file = fopen(file_path, "w+");
if (p_file == NULL)
{
log_print(_log_file, "\n[ERROR] can't create log file\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -15;
goto __exit;
}
is_has_record = false;
}
fclose(p_file);
/* 10.3 若存在记录文件,则读取各个文件 flash 和 RAM 占用情况 */
bool is_has_object = false;
bool is_has_region = false;
if (is_has_record)
{
record_file_process(file_path,
&record_load_region_head,
&record_object_info_head,
&is_has_object,
&is_has_region,
true); /* !uvprojx_file.is_custom_scatter */
}
if (is_has_record)
{
/* 将旧的 object 信息绑定到匹配的新的 object 信息上 */
for (struct object_info *new_obj_info = object_info_head;
new_obj_info != NULL;
new_obj_info = new_obj_info->next)
{
for (struct object_info *old_obj_info = record_object_info_head;
old_obj_info != NULL;
old_obj_info = old_obj_info->next)
{
if (strcasecmp(new_obj_info->name, old_obj_info->name) == 0) {
new_obj_info->old_object = old_obj_info;
}
}
}
log_save(_log_file, "\n[record region info]\n");
/* 将旧的 execution region 绑定到匹配的新的 execution region 上 */
for (struct load_region *old_load_region = record_load_region_head;
old_load_region != NULL;
old_load_region = old_load_region->next)
{
log_save(_log_file, "[load region] %s\n", old_load_region->name);
for (struct exec_region *old_exec_region = old_load_region->exec_region;
old_exec_region != NULL;
old_exec_region = old_exec_region->next)
{
for (struct load_region *new_load_region = load_region_head;
new_load_region != NULL;
new_load_region = new_load_region->next)
{
for (struct exec_region *new_exec_region = new_load_region->exec_region;
new_exec_region != NULL;
new_exec_region = new_exec_region->next)
{
if (strcmp(new_exec_region->name, old_exec_region->name) == 0) {
new_exec_region->old_exec_region = old_exec_region;
}
}
}
log_save(_log_file, "\t[execution region] %s, 0x%08X, 0x%08X, 0x%08X [type] %d [ID] %d\n",
old_exec_region->name, old_exec_region->base_addr, old_exec_region->size,
old_exec_region->used_size, old_exec_region->memory_type, old_exec_region->memory_id);
}
}
}
/* 10.4 打印并保存本次编译信息至记录文件 */
if (uvprojx_file.is_enable_lto == false)
{
if (_is_display_object)
{
size_t len = 0;
if (_is_display_path)
{
if (max_name_len > max_path_len) {
len = max_name_len;
} else {
len = max_path_len;
}
}
else {
len = max_name_len;
}
object_print_process(object_info_head, len, is_has_object);
}
/* 保存本次编译信息至记录文件 */
p_file = fopen(file_path, "w+");
if (p_file == NULL)
{
log_print(_log_file, "\n[ERROR] can't create record file\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -16;
goto __exit;
}
fputs(" Code (inc. data) RO Data RW Data ZI Data Debug Object Name\n", p_file);
for (struct object_info *object_temp = object_info_head;
object_temp != NULL;
object_temp = object_temp->next)
{
snprintf(_line_text, sizeof(_line_text),
"%10d %10d %10d %10d %10d %10d %s\n",
object_temp->code, 0, object_temp->ro_data, object_temp->rw_data, object_temp->zi_data, 0, object_temp->name);
fputs(_line_text, p_file);
}
fputs(STR_OBJECT_TOTALS "\n\n", p_file);
fclose(p_file);
}
// else {
// log_print(_log_file, "[WARNING] Because LTO is enabled, information for each file cannot be displayed\n \n");
// }
/* 11. 打印总 flash 和 RAM 占用情况,以进度条显示 */
/* 11.1 算出 execution region name 的最大长度 */
size_t max_region_name = 0;
for (struct load_region *l_region = load_region_head;
l_region != NULL;
l_region = l_region->next)
{
for (struct exec_region *e_region = l_region->exec_region;
e_region != NULL;
e_region = e_region->next)
{
size_t len = strnlen_s(e_region->name, 32);
if (len > max_region_name) {
max_region_name = len;
}
}
}
/* 11.2 判断当前的内存打印模式 */
MEMORY_PRINT_MODE print_mode = MEMORY_PRINT_MODE_0;
if (uvprojx_file.is_has_pack == false)
{
#if defined(ENABLE_REFER_TO_KEIL_DIALOG) && (ENABLE_REFER_TO_KEIL_DIALOG != 0)
if (_memory_info_head == NULL) {
print_mode = MEMORY_PRINT_MODE_2;
} else {
print_mode = MEMORY_PRINT_MODE_1;
}
#else
if (_memory_info_head && uvprojx_file.is_custom_scatter == false) {
print_mode = MEMORY_PRINT_MODE_1;
} else {
print_mode = MEMORY_PRINT_MODE_2;
}
#endif
}
log_save(_log_file, "[memory print mode]: %d\n", print_mode);
/* 11.3 开始打印 */
memory_numbering(MEMORY_TYPE_RAM);
memory_numbering(MEMORY_TYPE_FLASH);
if (_is_has_unused_region
&& print_mode == MEMORY_PRINT_MODE_0)
{
log_print(_log_file, UNUSED_LOAD_REGION_NAME "\n");
memory_print_unused(MEMORY_TYPE_RAM, max_region_name);
memory_print_unused(MEMORY_TYPE_FLASH, max_region_name);
log_print(_log_file, " \n");
}
bool is_print_null = true;
for (struct load_region *l_region = load_region_head;
l_region != NULL;
l_region = l_region->next)
{
log_print(_log_file, "%s\n", l_region->name);
if (print_mode == MEMORY_PRINT_MODE_1)
{
memory_mode1_print(l_region->exec_region, MEMORY_TYPE_RAM, false, max_region_name, is_has_record);
memory_mode1_print(l_region->exec_region, MEMORY_TYPE_RAM, true, max_region_name, is_has_record);
memory_mode1_print(l_region->exec_region, MEMORY_TYPE_FLASH, false, max_region_name, is_has_record);
memory_mode1_print(l_region->exec_region, MEMORY_TYPE_FLASH, true, max_region_name, is_has_record);
memory_mode1_print(l_region->exec_region, MEMORY_TYPE_UNKNOWN, false, max_region_name, is_has_record);
}
else if (print_mode == MEMORY_PRINT_MODE_2)
{
memory_mode2_print(l_region->exec_region, max_region_name, is_has_record);
}
else
{
memory_mode0_print(l_region->exec_region, l_region->name, MEMORY_TYPE_RAM, max_region_name, is_has_record, is_print_null);
memory_mode0_print(l_region->exec_region, l_region->name, MEMORY_TYPE_FLASH, max_region_name, is_has_record, is_print_null);
memory_mode0_print(l_region->exec_region, l_region->name, MEMORY_TYPE_UNKNOWN, max_region_name, is_has_record, is_print_null);
}
is_print_null = false;
}
/* 12. 打印栈使用情况 */
if (uvprojx_file.output_path[0] != '\0')
{
res = combine_path(file_path, file_path_size, keil_prj_path, uvprojx_file.output_path);
if (res == -1)
{
log_print(_log_file, "\n[ERROR] %s not a absolute path\n \n", keil_prj_path);
result = -17;
goto __exit;
}
else if (res == -2)
{
log_print(_log_file, "\n[ERROR] relative paths go up more levels than absolute paths\n \n");
result = -18;
goto __exit;
}
snprintf(file_path, file_path_size, "%s%s.htm", file_path, uvprojx_file.output_name);
log_save(_log_file, "[htm file path] %s\n", file_path);
stack_print_process(file_path);
}
/* 13. 保存本次 region 信息至记录文件 */
snprintf(file_path, file_path_size, "%s\\%s-record.txt", _current_dir, APP_NAME);
if (uvprojx_file.is_enable_lto) {
p_file = fopen(file_path, "w+");
} else {
p_file = fopen(file_path, "a");
}
if (p_file == NULL)
{
log_print(_log_file, "\n[ERROR] can't create record file\n");
log_print(_log_file, "[ERROR] Please check: %s\n", file_path);
result = -19;
goto __exit;
}
fputs(STR_MEMORY_MAP_OF_THE_IMAGE "\n\n", p_file);
for (struct load_region *l_region = load_region_head;
l_region != NULL;
l_region = l_region->next)
{
snprintf(_line_text, sizeof(_line_text),
"\t%s %s \n\n",
STR_LOAD_REGION, l_region->name);
fputs(_line_text, p_file);
for (struct exec_region *e_region = l_region->exec_region;
e_region != NULL;
e_region = e_region->next)
{
snprintf(_line_text, sizeof(_line_text),
"\t\t%s %s (%s0x%08X, %s0x%08X, %s0x%08X, END)\n\n",
STR_EXECUTION_REGION, e_region->name, STR_EXECUTE_BASE_ADDR, e_region->base_addr,
STR_REGION_USED_SIZE, e_region->used_size, STR_REGION_MAX_SIZE, e_region->size);
fputs(_line_text, p_file);
}
}
fputs(STR_IMAGE_COMPONENT_SIZE, p_file);
fclose(p_file);
__exit:
if (_current_dir) {
free(_current_dir);
}
if (file_path) {
free(file_path);
}
object_info_free(&object_info_head);
object_info_free(&record_object_info_head);
load_region_free(&load_region_head);
load_region_free(&record_load_region_head);
file_path_free(&_file_path_list_head);
memory_info_free(&_memory_info_head);
prj_path_list_free(_keil_prj_path_list);
log_print(_log_file, "=============================================================================================================================\n\n");
log_save(_log_file, "run time: %.3f s\n", (double)(clock() - run_time) / CLOCKS_PER_SEC);
fclose(_log_file);
return result;
}
/**
* @brief 入口参数处理
* @note
* @param param_qty: 参数数量
* @param param[]: 参数列表
* @param prj_name: [out] keil 工程名
* @param name_size: prj_name 的最大 size
* @param prj_path: [out] keil 工程绝对路径
* @param path_size: prj_path 的最大 size
* @param err_param: [out] 发生错误的参数位
* @retval 0: 正常 | -x: 错误
*/
int parameter_process(int param_qty,
char *param[],
char *prj_name,
size_t name_size,
char *prj_path,
size_t path_size,
int *err_param)
{
for (size_t i = 1; i < param_qty; i++)
{
log_save(_log_file, "[param %d] %s\n", i, param[i]);
if (param[i][0] == '-')
{
int seq = 0;
if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_is_save_log = false;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_is_display_object = true;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_is_display_object = false;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_is_display_path = true;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_is_display_path = false;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_progress_style = PROGRESS_STYLE_0;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_progress_style = PROGRESS_STYLE_1;
}
else if (strcasecmp(param[i], _command_list[seq++].cmd) == 0) {
_progress_style = PROGRESS_STYLE_2;
}
else if (strcasecmp(param[i], "-H") == 0
|| strcasecmp(param[i], "-HELP") == 0) {
return -4;
}
else
{
*err_param = i;
return -3;
}
}
else
{
char *last_slash = NULL;
size_t param_len = strnlen_s(param[i], MAX_PATH);
/* 绝对路径 */
if (param[i][1] == ':')
{
DWORD attributes = GetFileAttributes(param[i]);
if (attributes == INVALID_FILE_ATTRIBUTES) {
return -1;
}
/* 目录 */
if (attributes & FILE_ATTRIBUTE_DIRECTORY)
{
strncpy_s(_current_dir, sizeof(_current_dir), param[i], param_len);
if (param[i][param_len - 1] == '\\') {
_current_dir[param_len - 1] = '\0';
}
}
/* 文件 */
else
{
/* 不是 keil 工程则报错退出 */
if (is_keil_project(param[i]) == false) {
return -2;
}
strncpy_s(prj_path, path_size, param[i], param_len);
last_slash = strrchr(prj_path, '\\');
if (last_slash)
{
last_slash += 1;
strncpy_s(prj_name, name_size, last_slash, strnlen(last_slash, name_size));
}
}
}
/* 不支持相对路径 */
else if (param[i][0] == '\\' || param[i][0] == '.') {
return -2;
}
/* 文件名 */
else
{
snprintf(prj_path, path_size, "%s\\%s", _current_dir, param[i]);
/* 非 keil 工程则检查是否有扩展名 */
if (is_keil_project(param[i]) == false)
{
/* 有扩展名报错退出,无扩展名则从搜索到的列表里进行匹配 */
char *dot = strrchr(param[i], '.');
if (dot) {
return -2;
}
for (size_t index = 0; index < _keil_prj_path_list->size; index++)
{
if (strstr(_keil_prj_path_list->items[index], param[i]))
{
strncpy_s(prj_path, path_size, _keil_prj_path_list->items[index], strnlen_s(_keil_prj_path_list->items[index], MAX_PATH));
break;
}
}
}
last_slash = strrchr(prj_path, '\\');