debug.c 36 KB

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