root/modules/exmem_inspector.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _run
  2. _module_can_unload
  3. _module_exit_alt
  4. dummy_function1
  5. sampling_proc
  6. write_log
  7. gui_exmem_kbd_process
  8. basic_module_init
  9. gui_exmem_menu_kbd_process
  10. gui_exmem_draw

   1 #include "camera_info.h"
   2 #include "gui.h"
   3 #include "gui_draw.h"
   4 #include "meminfo.h"
   5 #include "module_load.h"
   6 #include "simple_module.h"
   7 #include "clock.h"
   8 #include "cachebit.h"
   9 
  10 // =========  MODULE INIT =================
  11 
  12 static int running = 0;
  13 static int sampling = 0;
  14 static int shandle = 0; // HP timer handle
  15 
  16 extern int basic_module_init();
  17 
  18 /***************** BEGIN OF AUXILARY PART *********************
  19   ATTENTION: DO NOT REMOVE OR CHANGE SIGNATURES IN THIS SECTION
  20  **************************************************************/
  21 
  22 int _run()
  23 {
  24     basic_module_init();
  25 
  26     return 0;
  27 }
  28 
  29 int _module_can_unload()
  30 {
  31     return (running==0 && shandle==0 && sampling==0);
  32 }
  33 
  34 int _module_exit_alt()
  35 {
  36     //running = 0;
  37     return 0;
  38 }
  39 
  40 /******************** Module Information structure ******************/
  41 
  42 libsimple_sym _librun =
  43 {
  44     {
  45          0, 0, _module_can_unload, _module_exit_alt, _run
  46     }
  47 };
  48 
  49 ModuleInfo _module_info =
  50 {
  51     MODULEINFO_V1_MAGICNUM,
  52     sizeof(ModuleInfo),
  53     {1,0},      // Module version
  54 
  55     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,           // Requirements of CHDK version
  56     ANY_PLATFORM_ALLOWED,       // Specify platform dependency
  57 
  58     (int32_t)"EXMEM Inspector",
  59     MTYPE_TOOL,                 // Show list of EXMEM allocations
  60 
  61     &_librun.base,
  62 
  63     ANY_VERSION,                // CONF version
  64     CAM_SCREEN_VERSION,         // CAM SCREEN version
  65     ANY_VERSION,                // CAM SENSOR version
  66     ANY_VERSION,                // CAM INFO version
  67 };
  68 
  69 /*************** END OF AUXILARY PART *******************/
  70 //-------------------------------------------------------------------
  71 
  72 #ifndef THUMB_FW
  73 int sampling_proc_ARM(int, int);
  74 
  75 void __attribute__((naked,noinline)) dummy_function1(void) {
  76     asm volatile (
  77     ".code 32\n"
  78 "sampling_proc_ARM:\n"
  79     "adr    r3, sapr_thumb+1\n"
  80     "bx     r3\n"
  81     ".code 16\n"
  82 "sapr_thumb:\n"
  83     "push   {lr}\n"
  84     "bl     sampling_proc\n"
  85     "pop    {pc}\n"
  86     );
  87 }
  88 #endif
  89 
  90 typedef struct
  91 {
  92     int            tim;
  93     unsigned int   typ;
  94     char          *adr;
  95     unsigned int   len;
  96 } exmemlog_s;
  97 
  98 #define LOG_LEN 128
  99 static exmemlog_s exl[LOG_LEN];
 100 static int exlpnt = 0;
 101 static unsigned int extypcnt = 0;
 102 static exmem_alloc_info exm_prev[42]; // should be enough
 103 static exmem_alloc_info eaitmp;
 104 static int speriod = 2000; // 2000usec
 105 
 106 int sampling_proc(int ttime, int param) {
 107     // check if sampling is still enabled, return if not
 108     if (!sampling) {
 109         shandle = 0;
 110         return 0;
 111     }
 112     // set timer immediately
 113 #ifndef THUMB_FW
 114     shandle = SetHPTimerAfterNow(speriod, sampling_proc_ARM, sampling_proc_ARM, 0);
 115 #else
 116     shandle = SetHPTimerAfterNow(speriod, sampling_proc, sampling_proc, 0);
 117 #endif
 118     // now look for changes
 119     int gtc = get_tick_count();
 120     unsigned int n;
 121     for (n=0; n<extypcnt; n++) {
 122         get_exmem_type_status(n, &eaitmp);
 123         if (exm_prev[n].addr!=eaitmp.addr || exm_prev[n].len!=eaitmp.len) {
 124             if (exlpnt<LOG_LEN) {
 125                 exl[exlpnt].tim = gtc;
 126                 exl[exlpnt].typ = n;
 127                 exl[exlpnt].adr = eaitmp.addr;
 128                 exl[exlpnt].len = eaitmp.len;
 129                 exlpnt++;
 130             }
 131         }
 132         exm_prev[n].addr=eaitmp.addr;
 133         exm_prev[n].len=eaitmp.len;
 134     }
 135     return 0;
 136 }
 137 
 138 //-------------------------------------------------------------------
 139 void write_log()
 140 {
 141     FILE *f = NULL;
 142     char txt[80], name[18];
 143     int n = 0;
 144     do {
 145         sprintf(txt, "A/CHDK/LOGS/EXM_%04d.LOG", n++);
 146         if (stat(txt,0) != 0) {
 147             f=fopen(txt, "wb");
 148             break;
 149         }
 150     } while(n<9999);
 151 
 152     if (!f)
 153         return;
 154 
 155     struct tm *tms;
 156     time_t t;
 157     t = time(NULL);
 158     tms = localtime(&t);
 159     sprintf(txt,"%04d-%02d-%02d %02d:%02d:%02d\n",tms->tm_year+1900,tms->tm_mon+1,tms->tm_mday,
 160                                                   tms->tm_hour,tms->tm_min,tms->tm_sec);
 161     fwrite(txt,1,strlen(txt),f);
 162     sprintf(txt,"%s, %s\n\n",camera_info.platform,camera_info.platformsub);
 163     fwrite(txt,1,strlen(txt),f);
 164     sprintf(txt,"TIME,TYPE,ADDRESS,SIZE\n");
 165     fwrite(txt,1,strlen(txt),f);
 166     for (n=0; n<LOG_LEN; n++)
 167     {
 168         char *s = get_exmem_type_name(exl[n].typ);
 169         if (!s)
 170         {
 171             strcpy(name, "-");
 172         }
 173         else
 174         {
 175             s += 6; // chop off 'EXMEM_'
 176             strncpy(name, s, 17); name[17] = 0;
 177         }
 178         if (exl[n].tim)
 179         {
 180             sprintf(txt,"%d,%s,%08x,%08x\n", exl[n].tim, name, (unsigned int)exl[n].adr, exl[n].len);
 181             fwrite(txt,1,strlen(txt),f);
 182         }
 183     }
 184     fclose(f);
 185 }
 186 
 187 /*************** GUI MODULE *******************/
 188 
 189 #include "gui_mbox.h"
 190 #include "keyboard.h"
 191 #include "stdlib.h"
 192 
 193 void gui_exmem_menu_kbd_process();
 194 int gui_exmem_kbd_process();
 195 void gui_exmem_draw();
 196 
 197 gui_handler GUI_MODE_EXMEM_INSPECTOR = 
 198 /*GUI_MODE_EXMEM_INSPECTOR*/   { GUI_MODE_MODULE, gui_exmem_draw, gui_exmem_kbd_process, gui_exmem_menu_kbd_process, 0, 0 };
 199 
 200 static unsigned int log_disp_start = 0;
 201 static int exmeminspect_mode = 0;
 202 static int exmeminspect_column = 0;
 203 static int disph = 0;
 204 int exmeminspect_redraw;
 205 gui_handler *exmeminspect_old_guimode;
 206 
 207 int gui_exmem_kbd_process()
 208 {
 209     switch (kbd_get_autoclicked_key())
 210     {
 211     case KEY_SET:
 212         sampling = !sampling;
 213         if (sampling) {
 214             // memset, reset, etc
 215             memset(exm_prev, 0, sizeof(exmem_alloc_info)*42);
 216             memset(exl, 0, sizeof(exmemlog_s)*LOG_LEN);
 217             exlpnt = 0;
 218         }
 219         sampling_proc(0,0); // TODO: could execute parallel to timer
 220         exmeminspect_redraw = 2;
 221         break;
 222     case KEY_UP:
 223         if (log_disp_start > 0) log_disp_start--;
 224         exmeminspect_redraw = 1;
 225         break;
 226     case KEY_DOWN:
 227         if (log_disp_start < LOG_LEN-1) log_disp_start++;
 228         exmeminspect_redraw = 1;
 229         break;
 230     case KEY_SHOOT_HALF:
 231         exmeminspect_mode = exmeminspect_mode==0?1:0;
 232         exmeminspect_redraw = 2;
 233         break;
 234     case KEY_LEFT:
 235         if (exlpnt>0)
 236             write_log();
 237         exmeminspect_redraw = 2;
 238         break;
 239     case KEY_RIGHT:
 240         exmeminspect_column = exmeminspect_column==0?1:0;
 241         exmeminspect_redraw = 2;
 242         break;
 243     }
 244     return 0;
 245 }
 246 
 247 //-------------------------------------------------------------------
 248 
 249 int basic_module_init()
 250 {
 251     if (!running)
 252     {
 253         running = 1;
 254         extypcnt = exmem_type_count;
 255         sampling = 0;
 256         shandle = 0;
 257         disph = camera_screen.height / FONT_HEIGHT;
 258     }
 259     exmeminspect_redraw = 2;
 260     exmeminspect_old_guimode = gui_set_mode(&GUI_MODE_EXMEM_INSPECTOR);
 261     return 1;
 262 }
 263 
 264 void gui_exmem_menu_kbd_process()
 265 {
 266     running = 0;
 267     sampling = 0;
 268     if (shandle) {
 269         CancelHPTimer(shandle);
 270         shandle = 0;
 271     }
 272 
 273     gui_set_mode(exmeminspect_old_guimode);
 274 }
 275 
 276 void gui_exmem_draw()
 277 {
 278     unsigned int idx, idxmax, showidx;
 279 
 280     if (exmeminspect_redraw) {
 281 
 282         if (exmeminspect_redraw == 2)
 283         {
 284             draw_rectangle(camera_screen.disp_left, 0, camera_screen.disp_right, camera_screen.height-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK), RECT_BORDER0|DRAW_FILLED);
 285             draw_string(camera_screen.disp_left, 0, "EXMEM Inspector", MAKE_COLOR(COLOR_WHITE, COLOR_BLACK));
 286             draw_string(camera_screen.disp_left, FONT_HEIGHT, sampling?"SET-stop  MENU-exit RIGHT-viewmode LEFT-save":"SET-start MENU-exit RIGHT-viewmode LEFT-save", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 287             if (exmeminspect_mode == 0) {
 288                 draw_string(camera_screen.disp_left+17*FONT_WIDTH, 0, "LIST,  HALFSHOOT->LOG", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 289                 if (exmeminspect_column == 0) {
 290                     draw_string(camera_screen.disp_left, 2*FONT_HEIGHT, "Idx Name              Addr      Size    ", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 291                 }
 292                 else {
 293                     draw_string(camera_screen.disp_left, 2*FONT_HEIGHT, "Idx Name              Addr      Top     ", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 294                 }
 295             }
 296             else {
 297                 draw_string(camera_screen.disp_left+17*FONT_WIDTH, 0, "LOG,  HALFSHOOT->LIST", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 298                 if (exmeminspect_column == 0) {
 299                     draw_string(camera_screen.disp_left, 2*FONT_HEIGHT, "Time    Name              Addr     Size    ", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 300                 }
 301                 else {
 302                     draw_string(camera_screen.disp_left, 2*FONT_HEIGHT, "Time    Name              Addr     Top     ", MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 303                 }
 304             }
 305         }
 306 
 307         showidx=3*FONT_HEIGHT;
 308         if (exmeminspect_mode == 0)
 309         {
 310             idxmax = extypcnt;
 311             for ( idx=0; idx<idxmax; idx++)
 312             {
 313                 char *s = get_exmem_type_name(idx);
 314                 if (!s)
 315                     continue;
 316                 exmem_alloc_info eai;
 317                 if (!get_exmem_type_status(idx, &eai) || !eai.addr)
 318                     continue;
 319 
 320                 char txt[50], name[18];
 321                 s += 6; // chop off 'EXMEM_'
 322                 strncpy(name, s, 17); name[17] = 0;
 323                 if (exmeminspect_column == 0) {
 324                     sprintf(txt,"%02d: %-17s %08x  %8x", idx, name, (unsigned int)eai.addr, eai.len);
 325                 }
 326                 else {
 327                     sprintf(txt,"%02d: %-17s %08x  %08x", idx, name, (unsigned int)eai.addr, (unsigned int)eai.addr+eai.len);
 328                 }
 329                 draw_string(camera_screen.disp_left, showidx, txt, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 330                 showidx += FONT_HEIGHT;
 331             }
 332         }
 333         else
 334         {
 335             idx = log_disp_start;
 336             while (showidx < camera_screen.height)
 337             {
 338                 if (idx >= LOG_LEN || exl[idx].tim == 0)
 339                 {
 340                     draw_string(camera_screen.disp_left, showidx, 
 341                     "                                             ",
 342                     MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 343                 }
 344                 else
 345                 {
 346                     char *s = get_exmem_type_name(exl[idx].typ);
 347                     if (!s)
 348                         s = "      -";
 349 
 350                     char txt[50], name[18];
 351                     s += 6; // chop off 'EXMEM_'
 352                     strncpy(name, s, 17); name[17] = 0;
 353                     if (exmeminspect_column == 0) {
 354                         sprintf(txt,"%06d: %-17s %08x %8x", exl[idx].tim, name, (unsigned int)exl[idx].adr, exl[idx].len);
 355                     }
 356                     else {
 357                         sprintf(txt,"%06d: %-17s %08x %08x", exl[idx].tim, name, (unsigned int)exl[idx].adr, (unsigned int)exl[idx].adr+exl[idx].len);
 358                     }
 359                     draw_string(camera_screen.disp_left, showidx, txt, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 360                 }
 361                 showidx += FONT_HEIGHT;
 362                 idx++;
 363             }
 364         }
 365     }
 366 
 367     exmeminspect_redraw = 0;
 368 }
 369 

/* [<][>][^][v][top][bottom][index][help] */