debug.c 35 KB

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