debug.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * arch/s390/kernel/debug.c
  3. * S/390 debug facility
  4. *
  5. * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Michael Holzheu (holzheu@de.ibm.com),
  8. * Holger Smolinski (Holger.Smolinski@de.ibm.com)
  9. *
  10. * Bugreports to: <Linux390@de.ibm.com>
  11. */
  12. #include <linux/config.h>
  13. #include <linux/stddef.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/ctype.h>
  18. #include <linux/sysctl.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/semaphore.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/fs.h>
  24. #include <linux/debugfs.h>
  25. #include <asm/debug.h>
  26. #define DEBUG_PROLOG_ENTRY -1
  27. #define ALL_AREAS 0 /* copy all debug areas */
  28. #define NO_AREAS 1 /* copy no debug areas */
  29. /* typedefs */
  30. typedef struct file_private_info {
  31. loff_t offset; /* offset of last read in file */
  32. int act_area; /* number of last formated area */
  33. int act_page; /* act page in given area */
  34. int act_entry; /* last formated entry (offset */
  35. /* relative to beginning of last */
  36. /* formated page) */
  37. size_t act_entry_offset; /* up to this offset we copied */
  38. /* in last read the last formated */
  39. /* entry to userland */
  40. char temp_buf[2048]; /* buffer for output */
  41. debug_info_t *debug_info_org; /* original debug information */
  42. debug_info_t *debug_info_snap; /* snapshot of debug information */
  43. struct debug_view *view; /* used view of debug info */
  44. } file_private_info_t;
  45. typedef struct
  46. {
  47. char *string;
  48. /*
  49. * This assumes that all args are converted into longs
  50. * on L/390 this is the case for all types of parameter
  51. * except of floats, and long long (32 bit)
  52. *
  53. */
  54. long args[0];
  55. } debug_sprintf_entry_t;
  56. extern void tod_to_timeval(uint64_t todval, struct timeval *xtime);
  57. /* internal function prototyes */
  58. static int debug_init(void);
  59. static ssize_t debug_output(struct file *file, char __user *user_buf,
  60. size_t user_len, loff_t * offset);
  61. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  62. size_t user_len, loff_t * offset);
  63. static int debug_open(struct inode *inode, struct file *file);
  64. static int debug_close(struct inode *inode, struct file *file);
  65. static debug_info_t* debug_info_create(char *name, int pages_per_area,
  66. int nr_areas, int buf_size);
  67. static void debug_info_get(debug_info_t *);
  68. static void debug_info_put(debug_info_t *);
  69. static int debug_prolog_level_fn(debug_info_t * id,
  70. struct debug_view *view, char *out_buf);
  71. static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  72. struct file *file, const char __user *user_buf,
  73. size_t user_buf_size, loff_t * offset);
  74. static int debug_prolog_pages_fn(debug_info_t * id,
  75. struct debug_view *view, char *out_buf);
  76. static int debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  77. struct file *file, const char __user *user_buf,
  78. size_t user_buf_size, loff_t * offset);
  79. static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  80. struct file *file, const char __user *user_buf,
  81. size_t user_buf_size, loff_t * offset);
  82. static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  83. char *out_buf, const char *in_buf);
  84. static int debug_raw_format_fn(debug_info_t * id,
  85. struct debug_view *view, char *out_buf,
  86. const char *in_buf);
  87. static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  88. int area, debug_entry_t * entry, char *out_buf);
  89. static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  90. char *out_buf, debug_sprintf_entry_t *curr_event);
  91. /* globals */
  92. struct debug_view debug_raw_view = {
  93. "raw",
  94. NULL,
  95. &debug_raw_header_fn,
  96. &debug_raw_format_fn,
  97. NULL,
  98. NULL
  99. };
  100. struct debug_view debug_hex_ascii_view = {
  101. "hex_ascii",
  102. NULL,
  103. &debug_dflt_header_fn,
  104. &debug_hex_ascii_format_fn,
  105. NULL,
  106. NULL
  107. };
  108. struct debug_view debug_level_view = {
  109. "level",
  110. &debug_prolog_level_fn,
  111. NULL,
  112. NULL,
  113. &debug_input_level_fn,
  114. NULL
  115. };
  116. struct debug_view debug_pages_view = {
  117. "pages",
  118. &debug_prolog_pages_fn,
  119. NULL,
  120. NULL,
  121. &debug_input_pages_fn,
  122. NULL
  123. };
  124. struct debug_view debug_flush_view = {
  125. "flush",
  126. NULL,
  127. NULL,
  128. NULL,
  129. &debug_input_flush_fn,
  130. NULL
  131. };
  132. struct debug_view debug_sprintf_view = {
  133. "sprintf",
  134. NULL,
  135. &debug_dflt_header_fn,
  136. (debug_format_proc_t*)&debug_sprintf_format_fn,
  137. NULL,
  138. NULL
  139. };
  140. unsigned int debug_feature_version = __DEBUG_FEATURE_VERSION;
  141. /* static globals */
  142. static debug_info_t *debug_area_first = NULL;
  143. static debug_info_t *debug_area_last = NULL;
  144. DECLARE_MUTEX(debug_lock);
  145. static int initialized;
  146. static struct file_operations debug_file_ops = {
  147. .owner = THIS_MODULE,
  148. .read = debug_output,
  149. .write = debug_input,
  150. .open = debug_open,
  151. .release = debug_close,
  152. };
  153. static struct dentry *debug_debugfs_root_entry;
  154. /* functions */
  155. /*
  156. * debug_areas_alloc
  157. * - Debug areas are implemented as a threedimensonal array:
  158. * areas[areanumber][pagenumber][pageoffset]
  159. */
  160. static debug_entry_t***
  161. debug_areas_alloc(int pages_per_area, int nr_areas)
  162. {
  163. debug_entry_t*** areas;
  164. int i,j;
  165. areas = (debug_entry_t ***) kmalloc(nr_areas *
  166. sizeof(debug_entry_t**),
  167. GFP_KERNEL);
  168. if (!areas)
  169. goto fail_malloc_areas;
  170. for (i = 0; i < nr_areas; i++) {
  171. areas[i] = (debug_entry_t**) kmalloc(pages_per_area *
  172. sizeof(debug_entry_t*),GFP_KERNEL);
  173. if (!areas[i]) {
  174. goto fail_malloc_areas2;
  175. }
  176. for(j = 0; j < pages_per_area; j++) {
  177. areas[i][j] = (debug_entry_t*)kmalloc(PAGE_SIZE,
  178. GFP_KERNEL);
  179. if(!areas[i][j]) {
  180. for(j--; j >=0 ; j--) {
  181. kfree(areas[i][j]);
  182. }
  183. kfree(areas[i]);
  184. goto fail_malloc_areas2;
  185. } else {
  186. memset(areas[i][j],0,PAGE_SIZE);
  187. }
  188. }
  189. }
  190. return areas;
  191. fail_malloc_areas2:
  192. for(i--; i >= 0; i--){
  193. for(j=0; j < pages_per_area;j++){
  194. kfree(areas[i][j]);
  195. }
  196. kfree(areas[i]);
  197. }
  198. kfree(areas);
  199. fail_malloc_areas:
  200. return NULL;
  201. }
  202. /*
  203. * debug_info_alloc
  204. * - alloc new debug-info
  205. */
  206. static debug_info_t*
  207. debug_info_alloc(char *name, int pages_per_area, int nr_areas, int buf_size,
  208. int level, int mode)
  209. {
  210. debug_info_t* rc;
  211. /* alloc everything */
  212. rc = (debug_info_t*) kmalloc(sizeof(debug_info_t), GFP_KERNEL);
  213. if(!rc)
  214. goto fail_malloc_rc;
  215. rc->active_entries = (int*)kmalloc(nr_areas * sizeof(int), GFP_KERNEL);
  216. if(!rc->active_entries)
  217. goto fail_malloc_active_entries;
  218. memset(rc->active_entries, 0, nr_areas * sizeof(int));
  219. rc->active_pages = (int*)kmalloc(nr_areas * sizeof(int), GFP_KERNEL);
  220. if(!rc->active_pages)
  221. goto fail_malloc_active_pages;
  222. memset(rc->active_pages, 0, nr_areas * sizeof(int));
  223. if((mode == ALL_AREAS) && (pages_per_area != 0)){
  224. rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
  225. if(!rc->areas)
  226. goto fail_malloc_areas;
  227. } else {
  228. rc->areas = NULL;
  229. }
  230. /* initialize members */
  231. spin_lock_init(&rc->lock);
  232. rc->pages_per_area = pages_per_area;
  233. rc->nr_areas = nr_areas;
  234. rc->active_area = 0;
  235. rc->level = level;
  236. rc->buf_size = buf_size;
  237. rc->entry_size = sizeof(debug_entry_t) + buf_size;
  238. strlcpy(rc->name, name, sizeof(rc->name)-1);
  239. memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
  240. memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
  241. sizeof(struct dentry*));
  242. atomic_set(&(rc->ref_count), 0);
  243. return rc;
  244. fail_malloc_areas:
  245. kfree(rc->active_pages);
  246. fail_malloc_active_pages:
  247. kfree(rc->active_entries);
  248. fail_malloc_active_entries:
  249. kfree(rc);
  250. fail_malloc_rc:
  251. return NULL;
  252. }
  253. /*
  254. * debug_areas_free
  255. * - free all debug areas
  256. */
  257. static void
  258. debug_areas_free(debug_info_t* db_info)
  259. {
  260. int i,j;
  261. if(!db_info->areas)
  262. return;
  263. for (i = 0; i < db_info->nr_areas; i++) {
  264. for(j = 0; j < db_info->pages_per_area; j++) {
  265. kfree(db_info->areas[i][j]);
  266. }
  267. kfree(db_info->areas[i]);
  268. }
  269. kfree(db_info->areas);
  270. db_info->areas = NULL;
  271. }
  272. /*
  273. * debug_info_free
  274. * - free memory debug-info
  275. */
  276. static void
  277. debug_info_free(debug_info_t* db_info){
  278. debug_areas_free(db_info);
  279. kfree(db_info->active_entries);
  280. kfree(db_info->active_pages);
  281. kfree(db_info);
  282. }
  283. /*
  284. * debug_info_create
  285. * - create new debug-info
  286. */
  287. static debug_info_t*
  288. debug_info_create(char *name, int pages_per_area, int nr_areas, int buf_size)
  289. {
  290. debug_info_t* rc;
  291. rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
  292. DEBUG_DEFAULT_LEVEL, ALL_AREAS);
  293. if(!rc)
  294. goto out;
  295. /* create root directory */
  296. rc->debugfs_root_entry = debugfs_create_dir(rc->name,
  297. debug_debugfs_root_entry);
  298. /* append new element to linked list */
  299. if (!debug_area_first) {
  300. /* first element in list */
  301. debug_area_first = rc;
  302. rc->prev = NULL;
  303. } else {
  304. /* append element to end of list */
  305. debug_area_last->next = rc;
  306. rc->prev = debug_area_last;
  307. }
  308. debug_area_last = rc;
  309. rc->next = NULL;
  310. debug_info_get(rc);
  311. out:
  312. return rc;
  313. }
  314. /*
  315. * debug_info_copy
  316. * - copy debug-info
  317. */
  318. static debug_info_t*
  319. debug_info_copy(debug_info_t* in, int mode)
  320. {
  321. int i,j;
  322. debug_info_t* rc;
  323. rc = debug_info_alloc(in->name, in->pages_per_area, in->nr_areas,
  324. in->buf_size, in->level, mode);
  325. if(!rc || (mode == NO_AREAS))
  326. goto out;
  327. for(i = 0; i < in->nr_areas; i++){
  328. for(j = 0; j < in->pages_per_area; j++) {
  329. memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
  330. }
  331. }
  332. out:
  333. return rc;
  334. }
  335. /*
  336. * debug_info_get
  337. * - increments reference count for debug-info
  338. */
  339. static void
  340. debug_info_get(debug_info_t * db_info)
  341. {
  342. if (db_info)
  343. atomic_inc(&db_info->ref_count);
  344. }
  345. /*
  346. * debug_info_put:
  347. * - decreases reference count for debug-info and frees it if necessary
  348. */
  349. static void
  350. debug_info_put(debug_info_t *db_info)
  351. {
  352. int i;
  353. if (!db_info)
  354. return;
  355. if (atomic_dec_and_test(&db_info->ref_count)) {
  356. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  357. if (!db_info->views[i])
  358. continue;
  359. debugfs_remove(db_info->debugfs_entries[i]);
  360. }
  361. debugfs_remove(db_info->debugfs_root_entry);
  362. if(db_info == debug_area_first)
  363. debug_area_first = db_info->next;
  364. if(db_info == debug_area_last)
  365. debug_area_last = db_info->prev;
  366. if(db_info->prev) db_info->prev->next = db_info->next;
  367. if(db_info->next) db_info->next->prev = db_info->prev;
  368. debug_info_free(db_info);
  369. }
  370. }
  371. /*
  372. * debug_format_entry:
  373. * - format one debug entry and return size of formated data
  374. */
  375. static int
  376. debug_format_entry(file_private_info_t *p_info)
  377. {
  378. debug_info_t *id_snap = p_info->debug_info_snap;
  379. struct debug_view *view = p_info->view;
  380. debug_entry_t *act_entry;
  381. size_t len = 0;
  382. if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
  383. /* print prolog */
  384. if (view->prolog_proc)
  385. len += view->prolog_proc(id_snap,view,p_info->temp_buf);
  386. goto out;
  387. }
  388. if (!id_snap->areas) /* this is true, if we have a prolog only view */
  389. goto out; /* or if 'pages_per_area' is 0 */
  390. act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
  391. [p_info->act_page] + p_info->act_entry);
  392. if (act_entry->id.stck == 0LL)
  393. goto out; /* empty entry */
  394. if (view->header_proc)
  395. len += view->header_proc(id_snap, view, p_info->act_area,
  396. act_entry, p_info->temp_buf + len);
  397. if (view->format_proc)
  398. len += view->format_proc(id_snap, view, p_info->temp_buf + len,
  399. DEBUG_DATA(act_entry));
  400. out:
  401. return len;
  402. }
  403. /*
  404. * debug_next_entry:
  405. * - goto next entry in p_info
  406. */
  407. extern inline int
  408. debug_next_entry(file_private_info_t *p_info)
  409. {
  410. debug_info_t *id;
  411. id = p_info->debug_info_snap;
  412. if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
  413. p_info->act_entry = 0;
  414. p_info->act_page = 0;
  415. goto out;
  416. }
  417. if(!id->areas)
  418. return 1;
  419. p_info->act_entry += id->entry_size;
  420. /* switch to next page, if we reached the end of the page */
  421. if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
  422. /* next page */
  423. p_info->act_entry = 0;
  424. p_info->act_page += 1;
  425. if((p_info->act_page % id->pages_per_area) == 0) {
  426. /* next area */
  427. p_info->act_area++;
  428. p_info->act_page=0;
  429. }
  430. if(p_info->act_area >= id->nr_areas)
  431. return 1;
  432. }
  433. out:
  434. return 0;
  435. }
  436. /*
  437. * debug_output:
  438. * - called for user read()
  439. * - copies formated debug entries to the user buffer
  440. */
  441. static ssize_t
  442. debug_output(struct file *file, /* file descriptor */
  443. char __user *user_buf, /* user buffer */
  444. size_t len, /* length of buffer */
  445. loff_t *offset) /* offset in the file */
  446. {
  447. size_t count = 0;
  448. size_t entry_offset;
  449. file_private_info_t *p_info;
  450. p_info = ((file_private_info_t *) file->private_data);
  451. if (*offset != p_info->offset)
  452. return -EPIPE;
  453. if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
  454. return 0;
  455. entry_offset = p_info->act_entry_offset;
  456. while(count < len){
  457. int formatted_line_size;
  458. int formatted_line_residue;
  459. int user_buf_residue;
  460. size_t copy_size;
  461. formatted_line_size = debug_format_entry(p_info);
  462. formatted_line_residue = formatted_line_size - entry_offset;
  463. user_buf_residue = len-count;
  464. copy_size = min(user_buf_residue, formatted_line_residue);
  465. if(copy_size){
  466. if (copy_to_user(user_buf + count, p_info->temp_buf
  467. + entry_offset, copy_size))
  468. return -EFAULT;
  469. count += copy_size;
  470. entry_offset += copy_size;
  471. }
  472. if(copy_size == formatted_line_residue){
  473. entry_offset = 0;
  474. if(debug_next_entry(p_info))
  475. goto out;
  476. }
  477. }
  478. out:
  479. p_info->offset = *offset + count;
  480. p_info->act_entry_offset = entry_offset;
  481. *offset = p_info->offset;
  482. return count;
  483. }
  484. /*
  485. * debug_input:
  486. * - called for user write()
  487. * - calls input function of view
  488. */
  489. static ssize_t
  490. debug_input(struct file *file, const char __user *user_buf, size_t length,
  491. loff_t *offset)
  492. {
  493. int rc = 0;
  494. file_private_info_t *p_info;
  495. down(&debug_lock);
  496. p_info = ((file_private_info_t *) file->private_data);
  497. if (p_info->view->input_proc)
  498. rc = p_info->view->input_proc(p_info->debug_info_org,
  499. p_info->view, file, user_buf,
  500. length, offset);
  501. else
  502. rc = -EPERM;
  503. up(&debug_lock);
  504. return rc; /* number of input characters */
  505. }
  506. /*
  507. * debug_open:
  508. * - called for user open()
  509. * - copies formated output to private_data area of the file
  510. * handle
  511. */
  512. static int
  513. debug_open(struct inode *inode, struct file *file)
  514. {
  515. int i = 0, rc = 0;
  516. file_private_info_t *p_info;
  517. debug_info_t *debug_info, *debug_info_snapshot;
  518. down(&debug_lock);
  519. /* find debug log and view */
  520. debug_info = debug_area_first;
  521. while(debug_info != NULL){
  522. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  523. if (!debug_info->views[i])
  524. continue;
  525. else if (debug_info->debugfs_entries[i] ==
  526. file->f_dentry) {
  527. goto found; /* found view ! */
  528. }
  529. }
  530. debug_info = debug_info->next;
  531. }
  532. /* no entry found */
  533. rc = -EINVAL;
  534. goto out;
  535. found:
  536. /* Make snapshot of current debug areas to get it consistent. */
  537. /* To copy all the areas is only needed, if we have a view which */
  538. /* formats the debug areas. */
  539. if(!debug_info->views[i]->format_proc &&
  540. !debug_info->views[i]->header_proc){
  541. debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
  542. } else {
  543. debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
  544. }
  545. if(!debug_info_snapshot){
  546. rc = -ENOMEM;
  547. goto out;
  548. }
  549. p_info = (file_private_info_t *) kmalloc(sizeof(file_private_info_t),
  550. GFP_KERNEL);
  551. if(!p_info){
  552. if(debug_info_snapshot)
  553. debug_info_free(debug_info_snapshot);
  554. rc = -ENOMEM;
  555. goto out;
  556. }
  557. p_info->offset = 0;
  558. p_info->debug_info_snap = debug_info_snapshot;
  559. p_info->debug_info_org = debug_info;
  560. p_info->view = debug_info->views[i];
  561. p_info->act_area = 0;
  562. p_info->act_page = 0;
  563. p_info->act_entry = DEBUG_PROLOG_ENTRY;
  564. p_info->act_entry_offset = 0;
  565. file->private_data = p_info;
  566. debug_info_get(debug_info);
  567. out:
  568. up(&debug_lock);
  569. return rc;
  570. }
  571. /*
  572. * debug_close:
  573. * - called for user close()
  574. * - deletes private_data area of the file handle
  575. */
  576. static int
  577. debug_close(struct inode *inode, struct file *file)
  578. {
  579. file_private_info_t *p_info;
  580. p_info = (file_private_info_t *) file->private_data;
  581. if(p_info->debug_info_snap)
  582. debug_info_free(p_info->debug_info_snap);
  583. debug_info_put(p_info->debug_info_org);
  584. kfree(file->private_data);
  585. return 0; /* success */
  586. }
  587. /*
  588. * debug_register:
  589. * - creates and initializes debug area for the caller
  590. * - returns handle for debug area
  591. */
  592. debug_info_t*
  593. debug_register (char *name, int pages_per_area, int nr_areas, int buf_size)
  594. {
  595. debug_info_t *rc = NULL;
  596. if (!initialized)
  597. BUG();
  598. down(&debug_lock);
  599. /* create new debug_info */
  600. rc = debug_info_create(name, pages_per_area, nr_areas, buf_size);
  601. if(!rc)
  602. goto out;
  603. debug_register_view(rc, &debug_level_view);
  604. debug_register_view(rc, &debug_flush_view);
  605. debug_register_view(rc, &debug_pages_view);
  606. out:
  607. if (!rc){
  608. printk(KERN_ERR "debug: debug_register failed for %s\n",name);
  609. }
  610. up(&debug_lock);
  611. return rc;
  612. }
  613. /*
  614. * debug_unregister:
  615. * - give back debug area
  616. */
  617. void
  618. debug_unregister(debug_info_t * id)
  619. {
  620. if (!id)
  621. goto out;
  622. down(&debug_lock);
  623. debug_info_put(id);
  624. up(&debug_lock);
  625. out:
  626. return;
  627. }
  628. /*
  629. * debug_set_size:
  630. * - set area size (number of pages) and number of areas
  631. */
  632. static int
  633. debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
  634. {
  635. unsigned long flags;
  636. debug_entry_t *** new_areas;
  637. int rc=0;
  638. if(!id || (nr_areas <= 0) || (pages_per_area < 0))
  639. return -EINVAL;
  640. if(pages_per_area > 0){
  641. new_areas = debug_areas_alloc(pages_per_area, nr_areas);
  642. if(!new_areas) {
  643. printk(KERN_WARNING "debug: could not allocate memory "\
  644. "for pagenumber: %i\n",pages_per_area);
  645. rc = -ENOMEM;
  646. goto out;
  647. }
  648. } else {
  649. new_areas = NULL;
  650. }
  651. spin_lock_irqsave(&id->lock,flags);
  652. debug_areas_free(id);
  653. id->areas = new_areas;
  654. id->nr_areas = nr_areas;
  655. id->pages_per_area = pages_per_area;
  656. id->active_area = 0;
  657. memset(id->active_entries,0,sizeof(int)*id->nr_areas);
  658. memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
  659. spin_unlock_irqrestore(&id->lock,flags);
  660. printk(KERN_INFO "debug: %s: set new size (%i pages)\n"\
  661. ,id->name, pages_per_area);
  662. out:
  663. return rc;
  664. }
  665. /*
  666. * debug_set_level:
  667. * - set actual debug level
  668. */
  669. void
  670. debug_set_level(debug_info_t* id, int new_level)
  671. {
  672. unsigned long flags;
  673. if(!id)
  674. return;
  675. spin_lock_irqsave(&id->lock,flags);
  676. if(new_level == DEBUG_OFF_LEVEL){
  677. id->level = DEBUG_OFF_LEVEL;
  678. printk(KERN_INFO "debug: %s: switched off\n",id->name);
  679. } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
  680. printk(KERN_INFO
  681. "debug: %s: level %i is out of range (%i - %i)\n",
  682. id->name, new_level, 0, DEBUG_MAX_LEVEL);
  683. } else {
  684. id->level = new_level;
  685. }
  686. spin_unlock_irqrestore(&id->lock,flags);
  687. }
  688. /*
  689. * proceed_active_entry:
  690. * - set active entry to next in the ring buffer
  691. */
  692. extern inline void
  693. proceed_active_entry(debug_info_t * id)
  694. {
  695. if ((id->active_entries[id->active_area] += id->entry_size)
  696. > (PAGE_SIZE - id->entry_size)){
  697. id->active_entries[id->active_area] = 0;
  698. id->active_pages[id->active_area] =
  699. (id->active_pages[id->active_area] + 1) %
  700. id->pages_per_area;
  701. }
  702. }
  703. /*
  704. * proceed_active_area:
  705. * - set active area to next in the ring buffer
  706. */
  707. extern inline void
  708. proceed_active_area(debug_info_t * id)
  709. {
  710. id->active_area++;
  711. id->active_area = id->active_area % id->nr_areas;
  712. }
  713. /*
  714. * get_active_entry:
  715. */
  716. extern inline debug_entry_t*
  717. get_active_entry(debug_info_t * id)
  718. {
  719. return (debug_entry_t *) (((char *) id->areas[id->active_area]
  720. [id->active_pages[id->active_area]]) +
  721. id->active_entries[id->active_area]);
  722. }
  723. /*
  724. * debug_finish_entry:
  725. * - set timestamp, caller address, cpu number etc.
  726. */
  727. extern inline void
  728. debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
  729. int exception)
  730. {
  731. STCK(active->id.stck);
  732. active->id.fields.cpuid = smp_processor_id();
  733. active->caller = __builtin_return_address(0);
  734. active->id.fields.exception = exception;
  735. active->id.fields.level = level;
  736. proceed_active_entry(id);
  737. if(exception)
  738. proceed_active_area(id);
  739. }
  740. static int debug_stoppable=1;
  741. static int debug_active=1;
  742. #define CTL_S390DBF 5677
  743. #define CTL_S390DBF_STOPPABLE 5678
  744. #define CTL_S390DBF_ACTIVE 5679
  745. /*
  746. * proc handler for the running debug_active sysctl
  747. * always allow read, allow write only if debug_stoppable is set or
  748. * if debug_active is already off
  749. */
  750. static int
  751. s390dbf_procactive(ctl_table *table, int write, struct file *filp,
  752. void __user *buffer, size_t *lenp, loff_t *ppos)
  753. {
  754. if (!write || debug_stoppable || !debug_active)
  755. return proc_dointvec(table, write, filp, buffer, lenp, ppos);
  756. else
  757. return 0;
  758. }
  759. static struct ctl_table s390dbf_table[] = {
  760. {
  761. .ctl_name = CTL_S390DBF_STOPPABLE,
  762. .procname = "debug_stoppable",
  763. .data = &debug_stoppable,
  764. .maxlen = sizeof(int),
  765. .mode = S_IRUGO | S_IWUSR,
  766. .proc_handler = &proc_dointvec,
  767. .strategy = &sysctl_intvec,
  768. },
  769. {
  770. .ctl_name = CTL_S390DBF_ACTIVE,
  771. .procname = "debug_active",
  772. .data = &debug_active,
  773. .maxlen = sizeof(int),
  774. .mode = S_IRUGO | S_IWUSR,
  775. .proc_handler = &s390dbf_procactive,
  776. .strategy = &sysctl_intvec,
  777. },
  778. { .ctl_name = 0 }
  779. };
  780. static struct ctl_table s390dbf_dir_table[] = {
  781. {
  782. .ctl_name = CTL_S390DBF,
  783. .procname = "s390dbf",
  784. .maxlen = 0,
  785. .mode = S_IRUGO | S_IXUGO,
  786. .child = s390dbf_table,
  787. },
  788. { .ctl_name = 0 }
  789. };
  790. struct ctl_table_header *s390dbf_sysctl_header;
  791. void
  792. debug_stop_all(void)
  793. {
  794. if (debug_stoppable)
  795. debug_active = 0;
  796. }
  797. /*
  798. * debug_event_common:
  799. * - write debug entry with given size
  800. */
  801. debug_entry_t*
  802. debug_event_common(debug_info_t * id, int level, const void *buf, int len)
  803. {
  804. unsigned long flags;
  805. debug_entry_t *active;
  806. if (!debug_active || !id->areas)
  807. return NULL;
  808. spin_lock_irqsave(&id->lock, flags);
  809. active = get_active_entry(id);
  810. memset(DEBUG_DATA(active), 0, id->buf_size);
  811. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  812. debug_finish_entry(id, active, level, 0);
  813. spin_unlock_irqrestore(&id->lock, flags);
  814. return active;
  815. }
  816. /*
  817. * debug_exception_common:
  818. * - write debug entry with given size and switch to next debug area
  819. */
  820. debug_entry_t
  821. *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
  822. {
  823. unsigned long flags;
  824. debug_entry_t *active;
  825. if (!debug_active || !id->areas)
  826. return NULL;
  827. spin_lock_irqsave(&id->lock, flags);
  828. active = get_active_entry(id);
  829. memset(DEBUG_DATA(active), 0, id->buf_size);
  830. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  831. debug_finish_entry(id, active, level, 1);
  832. spin_unlock_irqrestore(&id->lock, flags);
  833. return active;
  834. }
  835. /*
  836. * counts arguments in format string for sprintf view
  837. */
  838. extern inline int
  839. debug_count_numargs(char *string)
  840. {
  841. int numargs=0;
  842. while(*string) {
  843. if(*string++=='%')
  844. numargs++;
  845. }
  846. return(numargs);
  847. }
  848. /*
  849. * debug_sprintf_event:
  850. */
  851. debug_entry_t*
  852. debug_sprintf_event(debug_info_t* id, int level,char *string,...)
  853. {
  854. va_list ap;
  855. int numargs,idx;
  856. unsigned long flags;
  857. debug_sprintf_entry_t *curr_event;
  858. debug_entry_t *active;
  859. if((!id) || (level > id->level))
  860. return NULL;
  861. if (!debug_active || !id->areas)
  862. return NULL;
  863. numargs=debug_count_numargs(string);
  864. spin_lock_irqsave(&id->lock, flags);
  865. active = get_active_entry(id);
  866. curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
  867. va_start(ap,string);
  868. curr_event->string=string;
  869. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  870. curr_event->args[idx]=va_arg(ap,long);
  871. va_end(ap);
  872. debug_finish_entry(id, active, level, 0);
  873. spin_unlock_irqrestore(&id->lock, flags);
  874. return active;
  875. }
  876. /*
  877. * debug_sprintf_exception:
  878. */
  879. debug_entry_t*
  880. debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
  881. {
  882. va_list ap;
  883. int numargs,idx;
  884. unsigned long flags;
  885. debug_sprintf_entry_t *curr_event;
  886. debug_entry_t *active;
  887. if((!id) || (level > id->level))
  888. return NULL;
  889. if (!debug_active || !id->areas)
  890. return NULL;
  891. numargs=debug_count_numargs(string);
  892. spin_lock_irqsave(&id->lock, flags);
  893. active = get_active_entry(id);
  894. curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
  895. va_start(ap,string);
  896. curr_event->string=string;
  897. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  898. curr_event->args[idx]=va_arg(ap,long);
  899. va_end(ap);
  900. debug_finish_entry(id, active, level, 1);
  901. spin_unlock_irqrestore(&id->lock, flags);
  902. return active;
  903. }
  904. /*
  905. * debug_init:
  906. * - is called exactly once to initialize the debug feature
  907. */
  908. static int
  909. __init debug_init(void)
  910. {
  911. int rc = 0;
  912. s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table, 1);
  913. down(&debug_lock);
  914. debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
  915. printk(KERN_INFO "debug: Initialization complete\n");
  916. initialized = 1;
  917. up(&debug_lock);
  918. return rc;
  919. }
  920. /*
  921. * debug_register_view:
  922. */
  923. int
  924. debug_register_view(debug_info_t * id, struct debug_view *view)
  925. {
  926. int rc = 0;
  927. int i;
  928. unsigned long flags;
  929. mode_t mode = S_IFREG;
  930. struct dentry *pde;
  931. if (!id)
  932. goto out;
  933. if (view->prolog_proc || view->format_proc || view->header_proc)
  934. mode |= S_IRUSR;
  935. if (view->input_proc)
  936. mode |= S_IWUSR;
  937. pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
  938. NULL, &debug_file_ops);
  939. if (!pde){
  940. printk(KERN_WARNING "debug: debugfs_create_file() failed!"\
  941. " Cannot register view %s/%s\n", id->name,view->name);
  942. rc = -1;
  943. goto out;
  944. }
  945. spin_lock_irqsave(&id->lock, flags);
  946. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  947. if (!id->views[i])
  948. break;
  949. }
  950. if (i == DEBUG_MAX_VIEWS) {
  951. printk(KERN_WARNING "debug: cannot register view %s/%s\n",
  952. id->name,view->name);
  953. printk(KERN_WARNING
  954. "debug: maximum number of views reached (%i)!\n", i);
  955. debugfs_remove(pde);
  956. rc = -1;
  957. } else {
  958. id->views[i] = view;
  959. id->debugfs_entries[i] = pde;
  960. }
  961. spin_unlock_irqrestore(&id->lock, flags);
  962. out:
  963. return rc;
  964. }
  965. /*
  966. * debug_unregister_view:
  967. */
  968. int
  969. debug_unregister_view(debug_info_t * id, struct debug_view *view)
  970. {
  971. int rc = 0;
  972. int i;
  973. unsigned long flags;
  974. if (!id)
  975. goto out;
  976. spin_lock_irqsave(&id->lock, flags);
  977. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  978. if (id->views[i] == view)
  979. break;
  980. }
  981. if (i == DEBUG_MAX_VIEWS)
  982. rc = -1;
  983. else {
  984. debugfs_remove(id->debugfs_entries[i]);
  985. id->views[i] = NULL;
  986. rc = 0;
  987. }
  988. spin_unlock_irqrestore(&id->lock, flags);
  989. out:
  990. return rc;
  991. }
  992. static inline char *
  993. debug_get_user_string(const char __user *user_buf, size_t user_len)
  994. {
  995. char* buffer;
  996. buffer = kmalloc(user_len + 1, GFP_KERNEL);
  997. if (!buffer)
  998. return ERR_PTR(-ENOMEM);
  999. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  1000. kfree(buffer);
  1001. return ERR_PTR(-EFAULT);
  1002. }
  1003. /* got the string, now strip linefeed. */
  1004. if (buffer[user_len - 1] == '\n')
  1005. buffer[user_len - 1] = 0;
  1006. else
  1007. buffer[user_len] = 0;
  1008. return buffer;
  1009. }
  1010. static inline int
  1011. debug_get_uint(char *buf)
  1012. {
  1013. int rc;
  1014. for(; isspace(*buf); buf++);
  1015. rc = simple_strtoul(buf, &buf, 10);
  1016. if(*buf){
  1017. printk("debug: no integer specified!\n");
  1018. rc = -EINVAL;
  1019. }
  1020. return rc;
  1021. }
  1022. /*
  1023. * functions for debug-views
  1024. ***********************************
  1025. */
  1026. /*
  1027. * prints out actual debug level
  1028. */
  1029. static int
  1030. debug_prolog_pages_fn(debug_info_t * id,
  1031. struct debug_view *view, char *out_buf)
  1032. {
  1033. return sprintf(out_buf, "%i\n", id->pages_per_area);
  1034. }
  1035. /*
  1036. * reads new size (number of pages per debug area)
  1037. */
  1038. static int
  1039. debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  1040. struct file *file, const char __user *user_buf,
  1041. size_t user_len, loff_t * offset)
  1042. {
  1043. char *str;
  1044. int rc,new_pages;
  1045. if (user_len > 0x10000)
  1046. user_len = 0x10000;
  1047. if (*offset != 0){
  1048. rc = -EPIPE;
  1049. goto out;
  1050. }
  1051. str = debug_get_user_string(user_buf,user_len);
  1052. if(IS_ERR(str)){
  1053. rc = PTR_ERR(str);
  1054. goto out;
  1055. }
  1056. new_pages = debug_get_uint(str);
  1057. if(new_pages < 0){
  1058. rc = -EINVAL;
  1059. goto free_str;
  1060. }
  1061. rc = debug_set_size(id,id->nr_areas, new_pages);
  1062. if(rc != 0){
  1063. rc = -EINVAL;
  1064. goto free_str;
  1065. }
  1066. rc = user_len;
  1067. free_str:
  1068. kfree(str);
  1069. out:
  1070. *offset += user_len;
  1071. return rc; /* number of input characters */
  1072. }
  1073. /*
  1074. * prints out actual debug level
  1075. */
  1076. static int
  1077. debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
  1078. {
  1079. int rc = 0;
  1080. if(id->level == DEBUG_OFF_LEVEL) {
  1081. rc = sprintf(out_buf,"-\n");
  1082. }
  1083. else {
  1084. rc = sprintf(out_buf, "%i\n", id->level);
  1085. }
  1086. return rc;
  1087. }
  1088. /*
  1089. * reads new debug level
  1090. */
  1091. static int
  1092. debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  1093. struct file *file, const char __user *user_buf,
  1094. size_t user_len, loff_t * offset)
  1095. {
  1096. char *str;
  1097. int rc,new_level;
  1098. if (user_len > 0x10000)
  1099. user_len = 0x10000;
  1100. if (*offset != 0){
  1101. rc = -EPIPE;
  1102. goto out;
  1103. }
  1104. str = debug_get_user_string(user_buf,user_len);
  1105. if(IS_ERR(str)){
  1106. rc = PTR_ERR(str);
  1107. goto out;
  1108. }
  1109. if(str[0] == '-'){
  1110. debug_set_level(id, DEBUG_OFF_LEVEL);
  1111. rc = user_len;
  1112. goto free_str;
  1113. } else {
  1114. new_level = debug_get_uint(str);
  1115. }
  1116. if(new_level < 0) {
  1117. printk(KERN_INFO "debug: level `%s` is not valid\n", str);
  1118. rc = -EINVAL;
  1119. } else {
  1120. debug_set_level(id, new_level);
  1121. rc = user_len;
  1122. }
  1123. free_str:
  1124. kfree(str);
  1125. out:
  1126. *offset += user_len;
  1127. return rc; /* number of input characters */
  1128. }
  1129. /*
  1130. * flushes debug areas
  1131. */
  1132. void
  1133. debug_flush(debug_info_t* id, int area)
  1134. {
  1135. unsigned long flags;
  1136. int i,j;
  1137. if(!id || !id->areas)
  1138. return;
  1139. spin_lock_irqsave(&id->lock,flags);
  1140. if(area == DEBUG_FLUSH_ALL){
  1141. id->active_area = 0;
  1142. memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  1143. for (i = 0; i < id->nr_areas; i++) {
  1144. id->active_pages[i] = 0;
  1145. for(j = 0; j < id->pages_per_area; j++) {
  1146. memset(id->areas[i][j], 0, PAGE_SIZE);
  1147. }
  1148. }
  1149. printk(KERN_INFO "debug: %s: all areas flushed\n",id->name);
  1150. } else if(area >= 0 && area < id->nr_areas) {
  1151. id->active_entries[area] = 0;
  1152. id->active_pages[area] = 0;
  1153. for(i = 0; i < id->pages_per_area; i++) {
  1154. memset(id->areas[area][i],0,PAGE_SIZE);
  1155. }
  1156. printk(KERN_INFO "debug: %s: area %i has been flushed\n",
  1157. id->name, area);
  1158. } else {
  1159. printk(KERN_INFO
  1160. "debug: %s: area %i cannot be flushed (range: %i - %i)\n",
  1161. id->name, area, 0, id->nr_areas-1);
  1162. }
  1163. spin_unlock_irqrestore(&id->lock,flags);
  1164. }
  1165. /*
  1166. * view function: flushes debug areas
  1167. */
  1168. static int
  1169. debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  1170. struct file *file, const char __user *user_buf,
  1171. size_t user_len, loff_t * offset)
  1172. {
  1173. char input_buf[1];
  1174. int rc = user_len;
  1175. if (user_len > 0x10000)
  1176. user_len = 0x10000;
  1177. if (*offset != 0){
  1178. rc = -EPIPE;
  1179. goto out;
  1180. }
  1181. if (copy_from_user(input_buf, user_buf, 1)){
  1182. rc = -EFAULT;
  1183. goto out;
  1184. }
  1185. if(input_buf[0] == '-') {
  1186. debug_flush(id, DEBUG_FLUSH_ALL);
  1187. goto out;
  1188. }
  1189. if (isdigit(input_buf[0])) {
  1190. int area = ((int) input_buf[0] - (int) '0');
  1191. debug_flush(id, area);
  1192. goto out;
  1193. }
  1194. printk(KERN_INFO "debug: area `%c` is not valid\n", input_buf[0]);
  1195. out:
  1196. *offset += user_len;
  1197. return rc; /* number of input characters */
  1198. }
  1199. /*
  1200. * prints debug header in raw format
  1201. */
  1202. static int
  1203. debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  1204. int area, debug_entry_t * entry, char *out_buf)
  1205. {
  1206. int rc;
  1207. rc = sizeof(debug_entry_t);
  1208. memcpy(out_buf,entry,sizeof(debug_entry_t));
  1209. return rc;
  1210. }
  1211. /*
  1212. * prints debug data in raw format
  1213. */
  1214. static int
  1215. debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
  1216. char *out_buf, const char *in_buf)
  1217. {
  1218. int rc;
  1219. rc = id->buf_size;
  1220. memcpy(out_buf, in_buf, id->buf_size);
  1221. return rc;
  1222. }
  1223. /*
  1224. * prints debug data in hex/ascii format
  1225. */
  1226. static int
  1227. debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  1228. char *out_buf, const char *in_buf)
  1229. {
  1230. int i, rc = 0;
  1231. for (i = 0; i < id->buf_size; i++) {
  1232. rc += sprintf(out_buf + rc, "%02x ",
  1233. ((unsigned char *) in_buf)[i]);
  1234. }
  1235. rc += sprintf(out_buf + rc, "| ");
  1236. for (i = 0; i < id->buf_size; i++) {
  1237. unsigned char c = in_buf[i];
  1238. if (!isprint(c))
  1239. rc += sprintf(out_buf + rc, ".");
  1240. else
  1241. rc += sprintf(out_buf + rc, "%c", c);
  1242. }
  1243. rc += sprintf(out_buf + rc, "\n");
  1244. return rc;
  1245. }
  1246. /*
  1247. * prints header for debug entry
  1248. */
  1249. int
  1250. debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
  1251. int area, debug_entry_t * entry, char *out_buf)
  1252. {
  1253. struct timeval time_val;
  1254. unsigned long long time;
  1255. char *except_str;
  1256. unsigned long caller;
  1257. int rc = 0;
  1258. unsigned int level;
  1259. level = entry->id.fields.level;
  1260. time = entry->id.stck;
  1261. /* adjust todclock to 1970 */
  1262. time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
  1263. tod_to_timeval(time, &time_val);
  1264. if (entry->id.fields.exception)
  1265. except_str = "*";
  1266. else
  1267. except_str = "-";
  1268. caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
  1269. rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
  1270. area, time_val.tv_sec, time_val.tv_usec, level,
  1271. except_str, entry->id.fields.cpuid, (void *) caller);
  1272. return rc;
  1273. }
  1274. /*
  1275. * prints debug data sprintf-formated:
  1276. * debug_sprinf_event/exception calls must be used together with this view
  1277. */
  1278. #define DEBUG_SPRINTF_MAX_ARGS 10
  1279. static int
  1280. debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  1281. char *out_buf, debug_sprintf_entry_t *curr_event)
  1282. {
  1283. int num_longs, num_used_args = 0,i, rc = 0;
  1284. int index[DEBUG_SPRINTF_MAX_ARGS];
  1285. /* count of longs fit into one entry */
  1286. num_longs = id->buf_size / sizeof(long);
  1287. if(num_longs < 1)
  1288. goto out; /* bufsize of entry too small */
  1289. if(num_longs == 1) {
  1290. /* no args, we use only the string */
  1291. strcpy(out_buf, curr_event->string);
  1292. rc = strlen(curr_event->string);
  1293. goto out;
  1294. }
  1295. /* number of arguments used for sprintf (without the format string) */
  1296. num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
  1297. memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
  1298. for(i = 0; i < num_used_args; i++)
  1299. index[i] = i;
  1300. rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  1301. curr_event->args[index[1]], curr_event->args[index[2]],
  1302. curr_event->args[index[3]], curr_event->args[index[4]],
  1303. curr_event->args[index[5]], curr_event->args[index[6]],
  1304. curr_event->args[index[7]], curr_event->args[index[8]],
  1305. curr_event->args[index[9]]);
  1306. out:
  1307. return rc;
  1308. }
  1309. /*
  1310. * clean up module
  1311. */
  1312. void
  1313. __exit debug_exit(void)
  1314. {
  1315. debugfs_remove(debug_debugfs_root_entry);
  1316. unregister_sysctl_table(s390dbf_sysctl_header);
  1317. return;
  1318. }
  1319. /*
  1320. * module definitions
  1321. */
  1322. postcore_initcall(debug_init);
  1323. module_exit(debug_exit);
  1324. MODULE_LICENSE("GPL");
  1325. EXPORT_SYMBOL(debug_register);
  1326. EXPORT_SYMBOL(debug_unregister);
  1327. EXPORT_SYMBOL(debug_set_level);
  1328. EXPORT_SYMBOL(debug_stop_all);
  1329. EXPORT_SYMBOL(debug_register_view);
  1330. EXPORT_SYMBOL(debug_unregister_view);
  1331. EXPORT_SYMBOL(debug_event_common);
  1332. EXPORT_SYMBOL(debug_exception_common);
  1333. EXPORT_SYMBOL(debug_hex_ascii_view);
  1334. EXPORT_SYMBOL(debug_raw_view);
  1335. EXPORT_SYMBOL(debug_dflt_header_fn);
  1336. EXPORT_SYMBOL(debug_sprintf_view);
  1337. EXPORT_SYMBOL(debug_sprintf_exception);
  1338. EXPORT_SYMBOL(debug_sprintf_event);